Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The node getting killed after few runs when publisher is added

I have a node that converts a ROS image to OpenCV image based on this. I am using this to obatin a depth images and depth values of pixels. I don't have any issues with it as long as I print out the depth values.

Then I created a different package, retaining the above code, but with an added publisher. I have added the complete code at the end. This node uses a custom message type: depth_centre.msg. But this node is automatically killed after a few times. (I tried counting the number of loops and it is gets killed around 570 to 600). Why is this happening? Do I need to do something with the loop rate or size of message queue?

Code:

#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>

static const std::string OPENCV_WINDOW = "Image window";
static const std::string OPENCV_WINDOW_2 = "SubImage window";
int count = 0;
class ImageConverter
{
  ros::NodeHandle nh_;
  image_transport::ImageTransport it_;
  image_transport::Subscriber image_sub_;
  image_transport::Publisher image_pub_;

public:
  ImageConverter()
    : it_(nh_)
  {
    // Subscrive to input video feed and publish output video feed
    image_sub_ = it_.subscribe("/camera/depth/image_rect_color", 1, 
      &ImageConverter::imageCb, this);
    image_pub_ = it_.advertise("/image_converter/output_video", 1);

    //cv::namedWindow(OPENCV_WINDOW);
    //cv::namedWindow(OPENCV_WINDOW_2);
  }

  ~ImageConverter()
  {
    //cv::destroyWindow(OPENCV_WINDOW);
    //cv::destroyWindow(OPENCV_WINDOW_2);
  }



void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
      cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_32FC1);
    }
    catch (cv_bridge::Exception& e)
    {
      ROS_ERROR("cv_bridge exception: %s", e.what());
      return;
    }


    cv::Mat mainImage=cv_ptr->image;
    cv::Mat subImage(mainImage, cv::Rect(100,100,1000,600));

    // Update GUI Window
    //cv::imshow(OPENCV_WINDOW, cv_ptr->image);
    //cv::imshow(OPENCV_WINDOW_2, subImage);
    //cv::waitKey(3);

    // Output modified video stream
    image_pub_.publish(cv_ptr->toImageMsg());
    cv::Scalar intensity = cv_ptr->image.at<float>(359, 639); 
    float depth = intensity.val[0];
    count++;
    ROS_INFO("DEPTH INFO of pixel(count%d): [%f]",count,depth); 

  }
};

int main(int argc, char** argv)
{
  ros::init(argc, argv, "rscv_num");
  ImageConverter ic;
  ros::spin();
  return 0;
}

The node getting killed after few runs when publisher is added

I have a node that converts a ROS image to OpenCV image based on this. I am using this to obatin a depth images and depth values of pixels. I don't have any issues with it as long as I print out the depth values.

Then I created a different package, retaining the above code, but with an added publisher. I have added the complete code at the end. This node uses a custom message type: depth_centre.msg. But this node is automatically killed after a few times. (I tried counting the number of loops and it is gets killed around 570 to 600). Why is this happening? Do I need to do something with the loop rate or size of message queue?

Code:

#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 <rscv_pub_centre/depth_centre.h>

static const std::string OPENCV_WINDOW = "Image window";
static const std::string OPENCV_WINDOW_2 = "SubImage window";
int count = 0;
class ImageConverter
{
  ros::NodeHandle nh_;
  ros::NodeHandle pub_;
  image_transport::ImageTransport it_;
  image_transport::Subscriber image_sub_;
  image_transport::Publisher image_pub_;

public:
  ImageConverter()
    : it_(nh_)
  {
    // Subscrive to input video feed and publish output video feed
    image_sub_ = it_.subscribe("/camera/depth/image_rect_color", 1, 
      &ImageConverter::imageCb, this);
    image_pub_ = it_.advertise("/image_converter/output_video", 1);

    //cv::namedWindow(OPENCV_WINDOW);
    //cv::namedWindow(OPENCV_WINDOW_2);
  }

  ~ImageConverter()
  {
    //cv::destroyWindow(OPENCV_WINDOW);
    //cv::destroyWindow(OPENCV_WINDOW_2);
  }

  void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {
     cv_bridge::CvImagePtr cv_ptr;
    try
    {
      cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_32FC1);
    }
    catch (cv_bridge::Exception& e)
    {
      ROS_ERROR("cv_bridge exception: %s", e.what());
      return;
    }
 
    cv::Mat mainImage=cv_ptr->image;
    cv::Mat subImage(mainImage, cv::Rect(100,100,1000,600));

    // Update GUI Window
    //cv::imshow(OPENCV_WINDOW, cv_ptr->image);
    //cv::imshow(OPENCV_WINDOW_2, subImage);
    //cv::waitKey(3);

    // Output modified video stream
    image_pub_.publish(cv_ptr->toImageMsg());

    //Publish Values in meter
    ros::Publisher depth_pub = pub_.advertise<rscv_pub_centre::depth_centre>("depth_m_centre", 1);
    ros::Rate loop_rate(10);

    while (ros::ok())
    {
        rscv_pub_centre::depth_centre msg;
        int rw = cv_ptr->image.rows;
        int cl = cv_ptr->image.cols;
        for (int i=(rw/2)-2; i<(rw/2); i++)
        {
            for (int j=(cl/2)-2; j<(cl/2); j++)
            {
                      cv::Scalar intensity = cv_ptr->image.at<float>(359, 639); 
cv_ptr->image.at<float>(i, j); 
                      float depth = intensity.val[0];
                      msg.row=i;
                      msg.col=j;
                      msg.depth_in_meter=depth;

                      ROS_INFO("DEPTH INFO(%d,%d): [%f]",msg.row,msg.col,msg.depth_in_meter);
                      depth_pub.publish(msg); 

             }
        }
        count++;
    ROS_INFO("DEPTH INFO of pixel(count%d): [%f]",count,depth); 

    ROS_INFO("\nCount: %d", count);
        ros::spinOnce();
        loop_rate.sleep();
     }
  }
};

int main(int argc, char** argv)
{
  ros::init(argc, argv, "rscv_num");
"rscv_pub_centre_node");
  ImageConverter ic;
  ros::spin();
  return 0;
}