参考资料:

https://medium.com/@beta_b0t/how-to-setup-ros-with-python-3-44a69ca36674
Unable to use cv_bridge with ROS Kinetic and Python3
CMake Error :Could not find a package configuration file provided by “cv_bridge“解决办法 —— いしょ
Boost Python 的安装配置以及示例 (支持python3.x) —— zp0int
关于cmake找不到库的问题 —— Shadow_Yan
vision_opencv
ROS——在Ubuntu18.04下基于ROS Melodic编译python3的cv_bridge —— Irving.Gao

使用python3编译boost
回到安装boost的文件夹

cd boost_1_78_0

# sudo find / -name "python3.6m" # 寻找你系统下的 python3.6m
# which python3 # 寻找你系统下 python3 的安装路径
echo "using mpi ;
using gcc :  : g++ ;
using python : 3.6 : /usr/bin/python3 : /usr/include/python3.6m : /usr/local/lib ;" > ~/user-config.jam

./bootstrap.sh --with-python=/usr/bin/python3 --with-python-version=3.6 --with-python-root=/usr/local/lib/python3.6 --prefix=/usr/local
sudo ./b2 install -a --with=all
sudo ldconfig

console_bridge

git clone https://github.com/ros/console_bridge
cd console_bridge
mkdir -p build && cd build
cmake ..
sudo make install

python3+bridge

sudo apt-get install python3-pip python3-yaml
sudo pip3 install rospkg catkin_pkg
sudo apt-get install python-catkin-tools python3-dev python3-numpy
mkdir -p ~/catkin_build_ws && cd ~/catkin_build_ws

catkin clean
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so

catkin config --install
mkdir src
cd src
git clone -b melodic https://gitee.com/vencen/vision_opencv.git
apt-cache show ros-melodic-cv-bridge | grep Version

在git repo中签出正确的版本。在我们的例子中是1.13.0

cd vision_opencv/
git checkout 1.13.0
cd ~/catkin_build_ws
nano src/vision_opencv/cv_bridge/CMakeLists.txt

find_package(catkin REQUIRED COMPONENTS rosconsole sensor_msgs)
前添加set(rosconsole_DIR /opt/ros/melodic/share/rosconsole/cmake)和,这些路径由sudo find / -name “rosconsole”sudo find / -name “sensor_msgs”找到。

catkin build cv_bridge
source install/setup.bash --extend


添加编译成功的软件环境变量

nano ~/.bashrc

底部添加:source ~/catkin_build_ws/install/setup.bash —extend

. ~/.bashrc

更新.bashrc

测试

新开一个终端:

python3
import cv_bridge
from cv_bridge.boost.cv_bridge_boost import getCvType

cv_bridge
getCvType