Ask Your Question
0

Error in using cv_bridge: communication ROS - OpenCV

asked 2013-04-08 03:17:09 -0500

mateo_7_7 gravatar image

I'm using OpenCV in order to elaborate frames of a video stream acquired from a simulator (vrep) through ROS Topic: as suggested by the ROS guide i'm doing that via http://www.ros.org/wiki/cv_bridge/Tutorials/UsingCvBridgeToConvertBetweenROSImagesAndOpenCVImages (cv_bridge). In that example the operation on the image are done inside the callback function, and that's in turn inside a class. i'd like to process my images inside the main function and without using a class. This is my code but actually i have running errors because the pointer to the image acquired is NULL.

#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 <vector>
#include <tk_draft_msgs/proc_data.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/legacy/legacy.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "opencv2/core/core.hpp"
#include "opencv2/nonfree/features2d.hpp"

using namespace cv;
using namespace std;
namespace enc = sensor_msgs::image_encodings;


  Mat previousImage;
 static Mat currentImage;


 int mm=5;
 namespace enc = sensor_msgs::image_encodings;


 void newImageTrigger(const sensor_msgs::Image::ConstPtr& msg)
 {
 cv_bridge::CvImagePtr cv_ptr;


     previousImage = currentImage;

 try
 {
    cv_ptr =  cv_bridge::toCvCopy(msg, enc::BGR8);
 }
 catch (cv_bridge::Exception& e)
 {
     ROS_ERROR("cv_bridge exception: %s", e.what());
 }

 currentImage = cv_ptr->image;

 return;
   }


   int main( int argc, char** argv )
 {
ros::init(argc, argv, "communications_mio");

ros::NodeHandle n;
ros::Publisher data_pub_ = n.advertise<tk_draft_msgs::proc_data>...
    ("/0data_im",   1);
ros::Subscriber vrepVideoSubscriber = n.subscribe...
    ("/vrep/visionSensorData",1,newImageTrigger);


ros::Rate loop_rate(5);  

IplImage copy = currentImage;
IplImage* frame = ©
// computation......ERROR
while (ros::ok())
 {

     //other computations.......Error

    ros::spinOnce();
        loop_rate.sleep();
    }

     return 0;
     }

help! ps. "currentImage" is static only because i need to make a computation o a frame with respect to the follower.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2014-03-24 08:03:20 -0500

Wolf gravatar image

updated 2014-03-24 08:04:53 -0500

Your

IplImage copy = currentImage;

cannot work because at that point the image cannot be filled. Your callback is called in ros::spinOnce(), so before your first call of spinOnce() your currentImage is 100% empty.Afterwards it might be empty.

Check in your while loop and process only if filled:

ros::Rate loop_rate(5);  

while (ros::ok())
 {
        ros::spinOnce();

       if ( !currentImage.empty() )
       {
           IplImage copy = currentImage;

          //other computations.
        }

        loop_rate.sleep();
 }
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-04-08 03:17:09 -0500

Seen: 249 times

Last updated: Mar 24 '14