rosbag文件中提取图像--分别通过cam/image_raw和cam/image_raw/compressed方话题

来源:互联网 发布:拍摄淘宝静物打灯图解 编辑:程序博客网 时间:2024/06/03 18:19

1、通过cam/image_raw话题

     创建launch文件,如下:
     export.launch

   

 <launch>      <node pkg="rosbag" type="play" name="rosbag" args="-d 2 $(find image_view)/test.bag"/>      <node name="extract" pkg="image_view" type="extract_images" respawn="false" output="screen" cwd="ROS_HOME">        <remap from="image" to="/camera/image_raw"/>      </node> </launch>
将$(find image_view)/test.bag改为你自己的bag文件路径

然后,运行launch文件。

打开一个终端,运行   
roscore打开一个终端,cd到export.launch所在路径下,运行roslaunch export.launch
所提取的图片在~/.ros路径下,将其移到你的目标文件中,如:

cd ~mkdir Examplemv ~/.ros/*.jpg ~/Example


2、通过cam/image_raw/compressed话题

首先写一个test1.cpp节点

#include <ros/ros.h>#include <opencv2/highgui/highgui.hpp>#include <cv_bridge/cv_bridge.h>#include <cv_bridge/cv_bridge.h>#include <cv.h>#include <highgui.h>#include "cxcore.hpp"#include <stdio.h>#include <stdlib.h>#include <string>#include <sstream>using namespace std; long temptime=0; char base_name[256]; string str;void imageCallback(const sensor_msgs::CompressedImageConstPtr& msg){  try  {    cv::Mat image = cv::imdecode(cv::Mat(msg->data),1);//convert compressed image data to cv::Mat    //sprintf(base_name,"%4ld.jpg",temptime);    sprintf(base_name,"/home/wf/bagfiles/camera1/%4ld.jpg",temptime);    //str    //str="/home/wf/bagfiles/camera1/"+base_name;    cv::imwrite(base_name,image);    cv::imshow("view", image);    temptime++;    cv::waitKey(10);  }  catch (cv_bridge::Exception& e)  {    ROS_ERROR("Could not convert to image!");  }}int main(int argc, char **argv){  ros::init(argc, argv, "image_listener");  ros::NodeHandle nh;  cv::namedWindow("view");  cv::startWindowThread();  ros::Subscriber sub = nh.subscribe("/camera1/image_raw/compressed", 1, imageCallback);  ros::spin();  cv::destroyWindow("view");}

然后在cmakelists.txt中加入下面的3行:

add_executable(test1 src/test1.cpp)target_link_libraries(test1 ${catkin_LIBRARIES} ${OPENCV_LIBS})add_dependencies(test1 beginner_tutorials_generate_messages_cpp)
之后,把上述代码当做一个节点运行就可以了
参考:https://answers.ros.org/question/230476/how-to-subscribe-to-sensor_msgscompressedimage-without-the-raw-image/

原创粉丝点击