「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_video2image_topic.cpp
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace cv;
using namespace std;

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);


	VideoCapture capture;
	//连接视频
    // 添加微信 xiaoqiuslambiji 可以获取视频
	capture.open("~/catkin_ws/src/pub_image_topic_pkg/video/Mobile_Robot.mp4");
	if (!capture.isOpened()) {
		printf("could not load video data...\n");
		return -1;
	}


	int frames = capture.get(CAP_PROP_FRAME_COUNT);//获取视频针数目(一帧就是一张图片)
	double fps = capture.get(CAP_PROP_FPS);//获取每针视频的频率
	// 获取帧的视频宽度,视频高度
	Size size = Size(capture.get(CAP_PROP_FRAME_WIDTH), capture.get(CAP_PROP_FRAME_HEIGHT));
	cout << frames << endl;
	cout << fps << endl;
	cout << size << endl;
	// 创建视频中每张图片对象
	Mat frame;


    sensor_msgs::ImagePtr msg;

    ros::Rate loop_rate(10);//以10ms间隔发送图片
    while (nh.ok()) {
        capture >> frame;  

        if (!frame.empty()) {  
            msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();  
            pub.publish(msg);  
        }
        ROS_INFO("runnning!");
        ros::spinOnce();  
        loop_rate.sleep();//与ros::Rate loop_rate相对应,休息10ms
    }
}

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

运行命令

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

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

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