Error in using cv_bridge: communication ROS - OpenCV [closed]

asked 2013-04-08 03:19:12 -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 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;

    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;


   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...

ros::Rate loop_rate(5);  

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

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


     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 reopen merge delete

Closed for the following reason duplicate question by Dan Lazewatsky
close date 2013-04-08 03:55:56