首先确保安装了cmake工具
安装opencv依赖文件
sudo apt-get install build-essential sudo apt-get install git libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
由于本电脑之间装过OpenCV,所以不确定还需要哪些库,如果安装中遇到问题,在寻找解决方法。
下载OpenCV-3.4.19和OpenCV_contrib-3.4.19
链接和截图如下:
Release 3.4.19 · opencv/opencv_contrib · GitHub
Release 3.4.19 · opencv/opencv · GitHub
解压下载的文件
tar -zxvf opencv-3.4.19.tar.gz
tar -zxvf opencv_contrib-3.4.19.tar.gz
将opencv_contrib-3.4.19.tar.gz解压之后的文件夹opencv_contrib-3.4.19复制到opencv-3.4.19.tar.gz解压之后的文件夹opencv-3.4.19中,并将opencv_contrib-3.4.19改名为opencv_contrib,如图所示:
执行如下命令:
cd opencv-3.4.19/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
这个过程可能需要下载东西,似乎需要魔法。
执行编译
make -j$(nproc)
安装
sudo make install
配置环境
sudo gedit /etc/ld.so.conf.d/OpenCV.conf
打开是一个空文档,写入/usr/local/lib
sudo ldconfig
如果运行SURF检测报错如下:
当你运行要使用SURF的时候,就会报错 The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'create'
那么请将cmake修改如下,添加OPENCV_ENABLE_NONFREE=ON
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D OPENCV_ENABLE_NONFREE=ON ..
重新按照上面的流程,再走一遍。
参考:Ubuntu18.04安装opencv和opencv_contrib(SIFT,SURF,ORB使用) - 知乎
参考:ubuntu20编译opencv4.5.4+opencv_contrib4.5.4记录-CSDN博客