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

1、通过cam/image_raw话题

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

   

 
      
      
        
      
 
将$(find image_view)/test.bag改为你自己的bag文件路径

然后,运行launch文件。

打开一个终端,运行    
roscore

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

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


2、通过cam/image_raw/compressed话题

首先写一个test1.cpp节点

#include 
#include 
#include 


#include 
#include 
#include 
#include "cxcore.hpp"
#include 
#include 
#include 
#include 

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/

你可能感兴趣的:(rosbag文件中提取图像--分别通过cam/image_raw和cam/image_raw/compressed方话题)