OpenCV 安装配置
环境
- ubuntu22.04
- opencv4.7.0
1. 安装依赖
通过命令行安装依赖
apt install unzip pkg-config build-essential libgtk-3-dev libcanberra-gtk-module libavcodec-dev libavformat-dev libpng-dev libjpeg-dev libtiff-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libatlas-base-dev gfortran zlib1g zlib1g-dev
1.1 Jasper 依赖
Jasper安装命令
apt install libjasper1 libjasper-dev
若Jasper无法通过命令安装,可按照以下步骤手动安装:
① 下载Jasper源码
https://github.com/jasper-software
可通过镜像网站进行加速(https://ghproxy.com)
wget -O jasper-version-4.0.0.tar.gz https://ghproxy.com/https://github.com/jasper-software/jasper/archive/refs/tags/version-4.0.0.tar.gz
② 解压
tar -zxvf jasper-version-4.0.0.tar.gz
③ 按照源码中的INSTALL.txt完成编译安装
# 配置
cmake -G "Unix Makefiles" -H/opt/jasper-version-4.0.0/ -B/opt/jasper-version-4.0.0/cmakebuild/ -DCMAKE_INSTALL_PREFIX=/usr/local/
# 编译
cmake --build /opt/jasper-version-4.0.0/cmakebuild
# 安装
cmake --build /opt/jasper-version-4.0.0/cmakebuild --target install
1.2 Eigen 依赖
① 下载 Eigen 源码
http://eigen.tuxfamily.org/index.php?title=Main_Page
wget https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz
② 解压
https://github.com/jasper-software
tar -zxvf eigen-3.4.0.tar.gz
③ 配置编译安装
cd eigen-3.4.0
mkdir build
cd build
cmake ..
mmake install
2. 编译安装OpenCV
下载OpenCV和对应版本的opencv_contrib
OpenCV:https://github.com/opencv/opencv
opencv_contrib:https://github.com/opencv/opencv_contrib
2.1 下载源码包
https://ghproxy.com 为代理加速
wget -O opencv4.7.0.tar.gz https://ghproxy.com/https://github.com/opencv/opencv/archive/refs/tags/4.7.0.tar.gz
wget -O opencv_contrib4.7.0.tar.gz https://ghproxy.com/https://github.com/opencv/opencv_contrib/archive/refs/tags/4.7.0.tar.gz
2.2 解压
tar -zxvf opencv4.7.0.tar.gz
tar -zxvf opencv_contrib4.7.0.tar.gz
2.3 配置编译安装
cd opencv4.7.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j16
make install
2.3.1 修改配置选项
执行cmake命令后,可找到opencv-4.7.0/build/CMakeCache.txt 文件来修改OpenCV的具体配置为:
BUILD_JASPER:BOOL=ON
BUILD_JPEG:BOOL=ON
BUILD_OPENJPEG:BOOL=ON
BUILD_PNG:BOOL=ON
BUILD_TBB:BOOL=ON
MKL_WITH_TBB:BOOL=ON
WITH_TBB:BOOL=ON
BUILD_TIFF:BOOL=ON
BUILD_WEBP:BOOL=ON
BUILD_ZLIB:BOOL=ON
BUILD_opencv_js:BOOL=ON
BUILD_opencv_world:BOOL=ON
BUILD_EXAMPLES:BOOL=OFF
BUILD_PERF_TESTS:BOOL=OFF
BUILD_TESTS:BOOL=OFF
EIGEN_INCLUDE_PATH:PATH=/usr/local/include/eigen3/Eigen
OPENCV_DNN_PERF_CAFFE:BOOL=ON
OPENCV_DNN_PERF_CLCAFFE:BOOL=ON
OPENCV_EXTRA_MODULES_PATH:PATH=/opt/opencv_contrib-4.7.0/modules/
OPENCV_FFMPEG_ENABLE_LIBAVDEVICE:BOOL=ON
OPENCV_GENERATE_PKGCONFIG:BOOL=ON
2.3.2 ippicv 问题
cmake过程中可能会提示ippicv下载失败,可通过手动下载来解决:
① 找到文件opencv4.7.0/3rdparty/ippicv/ippicv.cmake
第5行,IPPICV_COMMIT值:a56b6ac6f030c312b2dce17430eef13aed9af274
第16行(按自己平台选择对应参数,APPLE、UNIX、WIN),OPENCV_ICV_NAME:ippicv_2020_lnx_intel64_20191018_general.tgz
第42行,“https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/”
三部分组合得到IPPICV下载地址:https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_lnx_intel64_20191018_general.tgz
② 通过命令将源码包下载至opencv-4.7.0/3rdparty/ippicv路径下
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_lnx_intel64_20191018_general.tgz
③ 修改刚才ippicv.cmake文件的第42行为:
#"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
"file:///opt/opencv-4.7.0/3rdparty/ippicv/"
2.3.3 wechat_qrcode 问题
遇到 wechat_qrcode 资源无法下载时,找到文件opencv_contrib-4.7.0/modules/wechat_qrcode/CMakeLists.txt 的第31行,添加代理加速,修改如下:
"https://ghproxy.com/https://raw.githubusercontent.com/WeChatCV/opencv_3rdparty/$"
2.3.4 xfeatures2d 问题
遇到 xfeatures2d 相关资源无法下载时
-
找到文件 opencv_contrib-4.7.0/modules/xfeatures2d/cmake/download_boostdesc.cmake 的第27行,修改如下:
"https://ghproxy.com/https://raw.githubusercontent.com/opencv/opencv_3rdparty/${OPENCV_3RDPARTY_COMMIT}/"
-
找到文件 opencv_contrib-4.7.0/modules/xfeatures2d/cmake/download_vgg.cmake 的第21行,修改如下:
"https://ghproxy.com/https://raw.githubusercontent.com/opencv/opencv_3rdparty/${OPENCV_3RDPARTY_COMMIT}/"
-
找到文件 opt/opencv_contrib-4.7.0/modules/face/CMakeLists.txt 的第19行,修改如下:
""https://ghproxy.com/https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/"
OK,到这里OpenCV应该可以顺利通过编译并安装了,可以开始干正事了~