OpenCV在使用时的问题
摘要
如果多版本安装OpenCV,切勿默认安装,务必每安装一个版本都新建一个目录,这样就会少很多问题。但是安装ROS会不知不觉的将自带的OpenCV安装到默认路径。一样会导致冲突。这时候最好的解决办法还没找到,所以最好的办法就是用ROS自带的OpenCV,虽然有警告,但是可以用呀!
背景
- 安装Ubuntu20.04对应的ROS,顺带安装个OpenCV4.2.0安装到默认路径(相当讨厌,也有优势);
- 安装了OpenCV4.8.0 到指定目录,用于ORB-SLAM3,顺利使用没有啥问题,未见冲突;
- 配置OpenCV3.2.0和VINS-fusion,编译通过,但是显示有警告。最可怕的是运行失败(直接段错误)可以使用gdb调试查看原因,原因是,在链接库文件时它偷偷摸摸链接到OpenCV4.2.0。让人担心的警告直接发生了。可供参考
- 解决方法,就是修改找opencv直接链接到ROS自带的4.2.0,编译VINS-fusion通过,但是显示有警告。可是它真的没有偷偷摸摸链接到其他库。勉强可用!!!
关于第3条,警告信息如下,这几个可能conflict
bash
/usr/local/opencv3.2.0/lib/libopencv_core.so.3.2
/usr/local/opencv3.2.0/lib/libopencv_imgcodecs.so.3.2
/usr/local/opencv3.2.0/lib/libopencv_calib3d.so.3.2
/usr/lib/x86_64-linux-gnu/libopencv_core.so.4.2
/usr/lib/x86_64-linux-gnu/libopencv_imgcodecs.so.4.2
# /usr/local/opencv3.2.0/lib/libboost_filesystem.so.1.71.0
/usr/local/opencv3.2.0/lib/libopencv_features2d.so.3.2
关于第4条,警告信息如下:
bash
...
[ 97%] Linking CXX executable /home/pf/VINSfusionFix/devel/lib/vins/kitti_gps_test
[ 98%] Linking CXX executable /home/pf/VINSfusionFix/devel/lib/vins/kitti_odom_test
/usr/bin/ld: warning: libopencv_calib3d.so.4.2, needed by /home/pf/VINSfusionFix/devel/lib/libvins_lib.so, may conflict with libopencv_calib3d.so.3.2
/usr/bin/ld: warning: libopencv_imgcodecs.so.3.2, needed by /home/pf/VINSfusionFix/devel/lib/libvins_lib.so, may conflict with libopencv_imgcodecs.so.4.2
/usr/bin/ld: warning: libopencv_core.so.3.2, needed by /usr/local/opencv3.2.0/lib/libopencv_calib3d.so.3.2, may conflict with libopencv_core.so.4.2
/usr/bin/ld: warning: libopencv_calib3d.so.4.2, needed by /home/pf/VINSfusionFix/devel/lib/libvins_lib.so, may conflict with libopencv_calib3d.so.3.2
/usr/bin/ld: warning: libopencv_imgcodecs.so.3.2, needed by /home/pf/VINSfusionFix/devel/lib/libvins_lib.so, may conflict with libopencv_imgcodecs.so.4.2
/usr/bin/ld: warning: libopencv_core.so.3.2, needed by /usr/local/opencv3.2.0/lib/libopencv_calib3d.so.3.2, may conflict with libopencv_core.so.4.2
[ 98%] Built target kitti_gps_test
[ 98%] Built target kitti_odom_test
[100%] Linking CXX executable /home/pf/VINSfusionFix/devel/lib/vins/vins_node
/usr/bin/ld: warning: libopencv_calib3d.so.4.2, needed by /home/pf/VINSfusionFix/devel/lib/libvins_lib.so, may conflict with libopencv_calib3d.so.3.2
/usr/bin/ld: warning: libopencv_core.so.3.2, needed by /usr/local/opencv3.2.0/lib/libopencv_calib3d.so.3.2, may conflict with libopencv_core.so.4.2
[100%] Built target vins_node
重点解释说明第4条
解决方法,就是修改找opencv直接链接到ROS自带的4.2.0,编译VINS-fusion通过,但是显示有警告。可是它真的没有偷偷摸摸链接到其他库。勉强可用!!!
修改一些报错
ROS noetic版本中的OpenCV4.2和VINS-fusion中需要使用的OpenCV3冲突,修改以兼容OpenCV4。幸运的是发现不用修改代码中不兼容的变量,可以直接添加缺失的头文件即可:
如下:
-
在camera_models包中的头文件Chessboard.h中添加
#include <opencv2/imgproc/types_c.h>
#include <opencv2/calib3d/calib3d_c.h> -
在camera_models包中的头文件CameraCalibration.h、pose_graph包中的头文件pose_graph.h和keyframe.h、BRIEF.h中添加
#include <opencv2/imgproc/types_c.h>
#include <opencv2/imgproc/imgproc_c.h> -
若vins_estiamtor包中报错,将头文件替换为
// #include <opencv/cv.h>
// #include <opencv/highgui.h>
#include <opencv2/highgui.hpp>
#include <opencv2/cvconfig.h>
找OpenCV
重点是找默认的opencv ,
-
你以为直接确定版本可以找到ROS对应的4.2.0,可是它找不到
find_package(OpenCV 4.2 REQUIRED)
message(WARNING "OpenCV_VERSION: ${OpenCV_VERSION}")#输出:
#CMake Warning at CMakeLists.txt:21 (message):
#OpenCV_VERSION: 4.8.0 -
你以为不加版本直接找可以找到ROS对应的4.2.0,可是它也找不到,不仅找不到,它找上一次有版本的opencv(自己体会这句话)
find_package(OpenCV REQUIRED)
message(WARNING "OpenCV_VERSION: ${OpenCV_VERSION}")#输出:
#CMake Warning at CMakeLists.txt:21 (message):
#OpenCV_VERSION: 4.8.0 -
正确的方法 去找ROS对应的4.2.0对应的文件OpenCVConfig.cmake 然后找到路径,这就可以了。
-
cmake编译原理是需要CMakeLists.txt找到 .cmake 文件,就在那几个目录下面,
- /usr/share/OpenCV/
- /usr/lib/x86_64-linux-gnu/cmake/opencv4/
- ...
set(OpenCV_DIR /usr/lib/x86_64-linux-gnu/cmake/opencv4/)
find_package(OpenCV REQUIRED)-- Found OpenCV: /usr (found version "4.2.0")
OPENCV VERSION: 4.2.0
find_package(catkin REQUIRED COMPONENTS
OpenCV
)
bash
pf@pf-NUC12WSKi7:~/VINSfusionFix$ catkin_make
Base path: /home/pf/VINSfusionFix
Source space: /home/pf/VINSfusionFix/src
Build space: /home/pf/VINSfusionFix/build
Devel space: /home/pf/VINSfusionFix/devel
Install space: /home/pf/VINSfusionFix/install
####
#### Running command: "make cmake_check_build_system" in "/home/pf/VINSfusionFix/build"
####
####
#### Running command: "make -j16 -l16" in "/home/pf/VINSfusionFix/build"
####
[ 5%] Built target libGeographiccc
[ 24%] Built target camera_models
[ 45%] Built target Calibrations
[ 49%] Built target global_fusion_node
[ 68%] Built target loop_fusion_node
[ 91%] Built target vins_lib
[ 97%] Built target kitti_gps_test
[ 97%] Built target kitti_odom_test
[100%] Built target vins_node