How to know/set the frequency at which the images are taken (grabbed)?

asked 2016-07-19 23:10:59 -0500

polar gravatar image

updated 2016-07-19 23:12:16 -0500

I found that the frequency I compute from the time between two successive callbacks and that from using rostopic hz different...

With rostopic hz /pg_15508342/image_raw I get:

verage rate: 149.314 min: 0.004s max: 0.009s std dev: 0.00051s window: 297

However, from this very short code, that computes the frequency at which the callback of the same topic is called,

#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 <time.h>
#include <boost/timer.hpp>
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp>
#include <boost/version.hpp>
#include  "boost/bind.hpp"
#include  "boost/bind.hpp"
#include <iostream>

using namespace std;

boost::posix_time::ptime time1; 
boost::posix_time::time_duration timeloop;
double timeloop_sc;

int image_itr(0);

void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
     //---
      if(image_itr == 0)
        time1 = boost::posix_time::microsec_clock::local_time();

      timeloop =  boost::posix_time::microsec_clock::local_time() - time1;
  time1 = boost::posix_time::microsec_clock::local_time();
  timeloop_sc = 1e-3* (double)timeloop.total_milliseconds();
  cout << "itr " << image_itr++ << "   fps: " << 1.0/timeloop_sc << endl;
 //----
  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("/pg_15508342/image_raw", 1, imageCallback);
  ros::spin();
  cv::destroyWindow("view");
}

I get around 30 fps. Moreover, if I run this same code from another host machine (through ssh), I get even a different value (varying but almost around 1 fps).

Why this difference of values? I should have missed/misunderstood something about ros image topic (?)

edit retag flag offensive close merge delete