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

subscribe to image at regular time intervals

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

Arul Selvam gravatar image

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

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
{
private:
    ros::NodeHandle nh;
    image_transport::Subscriber sub;
    // ...//
public:
    int perform(std::string topic)
    {
      cv::namedWindow("view");
      cv::startWindowThread();
      image_transport::ImageTransport it(nh);
      sub = it.subscribe(topic, 1, &SubscriberImage::imageCallback,this, image_transport::TransportHints("theora"));
      while(num < 10)
          ros::spinOnce();
      cv::destroyWindow("view");
      return 0;
    }

    void imageCallback(const sensor_msgs::ImageConstPtr& msg)
    {
        **/* Here I am saving 10 images to disk */** 
        try
        {
            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)
            {
                sub.shutdown();
            } 
        }
    }
};

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.

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

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

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

When I see your problem it looks a bit similar to the problem here: http://answers.ros.org/question/21234...

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(...)
{
  if(image_processing_enable) 
  {
   //.... Save your image, processing ... what ever ...
   image_no++;
   if(image_no==10)
   {
     image_processing_enable=false; // for getting only ten images. 
     image_no=0;
   }
  }
}

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
                       ^----------rosservice-----|

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

edit flag offensive delete link more

Comments

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 -0500 )edit

Question Tools

1 follower

Stats

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

Seen: 651 times

Last updated: Jun 28 '15