win11 Visual Studio 17 2022源码编译 opencv4.11.0 + cuda12.6.3 启用GPU加速
win11 Visual Studio 17 2022 源码编译 opencv4.11.0 + cuda12.6.3 启用GPU加速
配置:
生成 opencv
生成 opencv-python
1 下载源码和安装软件
win11 x64 系统
-
安装Visual Studio 17 2022
-
下载opencv4.11.0 源码
https://github.com/opencv/opencv/releases/tag/4.11.0
下载 Source code(zip) ,解压到H:\workspace\opencv-4.11.0
-
下载opencv_contrib源码
https://github.com/opencv/opencv_contrib/releases/tag/4.11.0
下载 Source code(zip) ,解压到H:\workspace\opencv-4.11.0\modules\opencv_contrib
-
下载cuda12.6.3 : 安装到
D:\software\nvidia\cuda12.6
https://developer.nvidia.com/cuda-12-6-3-download-archive
参考文档
https://docs.nvidia.com/cuda/archive/12.6.3/cuda-installation-guide-microsoft-windows/index.html -
下载 cudnn 对应cuda12版本
https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/windows-x86_64/cudnn-windows-x86_64-9.7.1.26_cuda12-archive.zip
解压到D:\software\nvidia\cudnn-9.7.1.26_cu12
,把此路径下 bin include lib
复制到cuda12.6路径下D:\software\nvidia\cuda12.6
-
下载安装 ffmpeg 最新版本
安装到D:\software\ffmpeg-gpl-shared
-
cmake version 3.25.2
安装到C:\software\CMake\bin
-
下载安装python3.12.9
安装到D:\software\Python\Python312
2 配置环境变量
CUDA_PATH=D:\software\nvidia\cuda12.6
FFMPRG_ROOT=D:\software\ffmpeg-gpl-shared
PATH=D:\software\nvidia\cuda12.6\bin;D:\software\nvidia\cuda12.6\libnvvp;%FFMPEG_ROOT%\bin;%FFMPEG_ROOT%\lib;C:\software\CMake\bin;D:\software\Python\Python312\;D:\software\Python\Python312\Scripts\;
3 源码修改下载地址
用 vscode 打开 源码 H:\workspace\opencv-4.11.0
,搜索替换
把 raw.githubusercontent.com
换成raw.staticdn.net
4 cmake 配置
使用我配置好的脚本直接运行,根据自己的路径修改
wmx_build.ps1 脚本:
# 设置 CUDA 路径
$env:CUDA_PATH = "D:\software\nvidia\cuda12.6"
# 添加 Python 路径
$env:PYTHON3_BASE = "D:/software/Python/Python312"
$env:PYTHON3_EXECUTABLE = "$env:PYTHON3_BASE/python.exe"
$env:PYTHON3_INCLUDE_DIR = "$env:PYTHON3_BASE/include"
$env:PYTHON3_LIBRARY = "$env:PYTHON3_BASE/libs/python312.lib"
$env:PYTHON3_NUMPY_INCLUDE_DIRS = "$env:PYTHON3_BASE/Lib/site-packages/numpy/_core/include"
$env:PYTHON3_PACKAGES_PATH = "$env:PYTHON3_BASE/Lib/site-packages"
# 切换到工作路径
$BUILD_DIR = "H:\workspace\opencv-4.11.0\build"
if (-Not (Test-Path $BUILD_DIR)) {
mkdir $BUILD_DIR
}
cd $BUILD_DIR
# Set-Location -Path $BUILD_DIR
echo "------------- Current directory is now: $(Get-Location)"
# 编译线程数量
$env:THREAD_COUNT = 32
# 安装opencv到路径
$env:INSTALL_PREFIX="D:\software\opencv\4.11.0"
# ========== CMake 配置 ==========
cmake -G "Visual Studio 17 2022" `
-D CMAKE_INSTALL_PREFIX=$env:INSTALL_PREFIX `
-D OPENCV_EXTRA_MODULES_PATH="H:\workspace\opencv-4.11.0\modules\opencv_contrib\modules" `
-D CMAKE_BUILD_TYPE=RELEASE `
-D CMAKE_CXX_FLAGS="/MP" ` # vs多线程编译
-D OPENCV_GENERATE_PKGCONFIG=YES `
-D WITH_CUDA=ON `
-D WITH_NVCUVID=OFF `
-D WITH_NVENC=OFF `
-D CUDA_NVCC_FLAGS="-D_FORCE_INLINES --threads=32" ` # nvcc 启用多线程编译
-D CUDA_FAST_MATH=ON `
-D CUDA_ARCH_BIN="8.9" ` # 我是4070tisuper ,GPU计算能力 : https://developer.nvidia.com/cuda-gpus
-D CUDNN_INCLUDE_DIR="$env:CUDA_PATH/include" `
-D CUDNN_LIBRARY="$env:CUDA_PATH/lib/x64/cudnn.lib" `
-D WITH_CUDNN=ON `
-D OPENCV_DNN_CUDA=ON `
-D BUILD_opencv_world=ON `
-D WITH_CUBLAS=ON `
-D BUILD_opencv_python3=ON `
-D BUILD_opencv_python_bindings_generator=ON `
-D OPENCV_FORCE_PYTHON_LIBS=ON `
-D PYTHON3_LIMITED_API=OFF `
-D OPENCV_SKIP_PYTHON_LOADER=OFF `
-D OPENCV_PYTHON3_INSTALL_PATH="$env:PYTHON3_PACKAGES_PATH" `
-D PYTHON3_EXECUTABLE="$env:PYTHON3_EXECUTABLE" `
-D PYTHON3_INCLUDE_DIR="$env:PYTHON3_INCLUDE_DIR" `
-D PYTHON3_LIBRARY="$env:PYTHON3_LIBRARY" `
-D PYTHON3_NUMPY_INCLUDE_DIRS="$env:PYTHON3_NUMPY_INCLUDE_DIRS" `
-D ENABLE_FAST_MATH=ON `
..
Set-Location -Path $BUILD_DIR
echo "------------- Current directory is now: $(Get-Location)"
# ========== 构建指令 ==========
echo "--------------Compile with CMake------------"
echo "cmake --build . --config Release --target ALL_BUILD -j 32"
echo "--------------Compile with MSBuild----------"
echo "msbuild OpenCV.sln -p:Configuration=Release -p:Platform=x64 -m:32"
# ========== 安装 ==========
echo "--------------Then install-------------------"
echo "cmake --install ./"
打开Developer PowerShell for VS 2022
把 wmx_build.ps1 脚本 拖入窗口,执行
或者打开cmake-gui ,根据需要配置,比如去掉各种test等等
输出:
**********************************************************************
** Visual Studio 2022 Developer PowerShell v17.5.4
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
(anaconda3)PS C:\Users\admin\source\repos> H:\workspace\opencv-4.11.0\wmx_build.ps1
------------- Current directory is now: H:\workspace\opencv-4.11.0\build
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22631.
-- ocv_init_download: OpenCV source tree is not fetched as git repository. 3rdparty resources will be downloaded from github.com by default.
-- Detected processor: AMD64
-- Could NOT find AVIF (missing: AVIF_LIBRARY AVIF_INCLUDE_DIR)
-- libjpeg-turbo: VERSION = 3.0.3, BUILD = opencv-4.11.0-libjpeg-turbo
-- CMAKE_ASM_NASM_COMPILER = D:/software/strawberry/c/bin/nasm.exe
-- CMAKE_ASM_NASM_OBJECT_FORMAT = win64
-- CMAKE_ASM_NASM_FLAGS = -DWIN64 -D__x86_64__
-- SIMD extensions: x86_64 (WITH_SIMD = 1)
-- Could NOT find OpenJPEG (minimal suitable version: 2.0, recommended version >= 2.3.1). OpenJPEG will be built from sources
-- OpenJPEG: VERSION = 2.5.0, BUILD = opencv-4.11.0-openjp2-2.5.0
-- OpenJPEG libraries will be built from sources: libopenjp2 (version "2.5.0")
-- found Intel IPP (ICV version): 2021.12.0 [2021.12.0]
-- at: H:/workspace/opencv-4.11.0/build/3rdparty/ippicv/ippicv_win/icv
-- found Intel IPP Integration Wrappers sources: 2021.12.0
-- at: H:/workspace/opencv-4.11.0/build/3rdparty/ippicv/ippicv_win/iw
-- Found NVCUVENC: D:/software/nvidia/cuda12.6/lib/x64/nvencodeapi.lib
-- CUDA detected: 12.6
-- CUDA: Using CUDA_ARCH_BIN=8.9
-- CUDA: NVCC target flags -D_FORCE_INLINES --threads=32;-gencode;arch=compute_89,code=sm_89;-D_FORCE_INLINES
-- CUDA: MSVS generator is detected. Disabling CMake re-run checks (CMAKE_SUPPRESS_REGENERATION=ON). You need to run CMake manually if updates are required.
-- Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
-- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
-- Could NOT find BLAS (missing: BLAS_LIBRARIES)
-- Could NOT find LAPACK (missing: LAPACK_LIBRARIES)
Reason given by package: LAPACK could not be found because dependency BLAS could not be found.
-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
-- Module opencv_alphamat disabled because the following dependencies are not found: Eigen
-- freetype2: NO
-- harfbuzz: NO
-- Julia not found. Not compiling Julia Bindings.
-- Module opencv_ovis disabled because OGRE3D was not found
-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
-- Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.
-- Failed to find gflags - Could not find gflags include directory, set GFLAGS_INCLUDE_DIR to directory containing gflags/gflags.h
-- Failed to find glog - Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h
-- Module opencv_sfm disabled because the following dependencies are not found: Eigen Glog/Gflags
-- Tesseract: NO
-- Processing WORLD modules...
-- module opencv_cudev...
-- module opencv_core...
-- Allocator metrics storage type: 'long long'
-- Excluding from source files list: <BUILD>/modules/world/test/test_intrin256.lasx.cpp
-- module opencv_cudaarithm...
-- module opencv_flann...
-- module opencv_hdf...
-- module opencv_imgproc...
-- module opencv_intensity_transform...
-- module opencv_ml...
-- module opencv_phase_unwrapping...
-- module opencv_plot...
-- module opencv_quality...
-- module opencv_reg...
-- module opencv_signal...
-- module opencv_surface_matching...
-- module opencv_cudafilters...
-- module opencv_cudaimgproc...
-- module opencv_cudawarping...
-- module opencv_dnn...
-- Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': H:/workspace/opencv-4.11.0/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
-- module opencv_dnn_superres...
-- module opencv_features2d...
-- module opencv_fuzzy...
-- module opencv_hfs...
-- module opencv_imgcodecs...
-- imgcodecs: OpenEXR codec is disabled in runtime. Details: https://github.com/opencv/opencv/issues/21326
-- module opencv_line_descriptor...
-- module opencv_photo...
-- module opencv_saliency...
-- module opencv_text...
-- module opencv_videoio...
-- module opencv_xphoto...
-- module opencv_calib3d...
-- module opencv_cudafeatures2d...
-- module opencv_cudastereo...
-- module opencv_datasets...
-- module opencv_highgui...
-- highgui: using builtin backend: WIN32UI
-- module opencv_mcc...
-- module opencv_objdetect...
-- module opencv_rapid...
-- module opencv_rgbd...
-- rgbd: Eigen support is disabled. Eigen is Required for Posegraph optimization
-- module opencv_shape...
-- module opencv_structured_light...
-- module opencv_video...
-- module opencv_xfeatures2d...
-- module opencv_ximgproc...
-- module opencv_xobjdetect...
-- module opencv_aruco...
-- module opencv_bgsegm...
-- module opencv_bioinspired...
-- module opencv_ccalib...
-- module opencv_cudabgsegm...
-- module opencv_cudalegacy...
-- module opencv_cudaobjdetect...
-- module opencv_dnn_objdetect...
-- module opencv_dpm...
-- module opencv_face...
-- module opencv_gapi...
-- module opencv_optflow...
-- module opencv_stitching...
-- module opencv_tracking...
-- module opencv_cudaoptflow...
-- Building with NVIDIA Optical Flow API 2.0
-- module opencv_stereo...
-- module opencv_superres...
-- module opencv_videostab...
-- Processing WORLD modules... DONE
-- Excluding from source files list: modules/imgproc/src/imgwarp.lasx.cpp
-- Excluding from source files list: modules/imgproc/src/resize.lasx.cpp
-- Excluding from source files list: <BUILD>/modules/world/layers/layers_common.rvv.cpp
-- Excluding from source files list: <BUILD>/modules/world/layers/layers_common.lasx.cpp
-- Excluding from source files list: <BUILD>/modules/world/int8layers/layers_common.rvv.cpp
-- Excluding from source files list: <BUILD>/modules/world/int8layers/layers_common.lasx.cpp
-- Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/conv_block.neon.cpp
-- Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/conv_block.neon_fp16.cpp
-- Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/conv_depthwise.rvv.cpp
-- Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/conv_depthwise.lasx.cpp
-- Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/fast_gemm_kernels.neon.cpp
-- Excluding from source files list: <BUILD>/modules/world/layers/cpu_kernels/fast_gemm_kernels.lasx.cpp
-- Use autogenerated whitelist H:/workspace/opencv-4.11.0/build/modules/js_bindings_generator/whitelist.json
-- Found 'misc' Python modules from H:/workspace/opencv-4.11.0/modules/python/package/extra_modules
-- Found 'mat_wrapper;utils' Python modules from H:/workspace/opencv-4.11.0/modules/core/misc/python/package
-- Found 'gapi' Python modules from H:/workspace/opencv-4.11.0/modules/gapi/misc/python/package
-- Found 'misc' Python modules from H:/workspace/opencv-4.11.0/modules/python/package/extra_modules
-- Found 'mat_wrapper;utils' Python modules from H:/workspace/opencv-4.11.0/modules/core/misc/python/package
-- Found 'gapi' Python modules from H:/workspace/opencv-4.11.0/modules/gapi/misc/python/package
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install
directory
Call Stack (most recent call first):
CMakeLists.txt:1172 (include)
--
-- General configuration for OpenCV 4.11.0 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): H:/workspace/opencv-4.11.0/modules/opencv_contrib/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2025-03-07T10:45:31Z
-- Host: Windows 10.0.22631 AMD64
-- CMake: 3.25.2
-- CMake generator: Visual Studio 17 2022
-- CMake build tool: C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe
-- MSVC: 1935
-- Configuration: Debug Release
-- Algorithm Hint: ALGO_HINT_ACCURATE
--
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3
-- requested: SSE3
-- Dispatched code generation: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_1 (18 files): + SSSE3 SSE4_1
-- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
-- AVX (9 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
-- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX FP16
-- AVX2 (38 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX FP16 AVX2 FMA3
-- AVX512_SKX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX FP16 AVX2 FMA3 AVX_512F AVX512_COMMON AVX512_SKX
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe (ver 19.35.32217.1)
-- C++ flags (Release): /MP /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:fast /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /O2 /Ob2 /DNDEBUG
-- C++ flags (Debug): /MP /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:fast /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /Zi /Ob0 /Od /RTC1
-- C Compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe
-- C flags (Release): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:fast /O2 /Ob2 /DNDEBUG
-- C flags (Debug): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:fast /Zi /Ob0 /Od /RTC1
-- Linker flags (Release): /machine:x64 /INCREMENTAL:NO
-- Linker flags (Debug): /machine:x64 /debug /INCREMENTAL
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies: cudart_static.lib nppc.lib nppial.lib nppicc.lib nppidei.lib nppif.lib nppig.lib nppim.lib nppist.lib nppisu.lib nppitc.lib npps.lib cublas.lib cudnn.lib cufft.lib -LIBPATH:D:/software/nvidia/cuda12.6/lib/x64
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape signal stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab world xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: cudacodec python_tests wechat_qrcode
-- Disabled by dependency: -
-- Unavailable: alphamat cannops cvv fastcv freetype java julia matlab ovis python2 python2 sfm viz
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: NO
--
-- Windows RT support: NO
--
-- GUI:
-- Win32 UI: YES
-- VTK support: NO
--
-- Media I/O:
-- ZLib: build (ver 1.3.1)
-- JPEG: build-libjpeg-turbo (ver 3.0.3-70)
-- SIMD Support Request: YES
-- SIMD Support: YES
-- WEBP: build (ver decoder: 0x0209, encoder: 0x020f, demux: 0x0107)
-- AVIF: NO
-- PNG: build (ver 1.6.43)
-- SIMD Support Request: YES
-- SIMD Support: YES (Intel SSE)
-- TIFF: build (ver 42 - 4.6.0)
-- JPEG 2000: build (ver 2.5.0)
-- OpenEXR: build (ver 2.3.0)
-- GIF: NO
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- FFMPEG: YES (prebuilt binaries)
-- avcodec: YES (58.134.100)
-- avformat: YES (58.76.100)
-- avutil: YES (56.70.100)
-- swscale: YES (5.9.100)
-- avresample: YES (4.0.0)
-- GStreamer: YES (1.18.5)
-- DirectShow: YES
-- Media Foundation: YES
-- DXVA: YES
--
-- Parallel framework: Concurrency
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Intel IPP: 2021.12.0 [2021.12.0]
-- at: H:/workspace/opencv-4.11.0/build/3rdparty/ippicv/ippicv_win/icv
-- Intel IPP IW: sources (2021.12.0)
-- at: H:/workspace/opencv-4.11.0/build/3rdparty/ippicv/ippicv_win/iw
-- Lapack: NO
-- Eigen: NO
-- Custom HAL: NO
-- Protobuf: build (3.19.1)
-- Flatbuffers: builtin/3rdparty (23.5.9)
--
-- NVIDIA CUDA: YES (ver 12.6, CUFFT CUBLAS NVCUVENC FAST_MATH)
-- NVIDIA GPU arch: 89
-- NVIDIA PTX archs:
--
-- cuDNN: YES (ver 9.7.1)
--
-- OpenCL: YES (NVD3D11)
-- Include path: H:/workspace/opencv-4.11.0/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 3:
-- Interpreter: D:/software/Python/Python312/python.exe (ver 3.12.9)
-- Libraries: D:/software/Python/Python312/libs/python312.lib (ver 3.12.9)
-- Limited API: NO
-- numpy: D:/software/Python/Python312/Lib/site-packages/numpy/_core/include (ver 2.2.3)
-- install path: D:/software/Python/Python312/Lib/site-packages/cv2/python-3.12
--
-- Python (for build): D:/software/Python/Python312/python.exe
--
-- Java:
-- ant: NO
-- Java: YES (ver 1.8.0.302)
-- JNI: D:/software/Android/jdk/jdk-8.0.302.8-hotspot/jdk8u302-b08/include D:/software/Android/jdk/jdk-8.0.302.8-hotspot/jdk8u302-b08/include/win32 D:/software/Android/jdk/jdk-8.0.302.8-hotspot/jdk8u302-b08/include
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: D:/software/Python/Python312
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: H:/workspace/opencv-4.11.0/build
------------- Current directory is now: H:\workspace\opencv-4.11.0\build
--------------Compile with CMake------------
cmake --build . --config Release --target ALL_BUILD -j 32
--------------Compile with MSBuild----------
msbuild OpenCV.sln -p:Configuration=Release -p:Platform=x64 -m:32
--------------Then install-------------------
msbuild OpenCV.sln -t:install -p:Configuration=Release -p:Platform=x64 -m:32
(anaconda3)PS H:\workspace\opencv-4.11.0\build>
5 多线程编译 :
我这里使用 32个线程,根据自己的cpu核心数配置
接着上面窗口输入:
cmake --build . --config Release --target ALL_BUILD -j 32
或者
msbuild OpenCV.sln -p:Configuration=Release -p:Platform=x64 -m:32
或者cmake-gui 窗口 Open Project 打开 vs 编译
6 安装
cmake --install ./
7 测试
test-cuda.py
import cv2
# 打印完整构建信息(检查CUDA相关配置)
print("\n--- OpenCV构建信息 ---")
print(cv2.getBuildInformation())
print("\n---------------------")
# 检查OpenCV版本及CUDA支持
print("OpenCV版本:", cv2.__version__)
print("CUDA是否可用:", cv2.cuda.getCudaEnabledDeviceCount() > 0)
# GPU内存管理测试
try:
# 创建GPU内存分配器
allocator = cv2.cuda_GpuMat()
allocator.create(1024, 1024, cv2.CV_8UC1) # 分配1024x1024的单通道图像内存
print("\nGPU内存分配成功")
except Exception as e:
print("\nGPU内存分配失败:", e)
输出:
异常处理
假如遇到 找不到 dll
问题
请使用 Dependencies
查看 cv2.cp312-win_amd64.pyd
依赖关系,没有缺少的dll 表示正确
如果缺少dll , 请查找并且复制到 cv2.cp312-win_amd64.pyd 同目录
,我这是
D:\software\Python\Python312\Lib\site-packages\cv2\python-3.12