Ask Your Question

subscribe to image at regular time intervals

asked 2015-06-28 02:51:55 -0600

Arul Selvam gravatar image

updated 2015-06-28 03:43:37 -0600

gvdhoorn gravatar image

ROS indigo OpenCV

Hi, I am writing an image subscriber and i want to use this to get some 10 images and then stop and repeat this again when I call it again. For this I have written subscriber as follows,

class SubscriberImage
    ros::NodeHandle nh;
    image_transport::Subscriber sub;
    // ...//
    int perform(std::string topic)
      image_transport::ImageTransport it(nh);
      sub = it.subscribe(topic, 1, &SubscriberImage::imageCallback,this, image_transport::TransportHints("theora"));
      while(num < 10)
      return 0;

    void imageCallback(const sensor_msgs::ImageConstPtr& msg)
        **/* Here I am saving 10 images to disk */** 
            if (num < 10 && !cv_bridge::toCvShare(msg, "bgr8")->image.empty())
                cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
                cv::imwrite(name,cv_bridge::toCvShare(msg, "bgr8")->image);
            else if(cv_bridge::toCvShare(msg, "bgr8")->image.empty())
                ROS_INFO_STREAM("Debug: camera image is empty");
            else if(num >= 10)

I am creating an object for this class in another node and whenever I call perform(), a new subscriber object is created and in call back i save 10 images to disk. After that I am calling sub.shutdown(). Everything works as expected but one problem is I am creating a new subscriber object every time and calling shutdown() after saving 10 images. Thus creating a new subscriber object every time takes sometime. I don't this wait time.

Can this be done in a better way? Please guide me in this.


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-06-28 16:42:05 -0600

updated 2015-06-28 16:50:12 -0600

When I see your problem it looks a bit similar to the problem here:

For you, I just would add a image counter for 10, so you do not do the subscribing thing all the time and keep it open. Trade off would be, that of course you will still lose the bandwidth, if that is critical to your application, this might be not the way, otherwise I guess it is. (The images will be constantly transmitted, but you ignore them.)

void callback(...)
   //.... Save your image, processing ... what ever ...
     image_processing_enable=false; // for getting only ten images. 

If it is the bandwith, you might create a node on your own which can republish 10 images for you on one system and transmits them to the target system when you do a call to a rosservice it.

ImageNode -Topic-> Republish-Images  -Topic-> Receiver Node

I would suggest an type std_srvs/Empty with name /get_images.

edit flag offensive delete link more


Hi Christian , in my case bandwidth is not an issue and i am using your first solution. Thanks :)

Arul Selvam gravatar image Arul Selvam  ( 2015-06-29 03:36:33 -0600 )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


Asked: 2015-06-28 02:50:49 -0600

Seen: 413 times

Last updated: Jun 28 '15