一、opencv4.8.0 ubuntu22.04上编译:
用cmake进行编译,需要配置三次。选中world选项,输入opencv_contrib_module路径。
ubuntu22.04上编译:
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D OPENCV_PYTHON3_VERSION=3.7 \
-D PYTHON_EXECUTABLE=/home/shiwei/anaconda3/bin/python3 \
-D PYTHON_DEFAULT_EXECUTABLE=/home/shiwei/anaconda3/bin/python3 \
-D PYTHON3_EXECUTABLE=/home/shiwei/anaconda3/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/home/shiwei/anaconda3/include/python3.7m \
-D PYTHON3_LIBRARY=/home/shiwei/anaconda3/lib/libpython3.7m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/shiwei/anaconda3/lib/python3.7/site-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/home/shiwei/anaconda3/lib/python3.7/site-packages \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/shiwei/anaconda3/lib/python3.7/site-packages \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_TIFF=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/shiwei/Downloads/opencv_contrib-4.7.0/modules \
-D BUILD_EXAMPLES=OFF \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D WITH_CUBLAS=ON \
-D CUDA_ARCH_BIN=7.5 \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D HAVE_opencv_python3=ON \
-D BUILD_opencv_xfeatures2d=OFF \
-D BUILD_opencv_wechat_qrcode=OFF \
-D BUILD_opencv_face=OFF \
-D BUILD_opencv_world=ON \
-D BUILD_JAVA=OFF \
-D OPENCV_DNN_OPENCL=ON \
-D WITH_OPENCL=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
..
二、DNN GPU版
需要勾选camke里面和cuda相关的选项,以及注意第三次配置的时候的gpu计算能力,否则可能会报错。Linux平台最好用命令行的方式编译。
勾选WITH_CUDA、OPENCV_DNN_CUDA、BUILD_STATUS
CUDA_FAST_MATH
可以参考csdn上的说明,三次配置后BUILD_ALL即可。
三、opencv-python-gpu版
在(二)的基础上需要勾选 BUILD_opencv_python3选项,然后再生产的lib里面有cv2.cp38-win_amd64.pyd文件即可。
验证gpu功能:
import cv2
print(cv2.cuda.getCudaEnabledDeviceCount())
dir(cv2.cuda)
import cv2 from cv2 import cudacuda.printCudaDeviceInfo(0)
注意;
ubuntu上编译py-opencv会出现各种各样的问题,如:
ImportError: librsvg-2.so.2: cannot open shared object file: No such file or directory
解决方法: sudo apt install librsvg2-2 librsvg2-dev librsvg2-common
ImportError: libavcodec.so.58: cannot open shared object file: No such file or directory
解决方法:
sudo apt-get install aptitude
sudo aptitude install libavcodec58
sudo aptitude install libavformat58
sudo aptitude install libswscale5
ImportError: libIlmImf-2_5.so.25: cannot open shared object file: No such file or directory
解决方法:
sudo apt-get install libopenexr-dev
or
sudo apt-get install libopenexr-dev
ImportError: libdc1394.so.25: cannot open shared object file: No such file or directory
解决方法:
sudo apt-get install libdc1394-dev libdc1394-utils
ImportError: /lib/x86_64-linux-gnu/librsvg-2.so.2: undefined symbol: pango_attr_insert_hyphens_new
解决方法:
conda install -c anaconda cairo==1.16.0
RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
ImportError: numpy.core.multiarray failed to import
解决方法:
pip install numpy==1.20.1