OpenCV的安装与版本管理
本文最后更新于 2024年10月14日 下午
本文主要分享了在Linux下OpenCV库的安装与版本管理。
安装
1. 安装依赖
1 |
|
2. 新建文件夹
下载地址:https://opencv.org/releases/
点击Sources
进行下载自己需要的版本。
1 |
|
3. 编译安装
编译安装流程
假设我们已经安装好一版OpenCV,一般都默认安装在
/usr/local
下。如果需要安装另一个版本的OpenCV,就不能再安装到/usr/local
,而是选择其他路径,否则会覆盖掉之前的版本。
1 |
|
报错处理
1 |
|
3.1.0
-
icv_add_video_stream_FFMPEG
1
2
3
4// 在opencv-3.2.0/modules/videoio/src/cap_ffmpeg_impl.hpp 里最顶端添加:
#define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22)
#define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
#define AVFMT_RAWPICTURE 0x0020
3.2.0
-
/usr/include/c++/5/bits/c0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C 2011 standard.
在根目录的
CmakeLists.txt
开头加上一句:1
add_definitions(-std=c++11)
4. 环境变量配置
1 |
|
也可以直接修改~/.bashrc
或者~/.zshrc
(用哪个终端就改哪个)?值得一试:
1 |
|
我是都用了,反正只要路径设置正确,就算重复覆盖额外多余了,不会报错就行。
5. 多版本共存
1 |
|
6. 安装检查
1 |
|
7. 验证配置结果
任意目录创建一个test.cpp
文件,写入如下内容:
1 |
|
使用命令行在其文件夹下执行:
1 |
|
我这么做会报错,所以,自己在这里是在根目录下新建文件CMakeLists.txt
,写入以下内容:
1 |
|
开始编译:
1 |
|
输出以下内容,即表明配置成功:
1 |
|
版本管理
方法1. 去掉cv_bridge,自行在CMakeLists.txt文件里指定OpenCV的路径
首先进行第一章安装
的第4、5步。
后,编辑CMakeLists.txt
文件:
1 |
|
如果编译时出现类似“Build error : undefined reference to cv_bridge::CvImage::toImageMsg() const”问题,可以尝试加入
cv_bridge
:
1
2
3
find_package(catkin REQUIRED COMPONENTS
cv_bridge
)
方法2. 对于ROS编译,可以修改cv_bridge的配置文件,使得cv_bridge去调用我们自己安装的OpenCV版本
首先进行第一章安装
的第4、5步。
后,编辑CMakeLists.txt
文件:
-
首先打开终端并进入cv_bridge配置文件所在目录,并用gedit指令打开该配置文件。
1
2
3# cd /opt/ros/kinetic/share/cv_bridge/cmake/
cd /opt/ros/melodic/share/cv_bridge/cmake/
sudo gedit cv_bridgeConfig.cmake -
然后按ctrl+f,输入关键词include找到以下内容并按图中的方式进行修改。Line 96左右。
1
2set(_include_dirs "include;/usr/local/opencv/opencv345/include/opencv2;/usr/local/opencv/opencv345/include/opencv;/usr/local/opencv/opencv345/include;/usr/include")
set(_include_dirs "include;/usr/local/opencv/opencv320/include/opencv2;/usr/local/opencv/opencv320/include/opencv;/usr/local/opencv/opencv320/include;/usr/include") -
将默认的OpenCV头文件目录替换后,再按ctrl+f并输入关键词libraries可以找到下图的区域,我们将其库文件也进行替换。Line 120左右。
1
2
3
4set(libraries "cv_bridge;/usr/local/opencv/opencv345/lib/libopencv_core.so.3.4.5;/usr/local/opencv/opencv345/lib/libopencv_imgproc.so.3.4.5;/usr/local/opencv/opencv345/lib/libopencv_imgcodecs.so.3.4.5;/usr/local/opencv/opencv345/lib/libopencv_calib3d.so.3.4.5;/usr/local/opencv/opencv345/lib/libopencv_highgui.so.3.4.5")
set(libraries "cv_bridge;/usr/local/opencv/opencv320/lib/libopencv_core.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_imgproc.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_imgcodecs.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_calib3d.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_highgui.so.3.2.0")
# OpenCV3.2.0库文件全
set(libraries "cv_bridge;/usr/local/opencv/opencv320/lib/libopencv_calib3d.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_core.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_features2d.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_flann.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_highgui.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_imgcodecs.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_imgproc.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_ml.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_objdetect.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_photo.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_shape.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_stitching.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_superres.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_video.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_videoio.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_videostab.so.3.2.0;/usr/local/opencv/opencv320/lib/libopencv_viz.so.3.2.0")与头文件直接写入目录的方式不同,库文件是将文件本身也写入到配置文件中。而OpenCV的库文件有很多种,如下图所示。
所以我们通常都将core、highgui、imgproc等常用的库文件写入到配置文件中,万一遇到要用的函数的库文件没有包含在配置文件中只要手动添加就可以。
-
修改cv_bridge的配置文件结束后,我们只需要在工程的CMakeLists.txt文件中的find_package加入cv_bridge就能够使用自己安装的OpenCV版本。不需要再像(1)方法中那样手动指定路径,而且这样做的好处就是能够使用cv_bridge。因为该包在OpenCV图像与ROS图像之间的转换中会经常用到。
1
find_package(cv_bridge)
这时候再编译工程就可以通过了,但奇怪的是我们发现编译输出OpenCV的版本还是3.3.1并且配置文件还是默认的OpenCV3。
-
在终端下面用gedit打开cv_bridge-extras.cmake文件,对该文件进行修改,把自己安装的OpenCV对应的目录输入到文件中。
1
2cd /opt/ros/kinetic/share/cv_bridge/cmake/
sudo gedit cv_bridge-extras.cmake1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21set(OpenCV_VERSION 3.4.5)
set(OpenCV_VERSION_MAJOR 3)
set(OpenCV_VERSION_MINOR 4)
set(OpenCV_VERSION_PATCH 5)
set(OpenCV_SHARED ON)
set(OpenCV_CONFIG_PATH /usr/local/opencv/opencv345/share/OpenCV)
set(OpenCV_INSTALL_PATH /usr/local/opencv/opencv345)
set(OpenCV_LIB_COMPONENTS opencv_calib3d;opencv_core;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;opencv_viz;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto)
set(OpenCV_USE_MANGLED_PATHS FALSE)
set(OpenCV_MODULES_SUFFIX )
#set(OpenCV_VERSION 3.2.0)
#set(OpenCV_VERSION_MAJOR 3)
#set(OpenCV_VERSION_MINOR 2)
#set(OpenCV_VERSION_PATCH 0)
#set(OpenCV_SHARED ON)
#set(OpenCV_CONFIG_PATH /usr/local/opencv/opencv320/share/OpenCV)
#set(OpenCV_INSTALL_PATH /usr/local/opencv/opencv320)
#set(OpenCV_LIB_COMPONENTS opencv_calib3d;opencv_core;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;opencv_viz;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto)
#set(OpenCV_USE_MANGLED_PATHS FALSE)
#set(OpenCV_MODULES_SUFFIX )再运行catkin_make编译工程就可以发现版本号回到了我们指定的版本号。当然,这只是对于显示设置而已,对于使用OpenCV没有太大的影响。
方法3. cv_bridge与手动指定结合的方式
首先进行第一章安装
的第4、5步。
后,编辑CMakeLists.txt
文件:
-
我们打开自己安装的OpenCV的配置文件可以发现该配置文件主要是用来设置一些OpenCV的相关信息。
-
然后我们只要在cv_bridge中导入OpenCV的配置文件,那么只要使用cv_bridge的包就可以在ROS中使用相关的OpenCV版本。
-
同样从配置文件中得到库函数目录的路径。
上面要注意的是在给系统赋予OpenCV头文件和库文件目录路径时,千万不要忘记把cv_bridge的头文件和库文件目录路径忘记赋给系统。
-
最后把cv_bridge配置文件设置OpenCV参数的选项注释掉。
-
完成。