多版本的安装
对于多个版本都安装到了显式指定的路径上,可以参考文章2进行设置;若是有一种版本常用的,则将其安装到默认目录/usr/local
下,而对于另一种新安装的,则显式指定安装目录CMAKE -D CMAKE_INSTALL_PREFIX=/home/xxx/opencv-x.x.x/build/installed
,见参考文章1
多版本切换使用
安装到/usr/local
的版本无需额外增加环境变量,都可以找到。
安装到指定目录下的OpenCV,则需要添加环境变量来使得系统知道去哪里找。具体做法:
- 打开~/.bashrc
- 添加如下内容
export PKG_CONFIG_PATH=/home/xxx/opencv-x.x.x/build/installed/lib/pkgconfig
export LD_LIBRARY_PATH=/home/xxx/opencv-x.x.x/build/installed/lib
source ~/.bashrc
更新使之生效- 在项目中的CMakeLists.txt中,要找到对应版本的OpenCVConfig.cmake文件,示例如下:
set(OpenCV_DIR "/home/xxx/opencv-x.x.x/build" #这里不指定则从默认的目录里如/usr/local去找
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:") #以下是输出对应的版本信息,以便确认为所需版本
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
附录之OpenCV_contrib模块安装
由于要使用nonfree模块,需要安装contrib套件,安装过程遇到许多麻烦,遂记录之。
nonfree modules安装不完整的问题
编译安装时最终都通过了,却发现如sift, surf都还是不能用。查看cmake输出发现其中有unavailable的模块,问题应该是出在这里:
-- OpenCV modules:
-- To be built: alphamat aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab viz xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv java js julia matlab ovis python3 sfm xfeatures2d
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: YES
--
继续向上查看,发现有如下的报错:
-- xfeatures2d/vgg: Download: vgg_generated_80.i
-- Try 1 failed
--
=======================================================================
Couldn't connect to server from the Internet.
Perhaps direct connections are not allowed in the current network.
To use proxy please check/specify these environment variables:
- http_proxy/https_proxy
- and/or HTTP_PROXY/HTTPS_PROXY
=======================================================================
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/vgg: Download failed: 7;"Couldn't connect to server"
For details please refer to the download log file:
/home/wrx/opencv-4.4.0/build/CMakeDownloadLog.txt
Call Stack (most recent call first):
opencv_contrib-4.4.0/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
opencv_contrib-4.4.0/modules/xfeatures2d/CMakeLists.txt:12 (download_vgg_descriptors)
似乎是下载vgg_generated_80.i失败,像这样的还有face_landmark_model.dat、ippicv_2020_lnx_intel64_20191018_general.tgz、boostdesc_bgm.i、boostdesc_bgm_hd.i、boostdesc_bgm_bi.i、boostdesc_lbgm.i、vgg_generated_120.i、 vgg_generated_48.i、vgg_generated_64.i、boostdesc_binboost_064.i、boostdesc_binboost_128.i、boostdesc_binboost_256.i
在log中所言,参考/home/wrx/opencv-4.4.0/build/CMakeDownloadLog.txt
去查看细节,打开该日志发现提供了下载地址
#use_cache "/home/wrx/opencv-4.4.0/.cache"
#do_unpack "ippicv_2020_lnx_intel64_20191018_general.tgz" "7421de0095c7a39162ae13a6098782f9" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_lnx_intel64_20191018_general.tgz" "/home/wrx/opencv-4.4.0/build/3rdparty/ippicv"
#check_md5 "/home/wrx/opencv-4.4.0/.cache/ippicv/7421de0095c7a39162ae13a6098782f9-ippicv_2020_lnx_intel64_20191018_general.tgz"
#mismatch_md5 "/home/wrx/opencv-4.4.0/.cache/ippicv/7421de0095c7a39162ae13a6098782f9-ippicv_2020_lnx_intel64_20191018_general.tgz" "d41d8cd98f00b204e9800998ecf8427e"
#delete "/home/wrx/opencv-4.4.0/.cache/ippicv/7421de0095c7a39162ae13a6098782f9-ippicv_2020_lnx_intel64_20191018_general.tgz"
#cmake_download "/home/wrx/opencv-4.4.0/.cache/ippicv/7421de0095c7a39162ae13a6098782f9-ippicv_2020_lnx_intel64_20191018_general.tgz" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_lnx_intel64_20191018_general.tgz"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_ADE_HASH_3rdparty_ade_v0_1_1f_zip"
#do_copy "boostdesc_bgm.i" "0ea90e7a8f3f7876d450e4149c97c74f" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i" "/home/wrx/opencv-4.4.0/build/downloads/xfeatures2d"
#missing "/home/wrx/opencv-4.4.0/build/downloads/xfeatures2d/boostdesc_bgm.i"
#check_md5 "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i"
#mismatch_md5 "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i" "d41d8cd98f00b204e9800998ecf8427e"
#delete "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i"
#cmake_download "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i"
#do_copy "boostdesc_bgm_bi.i" "232c966b13651bd0e46a1497b0852191" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i" "/home/wrx/opencv-4.4.0/build/downloads/xfeatures2d"
#missing "/home/wrx/opencv-4.4.0/build/downloads/xfeatures2d/boostdesc_bgm_bi.i"
#check_md5 "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i"
#mismatch_md5 "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i" "d41d8cd98f00b204e9800998ecf8427e"
#delete "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i"
#cmake_download "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i"
#do_copy "boostdesc_bgm_hd.i" "324426a24fa56ad9c5b8e3e0b3e5303e" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i" "/home/wrx/opencv-4.4.0/build/downloads/xfeatures2d"
#missing "/home/wrx/opencv-4.4.0/build/downloads/xfeatures2d/boostdesc_bgm_hd.i"
#check_md5 "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i"
#mismatch_md5 "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i" "d41d8cd98f00b204e9800998ecf8427e"
#delete "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i"
#cmake_download "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i"
#do_copy "boostdesc_binboost_064.i" "202e1b3e9fec871b04da31f7f016679f" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i" "/home/wrx/opencv-4.4.0/build/downloads/xfeatures2d"
#missing "/home/wrx/opencv-4.4.0/build/downloads/xfeatures2d/boostdesc_binboost_064.i"
#check_md5 "/home/wrx/opencv-4.4.0/.cache/xfeatures2d/boostdesc/202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i"
网络原因连接不上某些下载项,自己想办法事先下好吧~下载完毕后,这里尝试了几种方式来使得文件能替代。
1.参考1
对ippicv默认安装源修改,打开源文件:/home/xxx/opencv-x.x.x/3rdparty/ippicv/ippicv.cmake,将其中
https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}ippicv/
替换为file:~/path/to/ippicv/
类似的,对face_landmark_model的opencv默认安装源进行修改:
在源文件中打开:/home/xxx/opencv-x.x.x/opencv_contrib/modules/face/CMakeLinks.txt
,将其中的
https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/
替换为file:~/Downloads/
这种方法对这两个文件的识别有效。
但对vgg的源/home/xxx/opencv-x.x.x/opencv_contrib/modules/xfeatures2d/cmake/download_vgg.cmake
进行同样的修改无效!
且文中及这篇文章中说的将vgg等文件放到/home/xxx/opencv-x.x.x/opencv_contrib/modules/xfeatures2d/src/
目录下的方法都无效!
2. 参考2
将各文件按[hash码-文件名]格式保存到对应的.cache文件夹内覆盖掉原先0kb的文件。
其放置于/home/xxx/opencv-x.x.x/.cache/xfeatures2d/vgg/
文件夹内,最终成功搞定了xfeatures模块,编译安装日志如下:
wrx@wrx-m15-r3:~/opencv-4.4.0/buildnew$ cmake -DCMAKE_INSTALL_PREFIX=/home/wrx/opencv-4.4.0/buildnew/installed -DOPENCV_EXTRA_MODULES_PATH=/home/wrx/opencv-4.4.0/opencv_contrib-4.4.0/modules /home/wrx/opencv-4.4.0/ ..
-- Detected processor: x86_64
-- Looking for ccache - not found
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found suitable version "1.2.11", minimum required is "1.2.3")
-- The imported target "openjp2_static" references the file
"/usr/lib/x86_64-linux-gnu/libopenjp2.a"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
"/usr/lib/x86_64-linux-gnu/openjpeg-2.3/OpenJPEGTargets.cmake"
but not all the files it references.
-- The imported target "openjpip" references the file
"/usr/lib/x86_64-linux-gnu/libopenjpip.so.2.3.0"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
"/usr/lib/x86_64-linux-gnu/openjpeg-2.3/OpenJPEGTargets.cmake"
but not all the files it references.
-- The imported target "openjpip_server" references the file
"/usr/lib/x86_64-linux-gnu/libopenjpip_server.a"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
"/usr/lib/x86_64-linux-gnu/openjpeg-2.3/OpenJPEGTargets.cmake"
but not all the files it references.
-- The imported target "opj_decompress" references the file
"/usr/bin/opj_decompress"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
"/usr/lib/x86_64-linux-gnu/openjpeg-2.3/OpenJPEGTargets.cmake"
but not all the files it references.
-- The imported target "opj_compress" references the file
"/usr/bin/opj_