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

Error CvImagePtr cv_ptr;

asked 2014-07-31 07:55:28 -0500

ROSkinect gravatar image

updated 2014-08-01 03:58:17 -0500

I want to blend two images the rgb and the depth to look for the shift between. This is my program, I make it very easy to understand just using the basic of ros programming

    #include <ros/ros.h>
    #include <image_transport/image_transport.h>
    #include <opencv/cv.h>
    #include <opencv/highgui.h>
    #include <cv_bridge/cv_bridge.h>
    #include <sensor_msgs/image_encodings.h>

    using namespace cv;

    cv_bridge::CvImagePtr cv_ptr_rgb;

    void imageCallbackrgb(const sensor_msgs::ImageConstPtr& msg)
    {

        //cv_bridge::CvImagePtr cv_ptr_rgb;

        try
        {
            cv_ptr_rgb = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO8);
        }

        catch (cv_bridge::Exception& e)
        {
            ROS_ERROR("cv_bridge exception: %s", e.what());
            return;
        }


        //cv::imshow("OpenCV viewer Kinect RGB", cv_ptr_rgb->image);

        cvWaitKey(3);

    }


    void imageCallbackdepth(const sensor_msgs::ImageConstPtr& msg)
    {   
        // convert message from ROS to openCV
        cv_bridge::CvImagePtr cv_ptr_depth;

        try
        {
            cv_ptr_depth = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_16UC1);

        }

        catch (cv_bridge::Exception& e)
        {
            ROS_ERROR("cv_bridge exception: %s", e.what());
            return;
        }


        //conversion 16UC to 8UC
/*
Conversion Sutffs
*/
    /*
    //*******************************blending two images*************************************
        double alpha = 0.5; double beta; 
        beta = ( 1.0 - alpha );
        Mat dst;
        addWeighted( cv_ptr_rgb->image, alpha, image_8CU, 0.5, 0.5, dst);
        cv::imshow( "Linear Blend", dst );
    //**************************************************************************************
    */

        cv::imshow("OpenCV viewer Kinect RGB", cv_ptr_rgb->image);
        //cv::imshow("OpenCV viewer Kinect depth", image8U);
        cvWaitKey(3);

    }

    int main(int argc, char **argv)

    {
        ros::init(argc, argv, "listenerKinectuEye");
        ros::NodeHandle nh;

        image_transport::ImageTransport it(nh);

        image_transport::Subscriber subkirgb = it.subscribe("/camera/rgb/image_color", 1, imageCallbackrgb);
        image_transport::Subscriber subkidepth = it.subscribe("/camera/depth_registered/image_raw", 1, imageCallbackdepth);

        ROS_INFO("subscribed to Kinect and uEye topics");

        ros::spin();
    }

And this is the error that I get (sub_node is my executable):

sub_node: /usr/include/boost/smart_ptr/shared_ptr.hpp:418: T* boost::shared_ptr<t>::operator->() const [with T = cv_bridge::CvImage]: Assertion `px != 0' failed. Aborted (core dumped)

the error comes from the pointer cv_ptr_rgb that I use in imageCallbackdepth because even when I try to

cv::imshow("OpenCV viewer Kinect RGB", cv_ptr_rgb->image);

before doing any blending I get the same error.

How to solve this ? (there is another way to do it?)

edit retag flag offensive close merge delete

Comments

Is this really a ROS problem?

2ROS0 gravatar image 2ROS0  ( 2014-07-31 21:24:55 -0500 )edit

I don't know you have the same problem ? I found a basic solution you can use it

ROSkinect gravatar image ROSkinect  ( 2014-08-01 04:34:09 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-08-01 00:57:05 -0500

BennyRe gravatar image

updated 2014-08-01 05:02:48 -0500

You have a race condition with cv_ptr_rgb.

You set this global variable in imageCallbackrgb and use it in imageCallbackdepth.

If the latter callback is called first you get your null-pointer error.

Check if cv_ptr_rgb has been set and only then use it.

You could do it like this:

if (cv_ptr_rgb != NULL) addWeighted( cv_ptr_rgb->image, alpha, image8U, 0.5, 0.5, dst);
edit flag offensive delete link more

Comments

I don't really understand what should I do exactly ?

ROSkinect gravatar image ROSkinect  ( 2014-08-01 03:58:02 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-07-31 07:55:28 -0500

Seen: 629 times

Last updated: Aug 01 '14