0

Rosanswers logo

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

void imageCallback(const sensor_msgs::ImageConstPtr&amp; 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-&gt;image;
    }
    catch(cv_bridge::Exception&amp; e)
    {
        ROS_ERROR(&quot;cv_bridge exception: %s&quot;, 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(&quot;hsv&quot;,img_hsv);

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

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

    //Convert CV image to ROS output image message and publish
    try
    {
        image_pub.publish(cv_ptr_out-&gt;toImageMsg());
    }
    catch(cv_bridge::Exception&amp; e)
    {
        ROS_ERROR(&quot;cv_bridge exception: %s&quot;, 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;

}


Originally posted by Thang Nguyen on ROS Answers with karma: 93 on 2016-06-18

Post score: 0

1 Answers1

0

Rosanswers logo

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;

Originally posted by ahendrix with karma: 47576 on 2016-06-18

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Thang Nguyen on 2016-06-18:
Thanks ahendrix. What is the right way to initialize cv_ptr_out?