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

Query a frame from a video passed as a message by ROS to OpenCV

asked 2013-02-27 03:46:54 -0600

mateo_7_7 gravatar image

updated 2014-01-28 17:15:27 -0600

ngrennan gravatar image

hi everybody, I'm trying to extract a frame from a video passed by a ROS node to OpenCV. Obviously I made the necessary conversion (via cv_bridge) exactly as suggested in ros.org/wiki/cv_bridge/Tutorials/UsingCvBridgeToConvertBetweenROSImagesAndOpenCVImages .

namespace enc = sensor_msgs::image_encodings;
static const char 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_)
{
image_pub_ = it_.advertise("out", 1);
image_sub_ = it_.subscribe("in", 1, &ImageConverter::imageCb, this);

cv::namedWindow(WINDOW);
} 

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

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

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

cv::imshow(WINDOW, cv_ptr->image);
cv::waitKey(3);

image_pub_.publish(cv_ptr->toImageMsg());
}
};

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

So I don't have to do a cvCapture operation and now the video data (cv_ptr->image) are in the form cv::Mat . if I write the following code:

IplImage* frame_act;
frame_act = cvQueryFrame(cv_ptr->image);

i receive this error:

error: cannot convert ‘cv::Mat’ to ‘CvCapture*’ for argument ‘1’ to ‘IplImage* cvQueryFrame(CvCapture*)’

have you same ideas in order to solve this small problem? thank you very much for your help!

edit retag flag offensive close merge delete

Comments

I am facing the same problem. Is there a solution? i just need to save the first two frames from a ROS topic, saved into a cv mat variable to perform object tracking.

ramanan1991 gravatar image ramanan1991  ( 2016-10-26 09:47:41 -0600 )edit

2 Answers

Sort by » oldest newest most voted
2

answered 2013-02-27 05:23:16 -0600

Your subscriber will receive the video frame-by-frame, so the data referenced by cv_ptr is already a single frame. No need for cvQueryFrame.

edit flag offensive delete link more

Comments

oh..ops, ok

mateo_7_7 gravatar image mateo_7_7  ( 2013-02-27 23:58:13 -0600 )edit

thanks!! but..how can i acquire consecutive frames in separate variables? ex. first_frame=... second_frame=....

mateo_7_7 gravatar image mateo_7_7  ( 2013-02-28 00:01:47 -0600 )edit

A global std::vector<cv::Mat> would probably work.

Dan Lazewatsky gravatar image Dan Lazewatsky  ( 2013-02-28 05:22:41 -0600 )edit

thanks again, but please...i'm not so expert about c++. could you explain tour last suggestion?

mateo_7_7 gravatar image mateo_7_7  ( 2013-02-28 05:34:08 -0600 )edit
0

answered 2016-10-26 10:25:37 -0600

ramanan1991 gravatar image

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
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-02-27 03:46:54 -0600

Seen: 1,242 times

Last updated: Oct 26 '16