2
votes

I am following image subscribe tutorial on official ROS page. when I run my_subscriber no window popup appears. I type -

rosrun image_transport_tutorial my_subscriber

output is -

init done
opengl support available

And then nothing happens. (even the output "init done" is unexplained because there is not output line in my_subscriber.cpp)

I am following this tutorial - ROS tutorial

I have roscore and rosrun image_transport_tutorial my_publisher pathtofile already running in dofferent terminals. I checked that publisher is publishing by running command

rostopic list -v

my_subscriber file has the following contents -

#include <ros/ros.h>
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <stdio.h>
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
  try
  {
    cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
  }
  catch (cv_bridge::Exception& e)
  {
    ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
  }
}

int main(int argc, char **argv)
{
  std::cout<<"kapil";
  ros::init(argc, argv, "image_listener");
  ros::NodeHandle nh;
  cv::namedWindow("view");
  cv::startWindowThread();
  image_transport::ImageTransport it(nh);
  image_transport::Subscriber sub = it.subscribe("camera/image", 1, imageCallback);
  ros::spin();
  cv::destroyWindow("view");
}

Solved : I added waitKey function in the try block as suggested in one of the answers.

cv::waitKey(30);
1
Did you check if the topic camera/image is publishing something? Run this: $ rostopic list -v - Ha Dang
yes... i checked.. it is publishing. here is the output - Published topics: .. * /camera/image [sensor_msgs/Image] 1 publisher Subscribed topics: * /rosout [rosgraph_msgs/Log] 1 subscriber * /camera/image [sensor_msgs/Image] 1 subscriber - user12340
if you have the package image_view install, try the following: $rosrun image_view image_view image:=/camera/image . Tell me what you got. - Ha Dang
I have it installed. it is not working. this is the output - [ INFO] [1425987943.187518428]: Using transport "raw" init done opengl support available (image_view:12714): GLib-GObject-WARNING **: invalid uninstantiatable type '(null)' in cast to 'GtkWidget' (image_view:12714): GLib-GObject-WARNING **: instance of invalid non-instantiatable type '(null)' (image_view:12714): GLib-GObject-CRITICAL **: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed - user12340
try this: $rostopic echo /camera/image . Tell me what you get. - Ha Dang

1 Answers

2
votes

According to the comment to this answer, using cv::startWindowThread() does not always work. Maybe this is the issue in your case.

Try to add cv::waitKey(10) after cv::imshow instead. This will wait for some key press for 10 milliseconds, giving the window time to show the image. (This always seemed to me like a dirty hack, but it is the common way to show images in OpenCV...)