Image_subscriber using Image_transport couldn't run

asked 2020-04-19 05:25:02 -0600

N.N.Huy gravatar image

Hello everyone, i am newbie in ROS and i following image_transport to create a image subscriber. How ever, when i try to run this node, it notifies that:

(view:31131): GLib-GObject-CRITICAL **: 17:09:18.563: g_object_unref: assertion 'G_IS_OBJECT (object)' failed

(view:31131): GLib-GObject-CRITICAL **: 17:09:18.617: g_object_unref: assertion 'G_IS_OBJECT (object)' failed OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/highgui/src/window.cpp, line 304 terminate called after throwing an instance of 'cv::Exception' what(): /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow

Aborted (core dumped)

This is my subscriber code:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>

void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
  try
  {
    cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
    cv::waitKey(30);
  }
  catch (cv_bridge::Exception& e)
  {
    ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
  }
}

int main(int argc, char **argv)
{

  ros::init(argc, argv, "my_image_subscriber");
  ros::NodeHandle nh;
  //    Initialize and start node named "image_listener"
  cv::namedWindow("view");//    Create an OpenCV display window.
  cv::startWindowThread();

  image_transport::ImageTransport it(nh);
  //    create an ImageTransport instance, initializing it with our NodeHandle

  image_transport::Subscriber sub = it.subscribe("my_camera1/image", 1, imageCallback);

  ros::spin();
  cv::destroyWindow("view");
}

Can you help me to solve this issue ? Thanks in advance!

edit retag flag offensive close merge delete