参考:
opencv笔记:ubuntu安装opencv以及多版本共存 | 高深远的博客
https://zhuanlan.zhihu.com/p/604658181
安装不同版本opencv及共存、切换并验证。_pkg-config opencv --modversion-CSDN博客
Ubuntu下多版本OpenCV共存和切换_ubuntu20如同时安装opencv4.5和4.2以及版本的切换-CSDN博客
ubuntu下安装多版本的opencv并且切换使用_ubuntu下可以存在多个版本的opencv马-CSDN博客
参考官方:
主要参考:
在WSL-Ubuntu20.04下,安装OpenCV-3过程以及遇到的问题和相应的解决办法 - LesPlumes - 博客园
我是wsl2-ubuntu18.04,安装ros后已有opencv3.2.0,现在要再装opencv3.3.1
一.下载和安装依赖包
1、首先更新 apt-get,在安装前最好先更新一下系统,不然有可能会安装失败。在终端输入:
sudo apt-get update
sudo apt-get upgrade
2、接着安装官方给的opencv依赖包,在终端输入:
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
下面的步骤我做了:
错误:E: 无法定位软件包 libjasper-dev
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
二.下载源文件
下载opencv-3.3.1,以及opencv_contrib-3.3.1。
opencv-3.3.1可以到官网或者github上去下载源文件(官网版本选择在下方翻页,github上通过tag选择)。
wget -q https://github.com/opencv/opencv/archive/3.3.1.zip
解压源文件并进入
unzip opencv-3.3.1.zip
cd opencv-3.3.1/
(opencv_contrib下载连接:GitHub - opencv/opencv_contrib: Repository for OpenCV's extra modules)
将opencv_contrib-3.3.1文件夹放入opencv-3.3.1文件夹内(我这里的opencv_contrib-3.3.1改名为opencv_contrib)。
三.安装
1.编译前的准备工作
①在进行cmake之前,将“Opencv3缺失文件”这个文件夹中的所有.i文件都复制到
opencv3_contrib\modules\xfeatures2d\src\
这个目录下。
(百度网盘:https://pan.baidu.com/s/15QlHwu5ur2vc6ncyVS4e_Q 提取码:sysu)
②在进行make之前,将以下路径:
opencv-3.3.1\opencv_contrib\modules\xfeatures2d\include\opencv2\
的xfeatures2d文件夹和xfeatures2d.hpp都copy到
opencv-3.3.1\build\opencv2\
路径下。
(opencv_contrib下载连接:https://github.com/opencv/opencv_contrib)
sudo mv opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d build/opencv2
sudo mv opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp build/opencv2
③在进行make之前,下载nvida_sdk(链接:https://pan.baidu.com/s/1IykEbf7SZgf2IE0T52_bqA 提取码:sysu)
然后把下载下来的Video_Codec_Interface_12.1.14\Video_Codec_Interface_12.1.14\Interface\
目录下的三个文件,
都复制到opencv-3.3.1/modules/cudacodec/src/
下;
再依次打开以下文件:
precomp.hpp、video_decoder.hpp、cuvid_video_source.hpp、
frame_queue.hpp、video_parser.hpp
将 #if CUDA_VERSION >= 9000
修改为 #if CUDA_VERSION >= 9000 && CUDA_VERSION < 10000
sudo mv /home/leaf1804/Video_Codec_Interface_12.1.14/Interface/cuviddec.h /home/leaf1804/opencv-3.3.1/modules/cudacodec/src
sudo mv /home/leaf1804/Video_Codec_Interface_12.1.14/Interface/nvcuvid.h /home/leaf1804/opencv-3.3.1/modules/cudacodec/src
sudo mv /home/leaf1804/Video_Codec_Interface_12.1.14/Interface/nvEncodeAPI.h /home/leaf1804/opencv-3.3.1/modules/cudacodec/src
④在进行make之前,打开opencv-3.3.1\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
2.编译opencv
cd opencv-3.3.1
mkdir build && cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/home/leaf1804/software/opencv3.3.1 -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D BUILD_opencv_cudacodec=OFF -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
sudo make -j6
sudo make install
报错1:
运行sudo make -j6的时候报错:
[ 46%] Building NVCC (Device) object modules/cudabgsegm/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_mog2.cu.o /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. /home/leaf1804/opencv-3.3.1/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this. 20 errors detected in the compilation of "/tmp/tmpxft_00006fcc_00000000-13_mog2.compute_70.cpp1.ii". CMake Error at cuda_compile_generated_mog2.cu.o.cmake:264 (message): Error generating file /home/leaf1804/opencv-3.3.1/build/modules/cudabgsegm/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_mog2.cu.o modules/cudabgsegm/CMakeFiles/opencv_cudabgsegm.dir/build.make:89: recipe for target 'modules/cudabgsegm/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_mog2.cu.o' failed make[2]: *** [modules/cudabgsegm/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_mog2.cu.o] Error 1 CMakeFiles/Makefile2:7100: recipe for target 'modules/cudabgsegm/CMakeFiles/opencv_cudabgsegm.dir/all' failed make[1]: *** [modules/cudabgsegm/CMakeFiles/opencv_cudabgsegm.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs.... [ 46%] Building NVCC (Device) object modules/cudaarithm/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_sum.cu.o [ 46%] Building NVCC (Device) object modules/cudaarithm/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_threshold.cu.o /home/leaf1804/opencv-3.3.1/modules/cudaarithm/src/cuda/sub_mat.cu(78): warning: function "<unnamed>::SubOp2::operator()" was declared but never referenced /home/leaf1804/opencv-3.3.1/modules/cudaarithm/src/cuda/sub_mat.cu(97): warning: function "<unnamed>::SubOp4::operator()" was declared but never referenced Scanning dependencies of target opencv_cudaarithm [ 46%] Building CXX object modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/arithm.cpp.o [ 46%] Building CXX object modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/element_operations.cpp.o [ 47%] Building CXX object modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/reductions.cpp.o [ 47%] Building CXX object modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/core.cpp.o cc1plus: warning: /home/leaf1804/opencv-3.3.1/build/modules/cudaarithm/precomp.hpp.gch/opencv_cudaarithm_Release.gch: not used because
OPENCV_TRAITS_ENABLE_DEPRECATED' is defined [-Winvalid-pch]
cc1plus: warning: /home/leaf1804/opencv-3.3.1/build/modules/cudaarithm/precomp.hpp.gch/opencv_cudaarithm_Release.gch: not used becauseOPENCV_TRAITS_ENABLE_DEPRECATED' is defined [-Winvalid-pch]
cc1plus: warning: /home/leaf1804/opencv-3.3.1/build/modules/cudaarithm/precomp.hpp.gch/opencv_cudaarithm_Release.gch: not used becauseOPENCV_TRAITS_ENABLE_DEPRECATED' is defined [-Winvalid-pch]
cc1plus: warning: /home/leaf1804/opencv-3.3.1/build/modules/cudaarithm/precomp.hpp.gch/opencv_cudaarithm_Release.gch: not used becauseOPENCV_TRAITS_ENABLE_DEPRECATED' is defined [-Winvalid-pch]
[ 47%] Linking CXX shared library ../../lib/libopencv_cudaarithm.so [ 47%] Built target opencv_cudaarithm Makefile:181: recipe for target 'all' failed make: *** [all] Error 2
然后修改cmake命令后重新从cmake那一步开始再运行一遍
成功了!!
然后运行sudo make install
成功!!
报错后,可以清理并重新配置
在尝试修复之前,可以确保清理 build
目录以避免遗留问题:
cd /home/leaf/opencv-3.3.1/build
rm -rf *
我还删除过一次opencv-3.3.1和opencv_contrib
文件夹,从头开始再来过一次
四.切换不同版本opencv
打开bashrc文件(~/.bashrc)主目录下 gedit ~/.bashrc
,并加入
# # opencv-3.3.1
# export PKG_CONFIG_PATH=~/software/opencv3.3.1/lib/pkgconfig
# export LD_LIBRARY_PATH=~/software/opencv3.3.1/lib
#OpenCV 3.2.0
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/lib
用那个取消注释另一个,可以使用 pkg-config --modversion opencv
进行验证。
source ~/.bashrc
pkg-config --modversion opencv
# 查看详细的路径:
pkg-config --cflags --libs opencv
使用多版本opencv
在写CmakeList.txt,如果只有一个版本的opencv,我们一般直接使用
FIND_PACKAGE(OpenCV REQUIRED)
如果现在要使用的是默认安装的opencv3.2.0,则使用使用上面的指令就足够了。
如果现在要使用的是我们自己指定路径的opencv-3.3.1,则在上面指令前面加上如下指令:
set(OpenCV_DIR "/home/wh/opencv4/lib/cmake/opencv4"):引号里面的地址是OpencvConfig.cmake地址
OpenCVConfig.cmake 是 OpenCV 库的 配置文件。它是用来告诉项目如何使用 OpenCV 库的。它包含了 OpenCV 库的路径信息、版本信息和链接信息等。当项目使用 find_package(OpenCV) 命令时,CMake 就会去寻找这个文件,并读取其中的信息,最终使项目可以使用 OpenCV 库。
比如我的就要在原来的cmakelist.txt里面加上一句
set(OpenCV_DIR "/home/leaf1804/software/opencv3.3.1/share/OpenCV"),因为我的OpencvConfig.cmake地址是这里