我正在关注官方 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);