ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Is possible to compile a C++ file outside catkin ws?

asked 2021-04-03 05:48:58 -0500

EdoardoSerri gravatar image

Hi,

I'm trying to extract images from a rosbag using C++. in catkin_ws I had,almost, no problem doing that but I have many difficulties doing outside the catkin ws. My idea was to develop a class to extract images from a rosbag file and bind it to a python script outside the catkin ws. This is my code

#include <ros/ros.h>
#include <rosbag/view.h>
#include <gtest/gtest.h>
#include <std_msgs/String.h>
#include <std_msgs/Float32.h>
#include <boost/foreach.hpp>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string>

#define foreach BOOST_FOREACH

using namespace std;

void estrazione ()

{

rosbag::Bag bag;
bag.open("/home/edoardo/BAG/kitti_2011_09_26_drive_0002_synced.bag", rosbag::bagmode::Read);
cv::Mat image;
std::vector<std::string> topics;
topics.push_back(std::string("/kitti/camera_color_left/image_raw"));
ros::Time::init();
rosbag::View view(bag, rosbag::TopicQuery(topics));
std::cout << '1' << std::endl;
ros::Time result;
string inizio = "/home/edoardo/";
string fine = ".png";
foreach(rosbag::MessageInstance const m, view)
{   

    double t = ros::Time::now().toSec();

    std::cout << t << std::endl;
    sensor_msgs::ImageConstPtr s = m.instantiate<sensor_msgs::Image>();
    image = cv_bridge::toCvCopy(s)->image;
    result = m.getTime();
    string all_string = inizio+std::to_string(t)+fine;
    cv::imwrite(all_string,image);
    cv::waitKey(1);
}

bag.close();
}

int main (int argc, char** argv){
ros::init (argc, argv, "bag_it");
estrazione();

return 0;

}

I have tried compiling using this command

g++ prova.cpp -o yoprov -I/opt/ros/noetic/include -L/opt/ros/noetic/lib -lroscpp -lrostime -lrosconsole -lroscpp_serialization -lcv_bridge -lrosbag -lcpp_common `pkg-config --cflags --libs opencv`

But it gives me out an error about undefined rosbag import.

What am I missing? Is there a better way to do so?

PS I'm not using python to do this because I have to extract many frames and python is becoming quite slow

Thank you very much

edit retag flag offensive close merge delete

Comments

Wouldn't it be fine to use catkin_ws to execute the code you are presenting? I felt it would be better if the generated image files could be processed by a python script(?) outside catkin_ws.

miura gravatar image miura  ( 2021-04-03 11:53:26 -0500 )edit

Please post the full text of the error message. Also, I suspect the overhead would be in OpenCV's imwrite(), which will be basically the same for both Python and C++.

KenYN gravatar image KenYN  ( 2021-04-05 03:24:56 -0500 )edit

Sorry to reply this late, but I had something else going on. And sorry to have not posted the error but I had many different ones of the different nature all about rosbag imports not recognized. But if the overhead of imwirte is the same in C++ and Python, there is no point to complicate the code.

EdoardoSerri gravatar image EdoardoSerri  ( 2021-04-05 19:43:34 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-04-03 12:36:16 -0500

Posting your error would be helpful.

In the meantime, if all you care about is extracting images, you can use the image_view package (most likely already installed). Just create a directory for you images, play your rosbag and execute this:

rosrun image_view extract_images _sec_per_frame:=0.01 image:=/image/topic

More information can be found in this tutorial.

edit flag offensive delete link more

Comments

I have tried but, I am trying to do everything without launching the roscore. Furthermore, my image msgs are compressed and having a fast look to the package it looks like I cannot extract compressed images and with timestamp as file name. Thank you anyway!!

EdoardoSerri gravatar image EdoardoSerri  ( 2021-04-05 20:00:22 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-04-03 05:48:58 -0500

Seen: 295 times

Last updated: Apr 03 '21