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

Revision history [back]

click to hide/show revision 1
initial version

Hi I found a trick to make this work. The below code will save two frames into two different variables.

bool frame1_save = false;
bool frame2_save = false;

  void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {
    static int frame_count =1;

    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;
    }

    //Saving Frame 1
    if(frame_count==1)
    {
    frame1 = cv_ptr->image;
    frame1_save=true;
    }

   //Saving Frame 2
   else if(frame_count==2)
    {
    frame2 = cv_ptr->image;
    frame2_save=true;
    }

frame_count =( frame_count == 1) ? 2: 1 ;

if(frame1_save && frame2_save)
{
// Do further processing as you wish
}