ros接收图像并通过opencv显示,以kinect为例

 通过imagetransport传输信息,而不是通常的topic传递方法。

image_transport应该始终用于订阅和发布图像。它为以低带宽压缩格式传输图像提供了支持。

#include 
#include 
#include 
#include 
Mat colorImg;
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
  try
  {
    cv_bridge::CvImagePtr cv_ptr;
    cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    colorImg = cv_ptr->image;
    imshow("view",colorImg);
    cv::waitKey(1);
  }
  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_reciver");
  ros::NodeHandle nh;
  cv::namedWindow("view",CV_WINDOW_NORMAL);
  cv::startWindowThread();
  image_transport::ImageTransport it(nh);//使用ImageTransport方法创建图像发布者和订阅者,就像我们使用NodeHandle方法创建一般ROS发布者和订阅者一样
  image_transport::Subscriber sub = it.subscribe("/kinect2/hd/image_color", 1, imageCallback);
  ros::spin();
  cv::destroyWindow("view");
}

参考:http://wiki.ros.org/image_transport

 

你可能感兴趣的:(#,ROS的那些坑,imageTransport,图像接收,ros,kinect,opencv)