UBuntu20.04安装自己的opencv以及cv_bridge

解决问题:

默认UBuntu20.04的ROS自带的opencv版本是4.2.0,但是在写代码或者跑别人的项目时,所需要的版本往往不同。而且如果代码中使用了cv_bridge,虽然ROS里面是自带cv_bridge的,但他是连接到ROS自带的opencv,即使将CMakeLists.txt更改为要求的opencv的路径并编译通过,还是会链接到自带的版本。于是应该重新安装cv_bridge并链接到自定义的opencv版本。

一、安装自定义版本opencv(这里安装4.5.0)

一般是放在usr/local下,这是参考博客:

建议在local下再建一个文件夹opencv450,这样如果以后要装更多版本的opencv就方便管理,具体可以参考这篇博客:

ubuntu16.04 install opencv、Sohpus、PCL、OctoMap、VTK_lutu.sohp-CSDN博客

二、安装对应ROS版本的cv_bridge并链接到指定的opencv

下载对应ROS版本的cv_bridge:

https://github.com/ros-perception/vision_opencv/tree/noetic

下载后将cv_bridge单独取出然后将CMakeLists.txt里面的opencv路径更改为自己的opencv路径再安装。以我的opencv4.5.0为例:

我把它放在了这个位置:

打开cv_bridge里的CMakeList.txt,设置成指定的opencv版本:

set(OpenCV_DIR "/usr/local/share/opencv4")
find_package(OpenCV 4.5.0 REQUIRED
  COMPONENTS
    opencv_core
    opencv_imgproc
    opencv_imgcodecs
  CONFIG
)

set的opencv的路径就写指定的opencv下载路径对应的share文件夹下的opencv文件夹。我在安装Opencv4.5.0的时候把它装在usr/local下了,装的时候应该有一个指令是:

cmake -D CMAKE_INSTALL_PREFIX=/usr/local ..

这个 CMAKE_INSTALL_PREFIX就是安装的路径,建议在local下再建一个文件夹opencv4.5.0,这样方便管理。

然后安装:

这里的CMAKE_INSTALL_PREFIX和安装自定义的opencv安装位置意思是一样的,也可以在local下再建一个文件夹方便管理,比如CMAKE_INSTALL_PREFIX=/usr/local/cv_bridge450。

cmake -D CMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=Release -DSETUPTOOLS_DEB_LAYOUT=OFF ..
sudo make -j8
sudo make install

我这里写的还是usr/local。

参考博客:ROS工程不使用ROS自带的OpenCV_ros 不使用自带opencv-CSDN博客

不过因为报错“error: option --install-layout not recognized”所以把cmake那一句改了一下,参考解决办法:error: option --install-layout not recognized-CSDN博客,感谢评论的大佬。

如果install结束之后是这样就安装好了:

q**@q**-*****-***:~/Packages/cv_bridge/build$ sudo make install
[  0%] Built target geometry_msgs_generate_messages_cpp
[  0%] Built target std_msgs_generate_messages_nodejs
[  0%] Built target std_msgs_generate_messages_cpp
[  0%] Built target std_msgs_generate_messages_eus
[  0%] Built target geometry_msgs_generate_messages_lisp
[  0%] Built target geometry_msgs_generate_messages_py
[  0%] Built target sensor_msgs_generate_messages_nodejs
[  0%] Built target sensor_msgs_generate_messages_lisp
[  0%] Built target geometry_msgs_generate_messages_eus
[  0%] Built target std_msgs_generate_messages_lisp
[  0%] Built target std_msgs_generate_messages_py
[  0%] Built target sensor_msgs_generate_messages_cpp
[  0%] Built target sensor_msgs_generate_messages_eus
[  0%] Built target geometry_msgs_generate_messages_nodejs
[  0%] Built target sensor_msgs_generate_messages_py
[ 50%] Built target cv_bridge
[100%] Built target cv_bridge_boost
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/_setup_util.py
-- Up-to-date: /usr/local/env.sh
-- Up-to-date: /usr/local/setup.bash
-- Up-to-date: /usr/local/local_setup.bash
-- Up-to-date: /usr/local/setup.sh
-- Up-to-date: /usr/local/local_setup.sh
-- Up-to-date: /usr/local/setup.zsh
-- Up-to-date: /usr/local/local_setup.zsh
-- Up-to-date: /usr/local/.rosinstall
-- Installing: /usr/local/lib/pkgconfig/cv_bridge.pc
-- Up-to-date: /usr/local/share/cv_bridge/cmake/cv_bridge-extras.cmake
-- Up-to-date: /usr/local/share/cv_bridge/cmake/cv_bridgeConfig.cmake
-- Up-to-date: /usr/local/share/cv_bridge/cmake/cv_bridgeConfig-version.cmake
-- Up-to-date: /usr/local/share/cv_bridge/package.xml
+ cd /home/qly/Packages/cv_bridge
+ mkdir -p /usr/local/lib/python3.8/site-packages
+ /usr/bin/env PYTHONPATH=/usr/local/lib/python3.8/site-packages:/home/qly/Packages/cv_bridge/build/lib/python3.8/site-packages: CATKIN_BINARY_DIR=/home/qly/Packages/cv_bridge/build /usr/bin/python3 /home/qly/Packages/cv_bridge/setup.py egg_info --egg-base /home/qly/Packages/cv_bridge/build build --build-base /home/qly/Packages/cv_bridge/build install --root=/ --prefix=/usr/local --install-scripts=/usr/local/bin
running egg_info
creating /home/qly/Packages/cv_bridge/build/cv_bridge.egg-info
writing /home/qly/Packages/cv_bridge/build/cv_bridge.egg-info/PKG-INFO
writing dependency_links to /home/qly/Packages/cv_bridge/build/cv_bridge.egg-info/dependency_links.txt
writing top-level names to /home/qly/Packages/cv_bridge/build/cv_bridge.egg-info/top_level.txt
writing manifest file '/home/qly/Packages/cv_bridge/build/cv_bridge.egg-info/SOURCES.txt'
reading manifest file '/home/qly/Packages/cv_bridge/build/cv_bridge.egg-info/SOURCES.txt'
writing manifest file '/home/qly/Packages/cv_bridge/build/cv_bridge.egg-info/SOURCES.txt'
running build
running build_py
creating /home/qly/Packages/cv_bridge/build/lib
creating /home/qly/Packages/cv_bridge/build/lib/cv_bridge
copying python/cv_bridge/__init__.py -> /home/qly/Packages/cv_bridge/build/lib/cv_bridge
copying python/cv_bridge/core.py -> /home/qly/Packages/cv_bridge/build/lib/cv_bridge
running install
running install_lib
creating /usr/local/lib/python3.8/site-packages/cv_bridge
copying /home/qly/Packages/cv_bridge/build/lib/cv_bridge/__init__.py -> /usr/local/lib/python3.8/site-packages/cv_bridge
copying /home/qly/Packages/cv_bridge/build/lib/cv_bridge/core.py -> /usr/local/lib/python3.8/site-packages/cv_bridge
byte-compiling /usr/local/lib/python3.8/site-packages/cv_bridge/__init__.py to __init__.cpython-38.pyc
byte-compiling /usr/local/lib/python3.8/site-packages/cv_bridge/core.py to core.cpython-38.pyc
running install_egg_info
Copying /home/qly/Packages/cv_bridge/build/cv_bridge.egg-info to /usr/local/lib/python3.8/site-packages/cv_bridge-1.16.2-py3.8.egg-info
running install_scripts
/usr/local/lib/python3.8/dist-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated.
!!

        ********************************************************************************
        Please avoid running ``setup.py`` directly.
        Instead, use pypa/build, pypa/installer or other
        standards-based tools.

        See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details.
        ********************************************************************************

!!
  self.initialize_options()
-- Installing: /usr/local/include/cv_bridge
-- Installing: /usr/local/include/cv_bridge/cv_bridge.h
-- Installing: /usr/local/include/cv_bridge/rgb_colors.h
-- Installing: /usr/local/lib/python3.8/site-packages/cv_bridge/boost/__init__.py
-- Installing: /usr/local/lib/libcv_bridge.so
-- Set runtime path of "/usr/local/lib/libcv_bridge.so" to ""

然后如果要在自己的项目里用opencv的话,继续按照这篇博客里接着走就行:ROS工程不使用ROS自带的OpenCV_ros 不使用自带opencv-CSDN博客

感谢各位大佬提供的资料!