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

I need help with an error when run an OpenCV program

asked 2016-06-18 18:45:02 -0500

Thang Nguyen gravatar image

updated 2016-06-18 18:47:43 -0500

I am sorry if my subject is not clear but I am not sure how to describe this problem. Below is the error I receive when I try to run my node:

ubuntu@tegra-ubuntu:~/Documents/WorkSpaceOpenCV$ rosrun img_processor img_processor
img_processor: /usr/include/boost/smart_ptr/shared_ptr.hpp:653: typename boost::detail::sp_member_access<T>::type boost::shared_ptr<T>::operator->() const [with T = cv_bridge::CvImage; typename boost::detail::sp_member_access<T>::type = cv_bridge::CvImage*]: Assertion `px != 0' failed.
Aborted

I have 2 nodes. One is /camera which collect data from webcam and publish as /camera/raw_image.This run fine and I can see the image using image_viewer. Another node subscribes to /camera/raw_image and convert raw_image to hsv_image. I got error when I ran this node. You can see the code of my node below. Another question I have is even it abort the node, the rqt_graph still show this node run but with a red circle around it: screenshot_img_processor.png I am not sure what does it mean. I am new to ROS + OpenCV + C++

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

class Process_hsv
{
protected:
    ros::NodeHandle nh;

    image_transport::ImageTransport it;
    image_transport::Publisher image_pub;
    image_transport::Subscriber image_sub;

public:
    Process_hsv(ros::NodeHandle & nh):nh(nh), it(nh)
    {
        //Advertise image messages to a topic
        image_pub = it.advertise("/process_hsv/output_image",1);
        //Listen for image messages on a topic and setup callback
        image_sub = it.subscribe("/camera/image_raw",1,&Process_hsv::imageCallback,this);
        // Open HighGUI Window
        cv::namedWindow("hsv");
    }

    ~Process_hsv()
    {
        cv::destroyWindow("hsv");
    }

    void imageCallback(const sensor_msgs::ImageConstPtr& msg)
    {
        cv_bridge::CvImagePtr cv_ptr_in,cv_ptr_out;
        cv::Mat img_in, img_hsv;
        //Convert ROS iput image message to CV image by using cv_bridge
        try
        {
            cv_ptr_in = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
            img_in = cv_ptr_in->image;
        }
        catch(cv_bridge::Exception& e)
        {
            ROS_ERROR("cv_bridge exception: %s", e.what());
        }

        //Convert input img from BGR to HSV
        cv::cvtColor(img_in, img_hsv, CV_BGR2HSV);
        //Display HSV Image in HighGUI window
        cv::imshow("hsv",img_hsv);

        //Needed to keep the HighGUI Window open
        cv::waitKey(3);

        //Convert cv::Mat to IplImage
        cv_ptr_out->image = img_hsv;

        //Convert CV image to ROS output image message and publish
        try
        {
            image_pub.publish(cv_ptr_out->toImageMsg());
        }
        catch(cv_bridge::Exception& e)
        {
            ROS_ERROR("cv_bridge exception: %s", e.what());
        }

    }
};

int main (int argc, char **argv)
{
    //Initialize ROS Node
    ros::init(argc, argv, "image_processor");
    // Start node and create a Node Handler
    ros::NodeHandle nh;

    Process_hsv process_hsv(nh);
    ros::spin();
    return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-06-18 21:39:51 -0500

ahendrix gravatar image

In general this error (assertion failed: px != 0)means that you are trying to dereference a boost::shared_ptr that has not been initialized to point to anything.

Most of the xxPtr types in ROS are implemented with boost::shared_ptr, so you're probably using them even if you don't realize it.

Without a full stack trace it's hard to say exactly what is wrong, but my first guess would be that you're dereferencing cv_ptr_out before you initialize it:

cv_ptr_out->image = img_hsv;
edit flag offensive delete link more

Comments

Thanks ahendrix. What is the right way to initialize cv_ptr_out?

Thang Nguyen gravatar image Thang Nguyen  ( 2016-06-18 22:05:12 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-18 18:45:02 -0500

Seen: 1,254 times

Last updated: Jun 18 '16