ROS利用节点发布并订阅图片

写在前面:近期打算做一下视觉算法部署,需用到ROS知识传输视频,以此作为记录
首先需要电脑有ROS和opencv

一、图片发布

1、建立新的工作空间

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
# 初始化文件
catkin_init_workspace
#    编译工作空间
cd ~/catkin_ws/
catkin_make
#    设置环境变量,避免每次开终端都要source
 echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
 source ~/.bashrc

2、创建新的功能包

#   创建my_image_transport功能包
cd catkin_ws/
cd src/
catkin_create_pkg my_image_transport image_transport cv_bridge
#    重新编译
cd ..
catkin_make

3、发布图片

首先进入my_image_transport功能包,创建src文件夹新建my_publisher.cpp文件,并将下述内容复制进去即可

#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();
  }
}

即可在camera/image话题上发布图像

二、图片订阅

同样在src文件夹新建my_subscriber.cpp文件,将下述内容复制进去即可

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

即可

三、配置相关文件

1、在CMakeList.txt添加如下代码

find_package(OpenCV)
include_directories(include ${OpenCV_INCLUDE_DIRS})
message(${OpenCV_INCLUDE_DIRS})
#build my_publisher and my_subscriber
# 注意一定要将${OpenCV_LIBS},即opencv加入链接
add_executable(my_publisher src/my_publisher.cpp)
target_link_libraries(my_publisher ${catkin_LIBRARIES} ${OpenCV_LIBS})
 
add_executable(my_subscriber src/my_subscriber.cpp)
target_link_libraries(my_subscriber ${catkin_LIBRARIES} ${OpenCV_LIBS})

2、在package.xml添加如下代码

 
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>cv_bridge</build_depend>
  <build_depend>image_transport</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_export_depend>cv_bridge</build_export_depend>
  <build_export_depend>image_transport</build_export_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <exec_depend>cv_bridge</exec_depend>
  <exec_depend>image_transport</exec_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>std_msgs</exec_depend>

3、重新编译

cd ~/catkin_ws
catkin_make -DCATKIN_WHITHELIST_PACKAGES="my_image_transport"

出现下图即编译完成
在这里插入图片描述
打开roscore,运行rosrun my_image_transport my_publisher path/Picturespath/Pictures为图片路径,在另一个终端运行rosrun my_image_transport my_subscriber即可。

你可能感兴趣的:(linux)