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

Save an image during the subscription

asked 2014-07-24 08:13:28 -0500

ROSkinect gravatar image

Using that simple program how can I save an image during the subscription: like when I press a key (ctrl+s for example or s) I get an image *.jpeg file (or other extension doesn't matter) ?

void imageCallback(const sensor_msgs::ImageConstPtr& msg)

        cv_bridge::CvImagePtr cv_ptr;
            cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);

        catch (cv_bridge::Exception& e)
            ROS_ERROR("cv_bridge exception: %s", e.what());

        cv::imshow("OpenCV viewer uEye RGB", cv_ptr->image);

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-07-24 08:52:05 -0500

Wolf gravatar image

some Utilities:

#include <sstream>
#include <string>

#define SPACE_KEY (32)

    template<typename T> 
    inline std::string toString( const T& ao_Obj )
      std::stringstream lo_stream;

      lo_stream << ao_Obj;

      return lo_stream.str();

Then later in your code

  cv::imshow("OpenCV viewer uEye RGB", cv_ptr->image);    
  int key = cv::waitKey( 50 );

    if ( key == SPACE_KEY )
       static int count = 0;
       ROS_ASSERT( cv::imwrite( std::string( "file" )  + toString( count++ )  + std::string( ".png" ), cv_ptr->image ) );

Note that the cv window has to be the active one when the space key is pressed in order for saving the image. Note that your space press has to hit the 50 ms waiting duration, maybe a longer value is better here, but it will slow down your node....

edit flag offensive delete link more


where the image file.png will be saved ..? and when I look for it I can't find it !

ROSkinect gravatar image ROSkinect  ( 2014-07-24 09:08:21 -0500 )edit

in the local folder where you started your node ( if rosrun used ). If you use roslaunch is used, you have no way of knowing;) I. e. if using roslaunch you would need to give it a full path in some way. Saving images like this with ros would be moreless only for debugging purposes...

Wolf gravatar image Wolf  ( 2014-07-24 10:25:54 -0500 )edit

Question Tools

1 follower


Asked: 2014-07-24 08:13:28 -0500

Seen: 1,642 times

Last updated: Jul 24 '14