ROS 订阅图像节点(1)

博客

http://blog.csdn.net/github_30605157/article/details/50990493

参考ROS原网站

http://wiki.ros.org/image_transport/Tutorials

ROS:两个节点同时具有发布和订阅图像信息的功能

http://blog.csdn.net/ding977921830/article/details/70168877

 

 

 

发射结点 my_publisher

#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_publisher");
  ros::NodeHandle nh;
  image_transport::ImageTransport it(nh);
  image_transport::Publisher pub = it.advertise("camera/image", 1);

  cv::Mat image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
  sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();

  ros::Rate loop_rate(5);
  while (nh.ok()) {
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
  }
}

 

 

接收节点 my_subscriber

接受完要等待一下

#include 
#include 
#include 
#include 

//cv::Mat image;
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
  try
  {
   // image = cv_bridge::toCvShare(msg, "bgr8")->image;
   // cv::imshow("view", image);
  //  cvWaitKey(1);
   cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
   cvWaitKey(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_listener");
  ros::NodeHandle nh;
  cv::namedWindow("view", 0);
  cv::startWindowThread();
  image_transport::ImageTransport it(nh);
  image_transport::Subscriber sub = it.subscribe("camera/image", 1, imageCallback);
  
  
  ros::spin();
  cv::destroyWindow("view");
}

 

camke.list

 

cmake_minimum_required(VERSION 2.8.3)
project(my_image_transport)


find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  OpenCV
)


catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES my_image_transport
  CATKIN_DEPENDS cv_bridge image_transport
#  DEPENDS system_lib
)



include_directories(
# include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)



#build my_publisher and my_subscriber
add_executable(my_publisher src/my_publisher.cpp)
target_link_libraries(my_publisher ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})

add_executable(my_subscriber src/my_subscriber.cpp)
target_link_libraries(my_subscriber ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})

 package。xml

 



  my_image_transport
  0.0.0
  The my_image_transport package

  
  
  
  drone


  
  
  
  TODO


  
  
  
  


  
  
  
  


  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  catkin
  cv_bridge
  image_transport
  cv_bridge
  image_transport
  opencv2
  cv_bridge
  image_transport


  
  
    

  

 

 

 

测试命令

发射
rosrun my_image_ansport my_publisher 2.png 接受 rosrun my_image_transport my_subscriber

 http://www.cvlibs.net/datasets/kitti/eval_odometry.php

转载于:https://www.cnblogs.com/kekeoutlook/p/8031388.html

你可能感兴趣的:(ROS 订阅图像节点(1))