「ROS OpenCV」读取一张图像然后发布图像话题并在rviz中显示

创建工作空间和功能包

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make
cd ~/catkin_ws/src
catkin_create_pkg pub_sub_image_topic_pkg roscpp rospy std_msgs image_transport cv_bridge sensor_msgs
cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash

包依赖文件package.xml

gedit ~/catkin_ws/src/pub_sub_image_topic_pkg/package.xml

<?xml version="1.0"?>
<package format="2">
  <name>pub_sub_image_topic_pkg</name>
  <version>0.0.0</version>
  <description>The pub_sub_image_topic_pkg package</description>

  <maintainer email="[email protected]">q</maintainer>

  <license>TODO</license>

  <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>sensor_msgs</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>sensor_msgs</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>sensor_msgs</exec_depend>
  <exec_depend>std_msgs</exec_depend>


  <export>

  </export>
</package>

详细注释可执行代码文件

gedit ~/catkin_ws/src/pub_sub_image_topic_pkg/src/pub_image_topic.cpp
// http://wiki.ros.org/image_transport

#include 
#include 
#include 
#include 

#include 


int main(int argc, char** argv)
{
  ros::init(argc, argv, "publisher_image");
  // 声明节点
  ros::NodeHandle nh;
  // image_transport image订阅和发布
  // image_transport ("raw") - The default transport, sending sensor_msgs/Image through ROS
  // 用上面声明的节点句柄初始化it,it和nh的功能基本一样使用it来发布和订阅相消息
  image_transport::ImageTransport it(nh);
  // 第一个参数是话题的名称,第二个是缓冲区的大小(消息队列的长度发布图像消息时消息队列的长度只能是1)
  image_transport::Publisher pub = it.advertise("image", 1);
 
  // 添加微信 xiaoqiuslambiji 可以获取图片 
  cv::Mat image = cv::imread("/xiaoqiuslambiji/image/meinv.png", CV_LOAD_IMAGE_COLOR);
  if(image.empty()){
    printf("image empty\n");
  }
  // 将opencv格式的图像转化为ROS所支持的消息类型从而发布到相应的话题上,bgr8是编码格式
  sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();

  // 设置topic循环发布的频率为20hz
  ros::Rate loop_rate(5);

  while (nh.ok()) {
    pub.publish(msg);
    ros::spinOnce();
    // 通过睡眠度过一个循环中剩下的时间
    loop_rate.sleep();
  }
}

编译文件CMakeLists.txt

gedit ~/catkin_ws/src/pub_sub_image_topic_pkg/CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)

project(pub_sub_image_topic_pkg)

find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  rospy
  sensor_msgs
  std_msgs
)

catkin_package()

include_directories(${catkin_INCLUDE_DIRS})

find_package(OpenCV REQUIRED)

add_executable(pub_image_topic src/pub_image_topic.cpp)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(pub_image_topic ${OpenCV_LIBS} ${catkin_LIBRARIES})

运行命令

$ roscore
catkin_ws$ source devel/setup.bash 
catkin_ws$ rosrun pub_sub_image_topic_pkg pub_image_topic
$ rviz

「ROS OpenCV」读取一张图像然后发布图像话题并在rviz中显示_第1张图片
「ROS OpenCV」读取一张图像然后发布图像话题并在rviz中显示_第2张图片

「ROS OpenCV」读取一张图像然后发布图像话题并在rviz中显示_第3张图片

「ROS OpenCV」读取一张图像然后发布图像话题并在rviz中显示_第4张图片

你可能感兴趣的:(从零开始学习SLAM,image_transport)