「ROS OpenCV」 接收无压缩图像话题并用OpenCV可视化窗口显示

创建工作空间和功能包

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make
cd ~/catkin_ws/src
catkin_create_pkg pub_sub_image_topic_pkg roscpp rospy std_msgs image_transport cv_bridge sensor_msgs
cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash

功能包依赖文件

gedit ~/catkin_ws/src/pub_sub_image_topic_pkg/package.xml
<?xml version="1.0"?>
<package format="2">
  <name>pub_sub_image_topic_pkg</name>
  <version>0.0.0</version>
  <description>The pub_sub_image_topic_pkg package</description>

  <maintainer email="[email protected]">q</maintainer>

  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>cv_bridge</build_depend>
  <build_depend>image_transport</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>sensor_msgs</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_export_depend>cv_bridge</build_export_depend>
  <build_export_depend>image_transport</build_export_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>sensor_msgs</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <exec_depend>cv_bridge</exec_depend>
  <exec_depend>image_transport</exec_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>sensor_msgs</exec_depend>
  <exec_depend>std_msgs</exec_depend>

  <export>

  </export>
</package>

可执行完整代码

gedit ~/catkin_ws/src/pub_sub_image_topic_pkg/src/sub_image_topic.cpp
#include   
#include   
#include   
#include   

// 回调函数,当有新的图像消息到达camera/image时该函数就会被调用
void imageCallback(const sensor_msgs::ImageConstPtr& msg)  
{  
	try  
	{  
		// 这段代码用于显示捕捉到的图像
		// 其中cv_bridge::toCvShare(msg, "bgr8")->image
		// 用于将ROS图像消息转化为Opencv支持的图像格式采用bgr8编码方式
		// 和发布节点CvImage(std_msgs::Header(), "bgr8", image).toImageMsg()作用相反
		cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image); 
		cv::waitKey(10);  
	}  
	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, "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");  
}  

编译文件CMakeLists.txt

# 开发环境
# Ubuntu 16.04、ros Kinetic 默认安装的 opnecv3.3.1
cmake_minimum_required(VERSION 3.0.2)

project(pub_sub_image_topic_pkg)

find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  rospy
  sensor_msgs
  std_msgs
)

catkin_package()

include_directories(${catkin_INCLUDE_DIRS})

find_package(OpenCV REQUIRED)

add_executable(pub_image_topic src/pub_image_topic.cpp)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(pub_image_topic ${OpenCV_LIBS} ${catkin_LIBRARIES})

add_executable(pub_video2image_topic src/pub_video2image_topic.cpp)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(pub_video2image_topic ${OpenCV_LIBS} ${catkin_LIBRARIES})

add_executable(pub_camera2image_topic src/pub_camera2image_topic.cpp)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(pub_camera2image_topic ${OpenCV_LIBS} ${catkin_LIBRARIES})

add_executable(sub_image_topic src/sub_image_topic.cpp)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(sub_image_topic ${OpenCV_LIBS} ${catkin_LIBRARIES})

运行效果

$ roscore
catkin_ws$ source devel/setup.bash 
catkin_ws$ rosrun pub_sub_image_topic_pkg pub_camera2image_topic 0
catkin_ws$ source devel/setup.bash 
catkin_ws$ rosrun pub_sub_image_topic_pkg sub_image_topic

「ROS OpenCV」 接收无压缩图像话题并用OpenCV可视化窗口显示_第1张图片

你可能感兴趣的:(从零开始学习SLAM,image_transport)