0
$\begingroup$

Rosanswers logo

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;

 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 = &copy;
// 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.


Originally posted by mateo_7_7 on ROS Answers with karma: 90 on 2013-04-08

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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

Originally posted by Wolf with karma: 7555 on 2014-03-24

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.