Preparation
Platform and Software
- Windows10 system
- Visual Studio 2019:Visual Studio Community 2019
- Cmake:cmake-3.20.0-rc3-windows-x86_64.msi
- OpenCV 4.51:opencv-4.5.1.tar.gz
- OpenCV_contrib 4.5.1:opencv_contrib-4.5.1.tar.gz
NVIDIA driver, CUDA and cuDnn
Select the appropriate driver and CUDA version and the corresponding version of cudnn, and install the win10 environment.
Install and download VS 2019, cmake, opencv and extension module source code
VS 2019 download and install
cmake download and install
opencv and opencv_contrib download
opencv download link
opencv_contrib
Baidu cloud download
extraction code:lujx
Note: Click the Tag in the upper left corner, select version 4.5.4, select the same version for opencv and opencv_contrib
Unzip the files and you are ready
Create opencv_cuda
a folder, unzip opencv
and opencv_contrib
unzip it into this folder, and create a build folder in the same directory as opencv. At this point the preparations are complete.
The directory structure is roughly as follows:
|-opencv_cuda
|--build
|--opencv_contrib_4.5.4
|--opencv_4.5.4
|---.cache
|---其他原有的解压的子文件
In order to avoid compilation failure due to file download failure during the first compilation process, you need to copy the .cache folder to the decompressed opencv source folder
Compile OpenCV
CMake compile
- Open
cmake-gui
the software, respectively addwhere is the source code
andwhere to build the binaries
asopencv源码文件夹
andbuild文件夹
- First click
configure
, selectvs 2019
andx64
architecture, clickfinish
, start the first compilation - The compilation process needs to download various dependencies. There is a high probability that it will be stuck due to network problems. Copying the .cache folder directly can save this trouble
- (This step is optional) Create a virtual environment, it is recommended to use
anaconda
, and install numpy in the virtual environment (required for compilation), this step is to installCUDA版本
opencv into虚拟环境
it,只安装到宿主机环境不需要执行此步骤
- (This step is optional, but the prerequisite for performing this step is that the previous step must be performed) Change a few variables and point the path to the corresponding location of the virtual environment:
PYTHON3_EXECUTABLE
,PYTHON3_INCLUDE_DIR
,PYTHON3_LIBRARY
,PYTHON3_NUMPY_INCLUDE_DIRS
,PYTHON3_PACKAGES_PATH
- After the compilation is complete, enter
CUDA
and in the Search boxfast
, and check three configurations:WITH_CUDA
,OPENCV_DNN_CUDA
,ENABLE_FAST_MATH
- The Search box
world
willbuild_opencv_world
be checked, and all opencv libraries will be compiled together without the need to add each small module one by one. - Search box
BUILD
, checkBUILD_opencv_python3
- Search in the search box
MODULES
, inOPENCV_EXTRA_MODULES_RATH
an item, add theopencv_contrib4.5.1
directorymodules
- search box search
NON
,OPENCV_ENABLE_NONFREE
tick - Click the second time
configure
and wait for the log below to displayconfigure done
- Enter in the search box
cuda
, check itCUDA_FAST_MATH
, andCUDA_ARCH_BIN
change the computing power content of the graphics card to the computing power of your own graphics card. The corresponding computing power and graphics card model are shown in the picture in Chapter 1. For example, if the graphics card model is and the corresponding computing power is ,GTX 1050
delete6.1
it For other computing power versions, just keep6.1
it - Click configure again, this time the Configuring done is finally OK, then click Generate, wait a moment for Generating done to appear!
- Click Open Project and it will start your Visual Studio.
VS compile
-
After VS2019 opens the newly compiled project, it will respond for a while. You must wait for all the items displayed in the lower left corner to be loaded before proceeding.
-
Select
Release ``x64
, then findCmakeTargets
the next oneALL_BUILD
, right click → "Generate", and then start a long wait... (The notebook i7-9750H compiles for about 65 minutes, for reference only) -
解决方案资源管理器
—>CMakeTargets
—>INSTALL
—>生成
"Then wait again, fortunately this time is very short. At this time,opencv_cuda\build\lib\python3\Release
you can see the file under the foldercv2.cp36-win_amd64.pyd
(different python versions, the name will be slightly different) -
At the same time, in the virtual environment or host environment, you can
Lib\site-packages
seecv2
the folder under the path
Verify the opencv environment
Use the command line to enter the python environment and execute the code to verify:
c:\users\administrator> python
>>> import cv2
>>> cv2.cuda.getCudaEnabledDeviceCount()
1 # 得到GPU设备数量,即表示opencv的GPU版本已经安装成功