系统版本:ubuntu18.04
库版本:opencv4.6.0 &?opencv3.6.0
sudo apt-get purge libopencv*
首先进入原先编译opencv的build目录,在该目录下打开终端;再执行以下代码。(注意如果build已经删除了,可以重新编译一遍该版本的opencv,然后在最后一步执行sudo make uninstall)
sudo make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
以上可能有error,可忽略,最后执行如下操作:?
sudo apt-get --purge remove opencv-doc opencv-data python-opencv
链接1:?GitHub - opencv/opencv_contrib at 3.4.15
链接2:Releases - OpenCV?
注意:这里要与上面下载的opencv版本一致
?链接:
GitHub - opencv/opencv_contrib at 3.4.15
2.3.1? 下载好后,将opencv_contrib放到解压后opencv文件夹里面
2.3.2 安装依赖项
sudo apt-get update
sudo apt-get upgrade
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
2.3.3 cmake编译
2.3.3.1 在该文件夹opencv-3.4.15下打开终端
后续步骤参见下文3.3.3节
链接1:GitHub - opencv/opencv at 4.6.0
注意:这里要与上面下载的opencv版本一致
链接:https://github.com/opencv/opencv_contrib/tree/4.6.0?
3.3.1 两个都下载好以后,把opencv_? opencv_contrib放到解压后opencv文件夹里面
3.3.2 安装依赖
sudo apt-get update
sudo apt-get upgrade
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
3.3.3 cmake编译
3.3.3.1 在该文件夹opencv-4.6.0下打开终端
mkdir build && cd build
(1)?不编译opencv_contrib、cuda
注意:CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.6.0
cmake -D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.6.0 -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_ENABLE_NONFREE=True ..
注解:
1. CMAKE_INSTALL_PREFIX 是opencv的安装地址 默认安装在 usr/local
2. CMAKE_BUILD_TYPE 是opencv安装的版本,Release和Debug两种可选,默认安装Release
3. OPENCV_ENABLE_NONFREE 是否使用部分被申请了专利的算方法 这里选True的话就可以使用了
4. OPENCV_GENERATE_PKGCONFIG 强烈建议开启这个 设置为ON OPENCV_GENERATE_PKGCONFIG 因为opencv4默认不生成.pc文件,所以加上这句用于生成opencv4.pc文件,支持pkg-config功能。opencv4版本及以上 这里用ON.?
(2)?编译opencv_contrib,不编译cuda
注意:CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.6.0
OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.6.0/modules?
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.6.0 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.6.0/modules \
-D HAVE_opencv_python3=ON \
-D PYTHON_EXECUTABLE=usr/bin/python3 \
-D BUILD_EXAMPLES=ON ..
cmake -DCMAKE_BUILD_TYPE=RELEASE -DOPENCV_EXTRA_MODULES_PATH= ../opencv_contrib-4.6.0/modules -DCMAKE_INSTALL_PREFIX=/usr/local/opencv-4.6.0 -DWITH_CUDA=OFF -DBUILD_EXAMPLES=ON -DBUILD_DOCS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DOPENCV_GENERATE_PKGCONFIG=YES -DOPENCV_ENABLE_NONFREE=True -DBUILD_opencv_world=OFF ..
可能出现的错误:
ippicv_2020_win_intel64_20191018_general.zip等文件下载失败的解决办法,找了好久,还是这位老哥的方法解决了问题-CSDN博客
(3)?编译opencv_contrib、cuda
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.6.0 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D CUDA_ARCH_BIN=7.5 \
-D WITH_CUBLAS=1 \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.6.0/modules \
-D HAVE_opencv_python3=ON \
-D PYTHON_EXECUTABLE=usr/bin/python3 \
-D BUILD_EXAMPLES=ON ..
从上,WITH_CUDA和WITH_CUDNN项指的是用CUDA和cuDNN。OPENCV_DNN_CUDA项表示我们要编译后的dnn模块支持CUDA。ENABLE_FAST_MATH, CUDA_FAST_MATH和WITH_CUBLAS项是为了优化运算而开启的。
?3.3.3.2?cmake结束后make
注:终端输入nproc
?命令可以查看自己电脑有多少线程。我的电脑有8个。
sudo make -j8
3.3.3.3 安装指令
sudo make install
3.3.3.4 配置环境
sudo gedit /etc/ld.so.conf.d/opencv.conf
新增 /usr/local/opencv-4.6.0/lib
保存后终端输入
sudo ldconfig
更新共享链接库
sudo gedit /etc/bash.bashrc
在文末新增:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv-4.6.0/lib/pkgconfig
export PKG_CONFIG_PATH
保存后退出,终端输入以下命令使配置生效
source /etc/bash.bashrc
更新一下
sudo updatedb
查看是否安装成功,成功则会输出版本号?
pkg-config --modversion opencv
四、配置菜单选项
打开终端,输入以下指令
sudo update-alternatives --install /usr/local/opencv opencv /usr/local/opencv-3.4.15/ 3415
sudo update-alternatives --install /usr/local/opencv opencv /usr/local/opencv-4.6.0/ 4600
切换指令
sudo update-alternatives --config opencv
?