How to subscribe to a camera and save image?

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

sdcguy gravatar image

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

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)
 {
   try
   {
     cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
     cv::waitKey(30);
   }
   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;
   cv::namedWindow("view");
   cv::startWindowThread();
   image_transport::ImageTransport it(nh);
   image_transport::Subscriber sub = it.subscribe("/truevision/camera/front_right", 1, imageCallback);
   ros::spin();
   cv::destroyWindow("view");
 }
edit retag flag offensive close merge delete

Comments

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 image jayess  ( 2018-02-26 18:26:13 -0500 )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 image sdcguy  ( 2018-02-27 00:33:28 -0500 )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 image sdcguy  ( 2018-02-27 00:36:24 -0500 )edit

http://wiki.ros.org/image_transport/T... this is the tutorial I followed.

sdcguy gravatar image sdcguy  ( 2018-02-27 00:36:50 -0500 )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 image jayess  ( 2018-02-27 00:39:39 -0500 )edit

Just added the information.

sdcguy gravatar image sdcguy  ( 2018-02-27 00:54:42 -0500 )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 image gvdhoorn  ( 2018-02-27 03:00:48 -0500 )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 image gvdhoorn  ( 2018-02-27 03:02:29 -0500 )edit

I did compile it. So I'm not sure what the problem is.

sdcguy gravatar image sdcguy  ( 2018-02-27 12:14:32 -0500 )edit