[TOC]

背景

为了在某个conda环境使用带有cuda支持的opencv,手动编译

环境

ubuntu+cuda+conda+cmake

激活conda环境

conda create --name track python=3.6.9
conda activate track

建议安装上numpy,编译的opencv-python会有依赖

conda install numpy

下载

下载opencv和opencv_contrib包,并解压

wget https://github.com/opencv/opencv/archive/refs/tags/4.6.0.zip
wget https://github.com/opencv/opencv_contrib/archive/refs/tags/4.6.0.zip
mv 4.6.0.zip opencv_4.6.0.zip
mv 4.6.0.zip.1 opencv_contrib_4.6.0.zip
unzip opencv_4.6.0.zip
unzip opencv_contrib_4.6.0.zip

建立目录

cd opencv-4.6.0
mkdir build
cd build

编译

cmake

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.6.0/modules \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_OPENMP=ON \
-D WITH_TBB=ON \
-D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D CMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") \
-D PYTHON3_EXECUTABLE=$(which python) \
-D PYTHON3_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
..

编译安装

make
make install

参考

https://zhuanlan.zhihu.com/p/147595002