Ask Your Question
1

How to save subscribed image in any location

asked 2014-11-13 04:31:58 -0500

KDROS gravatar image

Hello Everyone, I am using OpenCV 2.9 and ROS Hydro. i used this Publisher for publish image and used Subscriber for subscribing images. I am able to show that image but how to save it in computer?? I Used imwrite like this after imshow()

cv::imwrite("//home//keshav//image.jpg",cv_bridge::toCvShare(msg, "bgr8")->image);

p

edit retag flag offensive close merge delete

Comments

Add what happend? Error or something?

Wolf gravatar image Wolf  ( 2014-11-13 07:25:12 -0500 )edit

there is no error but also It is not saving any images. I seached in net somewhere i found that I should use latest opencv for that..but i dont want to install that one.

KDROS gravatar image KDROS  ( 2014-11-13 23:35:03 -0500 )edit

Does the folder "//home//keshav//" exist? Usually it should be "/home/keshav/image.jpg" but that should not matter... Does imwrite return true? It should return true on success...

Wolf gravatar image Wolf  ( 2014-11-14 01:06:23 -0500 )edit

hoe to check that one..

KDROS gravatar image KDROS  ( 2014-11-16 23:34:17 -0500 )edit

if ( cv::imwrite("//home//keshav//image.jpg",cv_bridge::toCvShare(msg, "bgr8")->image) ) { ROS_INFO( "Returned true" ); } else { ROS_INFO( "Returned false" ); }

Wolf gravatar image Wolf  ( 2014-11-17 05:49:03 -0500 )edit

try { cv::imshow("view", cv_bridge::toCvShare(msg,"bgr8")->image); cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
if ( cv::imwrite("//home//keshav//image.jpg",cv_bridge::toCvShare(msg, "bgr8")->image) ) { ROS_INFO( "Returned true2" ); } else { ROS_INFO( "Returned false2" ); } }

KDROS gravatar image KDROS  ( 2014-11-17 06:30:01 -0500 )edit

It is neither returning true nor false.. :-(

KDROS gravatar image KDROS  ( 2014-11-17 06:30:27 -0500 )edit

I have to create ROS nodes, one node can capture images using webcam & 2nd node can publish and third node can subscribe. first am unable to capture also, I can publish and subscribe but imwrite is not working. Mr. Wolf plz share some solid links or code that I can easily execute for this purpose

KDROS gravatar image KDROS  ( 2014-11-17 06:33:31 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-11-18 15:46:26 -0500

Wolf gravatar image

updated 2014-11-18 15:47:06 -0500

If you just want to save an image, just have a look at the cv_bridge tutorial

http://wiki.ros.org/cv_bridge/Tutoria...

And add a cv::imwrite after the image was converted to cv::Mat:

#include <ros/ros.h>
#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 <sstream>                                                 // Added this

static const std::string OPENCV_WINDOW = "Image window";

class ImageConverter
{
  ros::NodeHandle nh_;
  image_transport::ImageTransport it_;
  image_transport::Subscriber image_sub_;
  image_transport::Publisher image_pub_;

public:
  ImageConverter()
    : it_(nh_)
  {
    // Subscrive to input video feed and publish output video feed
    image_sub_ = it_.subscribe("/camera/image_raw", 1, 
      &ImageConverter::imageCb, this);
    image_pub_ = it_.advertise("/image_converter/output_video", 1);

    cv::namedWindow(OPENCV_WINDOW);
  }

  ~ImageConverter()
  {
    cv::destroyWindow(OPENCV_WINDOW);
  }

  void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
      cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
      ROS_ERROR("cv_bridge exception: %s", e.what());
      return;
    }

    // Draw an example circle on the video stream
    if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
      cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));

    // Update GUI Window
    cv::imshow(OPENCV_WINDOW, cv_ptr->image);
    cv::waitKey(3);

    static int image_count = 0;                                // added this
    std::stringstream sstream;                               // added this
    sstream << "my_image" << image_count << ".png" ;                  // added this
    ROS_ASSERT( cv::imwrite( sstream.str(),  cv_ptr->image ) );      // added this
    image_count++;                                      // added this


    // Output modified video stream
    image_pub_.publish(cv_ptr->toImageMsg());
  }
};

int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_converter");
  ImageConverter ic;
  ros::spin();
  return 0;
}

This should save you the images with ascending counter locally in the folder where you start the node via rosrun .....

edit flag offensive delete link more

Comments

Thanq Wolf Sir..I think It would work, I will try it. One more question if you help. I was Using usb_cam driver for capturing image from usb camera. It auto publish (as I understand) when we press right click for saving images? How should I write subscriber for that.

KDROS gravatar image KDROS  ( 2014-11-18 22:42:42 -0500 )edit

If I use above code by changing TOPIC name. will it work?

KDROS gravatar image KDROS  ( 2014-11-18 23:10:12 -0500 )edit

jep, that should work...

Wolf gravatar image Wolf  ( 2014-11-19 04:39:45 -0500 )edit

Sorry, Its not working. I saw the usb_cam files, It is publishing image and camera info, what changes are required in above code.

KDROS gravatar image KDROS  ( 2014-11-19 23:02:17 -0500 )edit

What is not working? you need to renap the topic of the usb_cam to /camera/image_raw . Is something published to /image_converter/output_video?

Wolf gravatar image Wolf  ( 2014-11-20 01:40:45 -0500 )edit

One silly doubt I have.. When usb_cam will publish the image? As soon as I will save(by right clicking) or after some time frequently

KDROS gravatar image KDROS  ( 2014-11-20 04:22:19 -0500 )edit

waiting for reply

KDROS gravatar image KDROS  ( 2014-11-23 23:33:04 -0500 )edit

I am not familiar with the usb_cam driver. Typically cam drivers publish at constant frequency. You can check messages are comming using comand "rostopic echo --noarr /your_image_topic" (--noarr skips array data so you can actually see something)

Wolf gravatar image Wolf  ( 2014-11-24 01:51:00 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-11-13 04:31:58 -0500

Seen: 5,114 times

Last updated: Nov 18 '14