基于ROS利用客户端和服务端实现C++节点和python节点间传送图像
配置ROS下和python3通信以及配置python3可用的cv_bridge
参考:https://blog.csdn.net/qq_33445388/article/details/116034290
sudo apt-get install ros-melodic-cv-bridge python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml
在需要用到的cv_bridge工作空间内(将vision_opencv作为另一package了)
git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv
apt-cache show ros-melodic-cv-bridge | grep Version
cd src/vision_opencv/
git checkout 1.13.0
cd ../../
指示catkin将内置包安装到安装位置,
catkin config --install
catkin build cv_bridge
需要使用cv_bridge的时候 source install/setup.bash --extend 即可
自己建立的其他功能包正常catkin_make
例如, catkin_make -DCATKIN_WHITELIST_PACKAGES=service_example -DPYTHON_EXECUTABLE=/usr/bin/python3
指定编译有改动的某一package,以及和python3通信时用-DPYTHON_EXECUTABLE=/usr/bin/python3
C++
cv::Mat---->cv_bridge::CvImagePtr---->sensor_msgs::Image
sensor_msgs::ImagePtr msgLeft;
msgLeft = cv_bridge::CvImage(std_msgs::Header(), "rgb8",imLeft ).toImageMsg();
srv.request.src=*msgLeft;
sensor_msgs::Image---->cv_bridge::CvImagePtr---->cv::Mat
sensor_msgs::Image msg=srv.response.dit;#request.dit是sensor_msgs::Image格式
cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, "rgb8");
cv::Mat imResponse= cv_ptr->image;
python
sensor_msgs::Image----->cv2能处理的格式
cv_img = bridge.imgmsg_to_cv2(request.src, "rgb8")#request.src是sensor_msgs::Image格式
# cv2.imshow("frame" , cv_img)
# cv2.waitKey(1)
im= cv2.resize(cv_img, (400, 400))
cv2能处理的格式----->sensor_msgs::Image
response.dit=bridge.cv2_to_imgmsg(im, "rgb8")
cv_bridge编码信息
mono8: CV_8UC1, grayscale image
mono16: CV_16UC1, 16-bit grayscale image
bgr8: CV_8UC3, color image with blue-green-red color order
rgb8: CV_8UC3, color image with red-green-blue color order
bgra8: CV_8UC4, BGR color image with an alpha channel
rgba8: CV_8UC4, RGB color image with an alpha channel
2.报错解决
有未定义的引用时,一般是库没链接到位
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
sensor_msgs
message_generation
cv_bridge
image_transport
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES service_example
CATKIN_DEPENDS roscpp rospy std_msgs sensor_msgs message_runtime cv_bridge
# DEPENDS system_lib
)
)
3.友情提醒
http://wiki.ros.org/cn/ROS/Tutorials
要用到ros的某个基础模块时,如cv_bridge,在该网址里搜索cv_bridge即可