参考链接
Anaconda3 — OpenCV with CUDA GPU support for Windows 10 Anaconda3—具有Windows 10的CUDA GPU支持的OpenCV Visual Studio编译OpenCV:无法打开python27_d.lib(python36_d.lib)的问题 protobuf深坑-版本冲突-彻底解决
软件版本
- OpenCV 4.5.5
- opencv_contrib 4.5.5
- cuda 11.6.2
- cuDNN 8.4.0.27
- cmake 3.23.1
- ninja 1.10.2
- Visual Studio 2019
使用conda 安装Python
使用如下命令安装 python 3.8
conda create -n opencv anaconda python=3.8
如果遇到 “PROTOBUF_DEPRECATED was previously defined”问题,则说明protobuf版本冲突(conda 与 opencv),需要卸载conda安装的版本,命令如下:
conda uninstall libprotobuf
Generator使用ninja时不会遇到Release设置不了的情况,可以忽略后面的设置。但是直接使用Visual Studio 16 2019 则会遇到找不到python38_d.lib 文件的问题,需要修改 miniconda3/envs/opencv/include/pyconfig.h 文件,将 python38_d.lib修改为python38.lib, 并将如下所示行注释掉。
opencv 与 opencv_contrib
新建文件夹OpenCV_Build。
下载opencv 和 opencv_contrib,并解压到上述文件夹。
新建一个 set_env_paths.bat 脚本。
ECHO -- Set these variables below --
set "openCvSource=D:/OpenCV_Build/opencv-4.5.5"
set "openCVExtraModules=D:/OpenCV_Build/opencv_contrib-4.5.5/modules"
set "openCvBuild=%openCvSource%/build"
set "toolkitRoot=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6"
ECHO -- Set this to the location of your Conda Env
set "pathToAnaconda=C:/Users/38325/miniconda3/envs/opencv"
ECHO -- Leave these variables alone --
set "buildType=Release"
set "generator=Ninja"
set "pyVer=38"
新建一个 configure_and_build_opencv.bat 脚本。
ECHO -- Starting OpenCV Configuration --
ECHO ---- Opening Visual Studio builder ----
call "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Auxiliary/Build/vcvars64.bat"
ECHO ---- Setting up environment variables ----
call set_env_paths.bat
ECHO ---- Running CMake Commands ----
call "C:/Program Files/CMake/bin/cmake.exe" -B"%openCvBuild%/" -H"%openCvSource%/" -G"%generator%" -DCMAKE_BUILD_TYPE=%buildType% -DOPENCV_EXTRA_MODULES_PATH="%openCVExtraModules%/" ^
-DINSTALL_TESTS=ON -DINSTALL_C_EXAMPLES=ON -DBUILD_EXAMPLES=ON ^
-DBUILD_opencv_world=ON ^
-DWITH_CUDA=ON -DCUDA_TOOLKIT_ROOT_DIR="%toolkitRoot%" -DCUDA_FAST_MATH=ON -DWITH_CUBLAS=ON -DCUDA_ARCH_BIN=8.6 -DWITH_NVCUVID=ON ^
-DWITH_OPENGL=ON -DENABLE_FAST_MATH=ON ^
-DWITH_MFX=ON ^
-DBUILD_opencv_python3=ON -DPYTHON3_INCLUDE_DIR=%pathToAnaconda%/include -DPYTHON3_LIBRARY=%pathToAnaconda%/libs/python%pyVer%.lib -DPYTHON3_EXECUTABLE=%pathToAnaconda%/python.exe -DPYTHON3_NUMPY_INCLUDE_DIRS=%pathToAnaconda%/lib/site-packages/numpy/core/include -DPYTHON3_PACKAGES_PATH=%pathToAnaconda%/Lib/site-packages/ -DOPENCV_SKIP_PYTHON_LOADER=ON
ECHO -- OpenCV Configuration has finished, proceeding to build phase --
call "C:\Program Files\CMake\bin\cmake.exe" --build %openCvBuild% --target install
:End
PAUSE
最终目录如下: opencv-4.5.5/modules/objdetect/test/test_qrcode_encode.cpp 源代码中包含日文,需要将该文件转码后保存,或者直接将日文删除即可。
测试
运行如下命令,
dir "%USERPROFILE%\miniconda3\envs\opencv\Lib\site-packages\cv2*"
如果提示: 则说明安装成功。
测试opencv版本
set path=%openCvBuild%\install\x64\vc16\bin;%path%
python -c "import cv2; print(f'OpenCV: {cv2.__version__} for python installed and working')"
测试CUDA版本
"%openCvBuild%\install\x64\vc16\bin\opencv_perf_cudaarithm.exe" --gtest_filter=Sz_Type_Flags_GEMM.GEMM/29
|