moos-ivp 实现图像发送接收(三)

1.生成发布和订阅程序框架

./moos-ivp/scripts/GenMOOSApp  PublishImage   p "leone"//发布image
./moos-ivp/scripts/GenMOOSApp  ReceiveImage  p "leone"//订阅image

2.代码编写

(1)发布图像
PublishImage.cpp
先初始化

  bool PublishImage::OnStartUp()//完成初始化
 { 
                                                                       
   image_ = cv::Mat(480,640,CV_8UC1);
   bw_image_ = cv::Mat(480,640,CV_8UC1);
   
   if(!vc_.open(0))//打开摄像头
   {
     std::cerr<<"camera open FAIL\n";
   
     return false;
   } 
   else
     std::cerr<<"camera opened OK\n";
   
   
   m_timewarp = GetMOOSTimeWarp();//获取时间戳
   
   RegisterVariables();
   return(true);
 } 

Notify发布topic

bool PublishImage::Iterate()
{
  m_iterations++;
 
  vc_>>capture_frame_;
  if(capture_frame_.data!=NULL)
  {
    cv::cvtColor(capture_frame_, bw_image_, COLOR_BGR2GRAY);
    cv::resize(bw_image_, image_, image_.size(), 0, 0, cv::INTER_NEAREST);
    Notify("Image",(void*)image_.data,image_.size().area(),MOOSLocalTime());//发布topic
  }
  else
  {
    std::cerr<<"no image read from camera..\n";
  }
 
 
  return(true);
}

(2)接收图像

先注册

void ReceiveImage::RegisterVariables()
 {
    m_Comms.Register("Image", 0);//注册图像topic
 }

订阅图像topic

bool ReceiveImage::OnNewMail(MOOSMSG_LIST &NewMail)
{
  MOOSMSG_LIST::iterator p;
   
  //std::cout<<"On New Mail "<<std::endl;
  for(p=NewMail.begin(); p!=NewMail.end(); p++) {
    //CMOOSMsg &msg = *p;
 
    if(p->IsName("Image")){
      std::cout<<"time stamp:"<<p->GetTime()<<std::endl;
 
      memcpy(image_.data, p->GetBinaryData(), p->GetBinaryDataSize());
    }
 
 
 
#if 0 // Keep these around just for template
    string key   = msg.GetKey();
    string comm  = msg.GetCommunity();
    double dval  = msg.GetDouble();
    string sval  = msg.GetString(); 
    string msrc  = msg.GetSource();
    double mtime = msg.GetTime();
    bool   mdbl  = msg.IsDouble();
    bool   mstr  = msg.IsString();
#endif
   }
  
   return(true);
}

这里做一些显示图像的操作

 bool ReceiveImage::Iterate()//
 {
   m_iterations++;
  
   std::cout<<"receive image"<<std::endl;
  
   cv::imshow("display", image_);
   cv::waitKey(10);
  
  
   return(true);
 }

3.运行

test.moos配置文件

 // MOOS file                                                          
  
 ServerHost = localhost
 ServerPort = 9000
 Community  = enstaboat
  
 //------------------------------------------
 // Antler configuration  block
 ProcessConfig = ANTLER
 {
   MSBetweenLaunches = 200
  
   Run = MOOSDB          @ NewConsole = false//类似于ros的roscore
   Run = pPublishImage @ NewConsole = true //发布程序
   Run = pReceiveImage  @ NewConsole = true //接收程序
 }
  
 //------------------------------------------------
 // iCamera config block
  
 ProcessConfig = pPublishImage//以10hz发布 
 {
   AppTick   = 10 
   CommsTick = 10 
 }
  
 ProcessConfig = pReceiveImage//以10hz接收
 {
    AppTick   = 10
    CommsTick = 10 
 }

运行
pAnter test.moos

代码链接地址源码

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值