文章目录
一、安装包信息
1. 相关安装包版本
- eigen:版本:3.3.7
- ceres:版本:1.14
- opencv两个版本opencv3.4.6和ros noetic自带的opencv4
2. opencv双版本安装
安装不同的版本,只需要将其安装的路径修改即可,我这里是ros noetic使用的默认路径,opencv3.4.6的安装修改了安装路径到/usr/opencv_v_346
-
版本3.4.6
- opencv安装及修改安装路径参考教程:链接
- 关键是:
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/opencv_v_ ..
中CMAKE_INSTALL_PREFIX指定安装路径
- 关键是:
- 安装之后,opencv3版本相关文件路径
- /usr/opencv_v_346/bin/
- /usr/opencv_v_346/lib/
- /usr/opencv_v_346/include/opencv
- /usr/opencv_v_346/include/opencv2
- /usr/opencv_v_346/share/licenses/opencv3
- /usr/opencv_v_346/share/OpenCV
- opencv安装及修改安装路径参考教程:链接
-
ros中opencv4安装之后,相关文件路径(默认)
- /usr/bin/
- /usr/lib/jni/
- /usr/lib/x86_64-linux-gnu/
- /usr/include/opencv4
- /usr/include/opencv4/opencv2
- /usr/include/boost/compute/
- /usr/share/licenses/opencv4
- /usr/share/opencv4
- /usr/share/java/opencv4
- /usr/share/java/
- /usr/share/man/man1/
- /usr/share/doc/
- /usr/share/lintian/overrides/
- /var/lib/dpkg/info/
二、ros程序中使用自己的opencv包
方案1. 不使用cv_bridge(不推荐)
修改cmakelist中opencv的版本,使程序使用自己的opencv3.4.6
参考链接
- 找到自定义安装opencv的
opencvConfig.cmake
配置文件的路径/usr/opencv_v_346/share/Opencv
- 指定该文件的目录
set(OpenCV_DIR /usr/opencv_v_346/share/Opencv)
- 用find package指令找到OpenCV包
find_package(OpenCV 3 REQUIRED)
- 指定OpenCV的头文件目录
include_directories( ${OpenCV_INCLUDE_DIRS})
- 将编译生成的文件与OpenCV的动态库/静态库进行链接
target_link_libraries(${OpenCV_LIBS})
方案2. 修改ros中的cv_bridge包,使其调用自己的opencv版本
**方法:**进入cv_bridge配置文件所在目录,并用gedit指令打开相关配置文件进行修改
cd /opt/ros/noetic/share/cv_bridge/cmake/
2.1 修改cv_bridgeConfig.cmake
sudo gedit cv_bridgeConfig.cmake
- 对以下两个部分进行修改,一个是包含的头文件目录,将其修改为自己的opencv安装目录
注意自己安装目录前边还有个include;
#if(NOT "include;/usr/include/opencv4 " STREQUAL " ") # set(cv_bridge_INCLUDE_DIRS "") # set(_include_dirs "include;/usr/include/opencv4") # 346表示安装的版本是opencv3.4.6 if(NOT "include;/usr/opencv_v_346/include;/usr/opencv_v_346/include/opencv" STREQUAL " ") set(cv_bridge_INCLUDE_DIRS "") set(_include_dirs "include;/usr/opencv_v_346/include;/usr/opencv_v_346/include/opencv")
- 另一个是将指定库文件也修改为自己安装的opencv库文件
注意自己库文件前边还有个cv_bridge;
,如果丢失会报cv_bridge中的函数未定义#set(libraries "cv_bridge;/usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_dnn.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_features2d.so.4.2.0;/usr/lib/ x86_64-linux-gnu/libopencv_flann.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_highgui.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_ml.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_photo.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_stitching.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_video.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_videoio.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_aruco.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_bgsegm.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_bioinspired.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_ccalib.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_datasets.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_dnn_objdetect.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_dnn_superres.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_dpm.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_face.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_freetype.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_fuzzy.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_hdf.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_hfs.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_img_hash.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_line_descriptor.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_optflow.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_phase_unwrapping.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_plot.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_quality.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_reg.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_rgbd.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_saliency.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_shape.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_stereo.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_structured_light.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_superres.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_surface_matching.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_text.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_tracking.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_videostab.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_viz.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_ximgproc.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_xobjdetect.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_xphoto.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_core.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.4.2.0;/usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so.4.2.0") set(libraries "cv_bridge;/usr/opencv_v_346/lib/libopencv_videoio.so.3.4.6;/usr/opencv_v_346/lib/libopencv_imgproc.so.3.4.6;/usr/opencv_v_346/lib/libopencv_stitching.so.3.4.6;/usr/opencv_v_346/lib/libopencv_calib3d.so.3.4.6;/usr/opencv_v_346/lib/libopencv_objdetect.so.3.4.6;/usr/opencv_v_346/lib/libopencv_features2d.so.3.4.6;/usr/opencv_v_346/lib/libopencv_shape.so.3.4.6;/usr/opencv_v_346/lib/libopencv_imgcodecs.so.3.4.6;/usr/opencv_v_346/lib/libopencv_core.so.3.4.6;/usr/opencv_v_346/lib/libopencv_ml.so.3.4.6;/usr/opencv_v_346/lib/libopencv_flann.so.3.4.6;/usr/opencv_v_346/lib/libopencv_videostab.so.3.4.6;/usr/opencv_v_346/lib/libopencv_photo.so.3.4.6;/usr/opencv_v_346/lib/libopencv_superres.so.3.4.6;/usr/opencv_v_346/lib/libopencv_dnn.so.3.4.6;/usr/opencv_v_346/lib/libopencv_video.so.3.4.6;/usr/opencv_v_346/lib/libopencv_highgui.so.3.4.6")
2.2 修改cv_bridge-extras.cmake文件
sudo gedit cv_bridge-extras.cmake
如果不修改该文件,打印当前的opencv版本,仍是ros的,并且会有warning。(其实使用的还是自己安装的,只是显示的是ros的)
```python
#set(OpenCV_VERSION 4.2.0)
#set(OpenCV_VERSION_MAJOR 4)
#set(OpenCV_VERSION_MINOR 2)
#set(OpenCV_VERSION_PATCH 0)
#set(OpenCV_SHARED ON)
#set(OpenCV_CONFIG_PATH /usr/lib/x86_64-linux-gnu/cmake/opencv4)
#set(OpenCV_INSTALL_PATH /usr)
#set(OpenCV_LIB_COMPONENTS opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dnn_objdetect;opencv_dnn_superres;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_hfs;opencv_img_hash;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_quality;opencv_reg;opencv_rgbd;opencv_saliency;opencv_shape;opencv_stereo;opencv_structured_light;opencv_superres;opencv_surface_matching;opencv_text;opencv_tracking;opencv_videostab;opencv_viz;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto)
#set(OpenCV_USE_MANGLED_PATHS FALSE)
#set(OpenCV_MODULES_SUFFIX )
set(OpenCV_VERSION 3.4.6)
set(OpenCV_VERSION_MAJOR 3)
set(OpenCV_VERSION_MINOR 4)
set(OpenCV_VERSION_PATCH 6)
set(OpenCV_SHARED ON)
set(OpenCV_CONFIG_PATH /usr/opencv_v_346/share/OpenCV)
set(OpenCV_INSTALL_PATH /usr/opencv_v_346/)
set(OpenCV_LIB_COMPONENTS opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab)
set(OpenCV_USE_MANGLED_PATHS FALSE)
set(OpenCV_MODULES_SUFFIX )
```
方案3. 修改cv_bridge功能包,对源码进行编译
-
参考链接:
自己编译功能包cv_bridge -
相关命令
cmake文件输出打印消息:SET(Opencv_DIR "/usr/local/opencv2.4.11") find_package(OpenCV REQUIRED) MESSAGE(STATUS "OpenCV version: "${OpenCV_VERSION}) MESSAGE(${PROJECT_SOURCE_DIR}) MESSAGE(${PROJECT_BINARY_DIR})
查看linux下的opencv安装路径:
sudo find / -iname "*opencv*"
在全盘上不区分大小写,搜索带有关键字opencv的所有文件及文件夹都会输出到终端,如果输出太长建议输出到txt文件里查看,如下:
sudo find / -iname "*opencv*" > /home/ubuntu/Desktop/opencv_find.txt
查看opencv版本:
pkg-config --modversion opencv
3.1 源码下载地址以及源码修改
**目的:**使功能包使用opencv3编译而不是opencv4
源码官网地址
- 从官网下载cv_bridge的对应版本的源代码,拷贝cv_bridge安装包到工作空间下,
- 修改名称:将功能包文件名改为cv::bridge_1
- 修改cv::bridge_1/CMakeLists.txt中的工程名称
project(cv_bridge) 改为 project(cv_bridge_1)
- 修改cv::bridge_1/package.xml
<name>cv_bridge</name> 改为 <name>cv_bridge_1</name>
- 在cmakelist文件中,将
find_package(OpenCV 4)
的位置全改为OpenCv3
3.2 方式一:单独编译该源码
- 将该功能包单独放进ros工作空间
- 对该工作空间进行编译,生成cv_bridge_1功能包,然后在bashrc文件,添加环境变量
- 添加方式1: 直接使用 gedit 或 vi 编辑 .bashrc 文件,最后添加该内容
- 添加方式2:echo “source ~/工作空间/devel/setup.bash” >> ~/.bashrc
- 然后将vins fusion代码中,依赖项cv_bridge改为cv_bridge_1,同时将
find_package(OpenCV 4)
的位置全改为OpenCv3
3.3 方式二:该源码和vins fusion功能包一起编译
两个功能包同时编译,面临编译顺序的问题,因为vins功能包需要调用cv_bridge_1功能包,因此需要先对cv_bridge_1进行编译。
主要参考下面链接里方法2:手动修改依赖关系
功能包之间的相互调用
ROS中package.xml和cmakelist介绍
- 首先将cv_bridge_1功能包和vins fusion功能包放入同一个工作空间src目录下
- 然后将vins fusion代码中,所有使用cv_bridge的地方,在cmakelist文件中将cv_bridge改为cv_bridge_1;
- 所有使用cv_bridge的地方,修改package.xml文件,手动添加依赖:
<build_depend>cv_bridge_1</build_depend> <run_depend>cv_bridge_1</run_depend>
- 所有
find_package(OpenCV 4)
的位置全改为OpenCv3
- 最后直接catkin_make编译该工作空间即可