2

我正在关注官方 ROS 页面上的图像订阅教程。当我运行 my_subscriber 时,不会出现任何窗口弹出窗口。我打字——

rosrun image_transport_tutorial my_subscriber

输出是 -

init done
opengl support available

然后什么也没有发生。(即使输出“init done”也无法解释,因为 my_subscriber.cpp 中没有输出行)

我正在关注本教程 - ROS教程

我已经在不同的终端中运行了 roscore 和 rosrun image_transport_tutorial my_publisher pathtofile。我通过运行命令检查了发布者是否正在发布

rostopic list -v

my_subscriber 文件包含以下内容 -

#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");
}

已解决:我按照答案之一的建议在 try 块中添加了 waitKey 函数。

cv::waitKey(30);
4

1 回答 1

2

根据对此答案的评论,使用cv::startWindowThread()并不总是有效。也许这是你的问题。

尝试在cv::waitKey(10)之后添加cv::imshow。这将等待一些按键 10 毫秒,给窗口时间来显示图像。(在我看来,这总是一种肮脏的技巧,但这是在 OpenCV 中显示图像的常用方式......)

于 2015-03-10T12:46:16.690 回答