Is possible to compile a C++ file outside catkin ws?
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
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.
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++.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.