How to subscribe to a camera and save image?

asked 2018-02-26 18:18:36 -0600

sdcguy gravatar image

updated 2018-02-27 01:12:38 -0600

jayess gravatar image

Hi, So I am working with a car simulator and the car has simulated cameras attached to it. Now I am able to get a live video feed from the camera using rviz but I want to save the images that I am getting from the camera to be able to train my model using machine learning. I am not able to figure out how to save those images in a folder. I have tried many things that I found on the forums but nothing has really worked. I would really appreciate the help.

./testimage.cpp: line 6: syntax error near unexpected token `('
./testimage.cpp: line 6: `void imageCallback(const sensor_msgs::ImageConstPtr& msg)'

this was the error I got.

And this is what my code looks like:

#include <ros/ros.h>
 #include <image_transport/image_transport.h>
 #include <opencv2/highgui/highgui.hpp>
 #include <cv_bridge/cv_bridge.h>

void imageCallback(const sensor_msgs::ImageConstPtr& msg)
     cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
   catch (cv_bridge::Exception& e)
     ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());

 int main(int argc, char **argv)
   ros::init(argc, argv, "image_listener");
   ros::NodeHandle nh;
   image_transport::ImageTransport it(nh);
   image_transport::Subscriber sub = it.subscribe("/truevision/camera/front_right", 1, imageCallback);
edit retag flag offensive close merge delete


I have tried many things that I found on the forums but nothing has really worked

What specifically have you tried and what was the result?

jayess gravatar imagejayess ( 2018-02-26 18:26:13 -0600 )edit

./testimage.cpp ./testimage.cpp: line 6: syntax error near unexpected token (' ./testimage.cpp: line 6:void imageCallback(const sensor_msgs::ImageConstPtr& msg)'

I tried making an image subscriber following the tutorials on the ROS website. And then when I ran the code this is the error i got

sdcguy gravatar imagesdcguy ( 2018-02-27 00:33:28 -0600 )edit

Don't know what this error is because the code is the same as in the tutorial. Could this be due to the dependencies or something? because I am using a workspace that I created using the tutorial on workspaces. I am not too sure. I am pretty new to ROS.

sdcguy gravatar imagesdcguy ( 2018-02-27 00:36:24 -0600 )edit this is the tutorial I followed.

sdcguy gravatar imagesdcguy ( 2018-02-27 00:36:50 -0600 )edit

Please update your question with this information (including code examples and errors/terminal output) using the preformatted text or 101010 button for code/terminal output. Comments aren't the best place to give new information.

jayess gravatar imagejayess ( 2018-02-27 00:39:39 -0600 )edit

Just added the information.

sdcguy gravatar imagesdcguy ( 2018-02-27 00:54:42 -0600 )edit

@sdcguy: could it be that you're trying to execute the cpp file directly (ie: as ./testimage.cpp in a terminal)? This is C++ and that needs to compiled before you can run the executable.

gvdhoorn gravatar imagegvdhoorn ( 2018-02-27 03:00:48 -0600 )edit

If the only thing you're trying to do is to save sensor_msgs/Image to disk (so don't want to process them in your node), then take a look at image_saver in the image_view package.

gvdhoorn gravatar imagegvdhoorn ( 2018-02-27 03:02:29 -0600 )edit