首先安装一些依赖
sudo apt-get install -y build-essential checkinstall cmake pkg-config yasm git gfortran
# 添加源,防止可能会出现的找不到所需安装的库的情况
sudo add-apt-repository -y "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get update
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk2.0-dev libtbb-dev libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils
安装Python3支持需要的库
sudo apt install python3-dev python3-numpy
安装一些可选库
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev
opencv4.4下载:https://github.com/opencv/opencv/releases/tag/4.4.0
解压后,将opencv_contrib-4.4.0放入opencv-4.4.0的文件夹内。
cd opencv-4.4.0
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=RELEASE
-DCMAKE_INSTALL_PREFIX=/usr/local
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.4.0/modules ..
-DWITH_CUDA=1
-DENABLE_FAST_MATH=1
-DCUDA_FAST_MATH=1
-DWITH_CUBLAS=1
-DOPENCV_GENERATE_PKGCONFIG=1
-DCUDA_GENERATION=Pascal ..
-DPYTHON_DEFAULT_EXECUTABLE=/home/xxx/anaconda3/envs/py3.7/bin/python3 #路径要根据自己的实际情况改变
-DBUILD_opencv_python3=ON
-DBUILD_opencv_python2=OFF
-DPYTHON3_EXECUTABLE=/home/xxx/anaconda3/envs/py3.7/bin/python3 ##路径要根据自己的实际情况改变
-DPYTHON3_INCLUDE_DIR=/home/xx/anaconda3/envs/py3.7/include/python3.7m #路径要根据自己的实际情况改变
-DPYTHON3_LIBRARY=/home/xxx/anaconda3/envs/py3.7/lib/libpython3.7m.so.1 #路径要根据自己的实际情况改变
-DPYTHON_NUMPY_PATH=/home/xxx/anaconda3/envs/py3.7/lib/python3.7/site-packages #路径要根据自己的实际情况改变
-DINSTALL_PYTHON_EXAMPLES=ON
-DPYTHON3_PACKAGESS_PATH=/home/xxx/anaconda3/envs/py3.7/lib/python3.7/site-packages #路径要根据自己的实际情况改变
注意事项1:
IPPICV下载失败,查看CMakeDownloadLog.txt 获取下载地址https://raw.githubusercontent.com/opencv/opencv_3rdparty/a56b6ac6f030c312b2dce17430eef13aed9af274/ippicv/ippicv_2020_lnx_intel64_20191018_general.tgz
使用迅雷下载,将下好的.tgz文件放在opencv-4.4.0文件夹内,并修改/3rdparty/ippicv/ippicv.cmake的43行:
-
"file:///home/xx/opencv-4.4.0/"
-
#"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
注意事项2:
可能还会出现缺少boostdesc_bgm.i 等等文件,下面链接百度云盘下载这个文件后,放入到"opencv_contrib-4.4.0modulesxfeatures2dsrc"目录下
注意事项3
避免编译失败,找不到对应文件。
将opencv-4.4.0/modules/features2d拷贝到build目录下
sudo make -j4
sudo make install
最后一步
将/opencv/build/lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so复~/anaconda3/lib/python3.7/site-packages/文件夹中 #根据自己的实际情况进行修改
百度云链接
链接: https://pan.baidu.com/s/1w0oPJEftVjzbNlir6uKyhQ 密码: fln0