Error in using cv_bridge: communication ROS - OpenCV
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.