这里首先要完成ros和Pycharm联合编程的设置,具体可以参考qt和pycharm联合ros编程的设置
之后如果用到处理图像的这块的话,就需要下面cv_bridge的使用。
因为原来系统自带的cv_bridge只能在python2下使用,所以这里需要python3编译一下cv_bridge。
1、首先进入系统真正的空间中:打开一个新的终端,最好运行一下
source deactivate
2、首先进入python3的环境并安装相关依赖包
sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-kinetic-cv-bridge
3、创建一个工作空间用于存放待编译的 cv_bridge
文件
mkdir -p catkin_workspace/src
4、指示catkin设置cmake变量
cd catkin_workspace
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.5m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so
5、Instruct catkin to install built packages into install place。这一步不成功也没关系,可不用
catkin config --install
6、在catkin_workspace工作空间中克隆 cv_bridge src
git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv
7、Find version of cv_bridge in your repository
apt-cache show ros-kinetic-cv-bridge | grep Version
8、Checkout right version in git repo. In our case it is 1.12.8
cd src/vision_opencv/
git checkout 1.12.8
cd ../../
9、这里可能会有一个问题提前说一下,如果编译过程中遇到下面的问题。(提前看一下下面这个东西,如果是不对的,提前改过来)
CMake Error at /usr/share/cmake-3.6/Modules/FindBoost.cmake:1677 (message):
Unable to find the requested Boost libraries.
Boost version: 1.58.0
Boost include path: /usr/include
Could not find the following Boost libraries:
boost_python3
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the
directory containing Boost libraries or BOOST_ROOT to the location of
Boost.
Call Stack (most recent call first):
CMakeLists.txt:11 (find_package)
这是因为CMake试图找到libboost_python3.so
库,但是在ubuntu
中它是libboost_python-py35.so
(/usr/lib/x86_64-linux-gnu/libboost_python-py35.so)。因此应该在文件src/vision_opencv/cv_bridge/CMakeLists.txt中将下面这行更改find_package()中的内容,更改为python-py35。然后再重新编译:
原始行:
find_package(Boost REQUIRED python3)
更改成:
find_package(Boost REQUIRED python-py35)
10、最后进行编译
catkin build
11、验证:在install的同级目录下打开终端,在终端下面进行Python的验证,如果没有问题的话证明成功了
source install/setup.bash --extend
python3
from cv_bridge.boost.cv_bridge_boost import getCvType
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q5LlkbgF-1606876116651)(/home/wl/.config/Typora/typora-user-images/image-20201201214907844.png)]
至此,使用python3编译好的cv_bridge已经存在于路径
1、首先查看默认的cv_bridge的位置在哪里(’/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/init.py’)
python3
import cv_bridge
cv_bridge.__file__
# 这样就可以看到cv_bridge的默认路径了
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LFI8NhRP-1606876116654)(/home/wl/.config/Typora/typora-user-images/image-20201201220425303.png)]
2、下面再看看我们上面用python3编译好的cv_bridge的路径**(一定要在编译好的工作空间下打开,这里的编译的工作空间在/home/wl/software/ros_cvbridge/)**
source install/setup.bash --extend
python3
import cv_bridge
cv_bridge.__file__
可以看到我们我们编译好的cv_bridge的路径是(’/home/wl/software/ros_cvbridge/install/lib/python3/dist-packages/cv_bridge/init.py’)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-caWxbrsw-1606876116656)(/home/wl/.config/Typora/typora-user-images/image-20201201223317058.png)]
下面找到了路径以后要就可以进行talker.py和listener.py就可以了
三、Pycharm编译ROS发布图像的发布订阅的节点消息。
1、首先下面的这个程序是摄像头捕捉图像,并且发出去的程序。看程序的注释,里面有几个需要注意的点,还是值得关注一下。
talk.py
#!/usr/bin/env python
# license removed for brevity
import os
envpath = '/home/wl/anaconda3/envs/image_location/lib/python3.6/site-packages/cv2/qt/plugins/platforms'
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = envpath
import rospy
import sys
# 下面这个sensor_msg.msg一定要写在path.remove之前,否则把路径一旦remove的话,再import这样是会报错的
from sensor_msgs.msg import Image
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
import cv2
sys.path.append('/home/wl/software/ros_cvbridge/install/lib/python3/dist-packages')
from cv_bridge import CvBridge
def talker():
pub = rospy.Publisher('/tutorial/image', Image, queue_size=1)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(30)
bridge = CvBridge()
Video = cv2.VideoCapture(0)
while not rospy.is_shutdown():
ret, img = Video.read()
cv2.imshow("talker", img)
cv2.waitKey(3)
pub.publish(bridge.cv2_to_imgmsg(img, "bgr8"))
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
listener.py
#!/usr/bin/python
import os
envpath = '/home/wl/anaconda3/envs/image_location/lib/python3.6/site-packages/cv2/qt/plugins/platforms'
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = envpath
import rospy
import sys
from sensor_msgs.msg import Image
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
import cv2
sys.path.append('/home/wl/software/ros_cvbridge/install/lib/python3/dist-packages')
from cv_bridge import CvBridge
def callback(imgmsg):
bridge = CvBridge()
img = bridge.imgmsg_to_cv2(imgmsg, "bgr8")
cv2.imshow("listener", img)
cv2.waitKey(3)
def listener():
# In ROS, nodes are uniquely named. If two nodes with the same
# node are launched, the previous one is kicked off. The
# anonymous=True flag means that rospy will choose a unique
# name for our 'listener' node so that multiple listeners can
# run simultaneously.
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("/tutorial/image", Image, callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
listener()