「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_camera2image_topic.cpp
#include 
#include 
#include 
#include 
#include 
 
int main(int argc, char** argv)
{
    // 通过终端传参方式更改相机ID
    if(argv[1] == NULL)   
    {  
        ROS_INFO("argv[1]=NULL\n");  
        return 1;  
    }  
    ros::init(argc, argv, "image_publisher");
    ros::NodeHandle nh;
    image_transport::ImageTransport it(nh);
    image_transport::Publisher pub = it.advertise("camera/image", 1);

    // 通过终端传参方式更改相机ID
    std::istringstream video_sourceCmd(argv[1]); 
    int video_source; 

    if(!(video_sourceCmd >> video_source))   
    {  
        ROS_INFO("video_sourceCmd is %d\n",video_source);  
        return 1;  
    }  
    
    
    // video_source 代表的数字为要索引的摄像头
    cv::VideoCapture cap(video_source);
    if(!cap.isOpened()) 
    {
        ROS_INFO("can not opencv video device\n");
        return 1;
    }
    cv::Mat frame;
    sensor_msgs::ImagePtr msg;
    // 以10ms间隔发送图片
    ros::Rate loop_rate(10);
    while (nh.ok()) 
    {
        // 从摄像头抓取一帧图像
        cap >> frame;
        if (!frame.empty()) {  
            msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();  
            pub.publish(msg);  
        }
        ROS_INFO("runnning!");
        ros::spinOnce();  
        // 与ros::Rate loop_rate相对应,休息10ms
        loop_rate.sleep();
    }
    
    ros::spin();
}

编译文件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_camera2image_topic src/pub_camera2image_topic.cpp)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(pub_camera2image_topic ${OpenCV_LIBS} ${catkin_LIBRARIES})

运行效果

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

「ROS OpenCV」摄像头实时发布图像话题并在rviz中显示_第1张图片

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