ROS&OpenCV进行摄像头数据的采集与订阅发布

依赖
opencv库
ros-indigo-image-transport

首先用openCV来捕获视频流,然后转换成ROS图像的传输格式

#include 
#include 
#include 
#include 
#include 

int main( int argc, char **argv )
{
  ros::init( argc, argv, "pub_cam_node" );
  ros::NodeHandle n;

  // Open camera with CAMERA_INDEX (webcam is typically #0).
  const int CAMERA_INDEX = 0;
  cv::VideoCapture capture( CAMERA_INDEX ); //摄像头视频的读操作
  if( not capture.isOpened() )
  {
    ROS_ERROR_STREAM(
      "Failed to open camera with index " << CAMERA_INDEX << "!"
    );
    ros::shutdown();
  }
  //1 捕获视频
  
  //2 创建ROS中图像的发布者
  image_transport::ImageTransport it( n ); 
  image_transport::Publisher pub_image = it.advertise( "camera", 1 );

  
  //cv_bridge功能包提供了ROS图像和OpenCV图像转换的接口,建立了一座桥梁
  cv_bridge::CvImagePtr frame = boost::make_shared< cv_bridge::CvImage >();
  frame->encoding = sensor_msgs::image_encodings::BGR8;

  while( ros::ok() ) {
    capture >> frame->image; //流的转换

    if( frame->image.empty() )
    {
      ROS_ERROR_STREAM( "Failed to capture frame!" );
      ros::shutdown();
    }
	//打成ROS数据包
    frame->header.stamp = ros::Time::now();
    pub_image.publish( frame->toImageMsg() );

    cv::waitKey( 3 );//opencv刷新图像 3ms
    ros::spinOnce();
  }

  capture.release();  //释放流
  return EXIT_SUCCESS;
}

OpenCV可以捕获摄像头数据,或者从文件中读取数据
cv::VideoCapture capture( const string& filename, // 输入文件名 );
cv::VideoCapture capture( int device // 视频捕捉设备 id ); 设备号从0开始往后排


再写一个节点来订阅这个ROS发布的图像话题

#include   
#include   
#include   
#include   
  
void imageCallback(const sensor_msgs::ImageConstPtr& msg)  
{  
//sensor_msgs::Image ROS中image传递的消息形式
  try  
  {  
    cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);  
   // cv::WaitKey(3);  
  }  
  catch (cv_bridge::Exception& e)  
  {  
    ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());  
  }  
}  
  
int main(int argc, char **argv)  
{  
  ros::init(argc, argv, "image_sub_node");  
  ros::NodeHandle nh;  
  cv::namedWindow("view");  
  cv::startWindowThread();  
  image_transport::ImageTransport it(nh);  
  image_transport::Subscriber sub = it.subscribe("camera", 1, imageCallback);  
  ros::spin();  
  cv::destroyWindow("view");  //窗口
}  

还可以通过 web_video_server 把image通过网络进行传输


#编译出错 cannot find -lopencv_dep_cudart
catkin_make 本质上还是make文件
Cmakelists.txt中做相应的修改,

在cmake编译时 加参数 -D CUDA_USE_STATIC_CUDA_RUNTIME=OFF
或者直接在Cmakelist.txt文件中修改,
CMakeLists.txt中find_package(OpenCV REQUIRED)之前加上:set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)

你可能感兴趣的:(ROS机器人系统)