guide
Preface
It took a week from the beginning of configuring OpenCV's cuda environment to success, so I feel it is necessary to record it. I have to say that this thing is really metaphysical. It’s really sad that I can’t do what others can do.
1. Installation package download address
1.1. Official download path
CMake3.15.0:
https://github.com/Kitware/CMake/releases/tag/v3.15.0
OpenCV4.1.0:
https://sourceforge.net/projects/opencvlibrary/files/4.1.0/opencv-4.1.0-vc14_vc15.exe/download
opencv_contrib4.1.0:
https://codeload.github.com/opencv/opencv_contrib/zip/refs/tags/4.1.0
cuda10.0:
https://developer.nvidia.com/cuda-10.0-download-archive
cudnn7.6.1:
https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v7.6.1.34/prod/10.0_20190620/cudnn-10.0-windows10-x64-v7.6.1.34.zip
VS2017 Enterprise:
https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/
1.2. Other download paths
CMake3.15.0+OpenCV4.1.0+opencv_contrib4.1.0+VS2017
cudda10.0+cudnn7.6.1 extraction code: 14o4
Tips: Pay attention to the installation sequence, and there is a warning for long articles.
2. Install required software, etc.
2.1. VS2017
Download an executable file, double-click to run it to enter the VS2017 installation program, and check ① Desktop development using C++; ② Python development; ③ Visual Studio extension development in the workload. After that, you can change the installation location to what you want, as shown in the figure below. My installation path is E:\Microsoft_Visual_Studio_2017_Enterprise
.
I installed the enterprise version of VS. You can choose this according to your own situation. Community version, enterprise version and professional version are all available.
2.2. cuda+cudnn
① Download cuda and get an executable file. Double-click it to run it to enter the installation program. Just execute it in sequence. Please select Custom (Advanced) in the middle.
Then uncheck VS Configuration, otherwise an error may be reported.
Just uncheck the box and execute it sequentially.
② Download cudnn and get a compressed package. After decompression, copy the cudnn64_7.dll, cudnn.h, and cudnn.lib files in bin, include, and lib/x64 to the same path under the previous cuda installation path.
2.3. opencv+opencv_contrib
① Download opencv and get an executable file. You can double-click to run it or decompress it directly (note that you change the decompressed folder path). After decompression, you will get a folder opencv.
After decompression is complete:
Copy opencv_ffmpeg410_64.dll from directory E:\opencv\build\x64\vc15\bin to path C:\Windows\System32.
Copy opencv_world410.dll and opencv_world410d.dll to path C:\Windows\SysWOW64
Then add environment variables:
D:\opencv4.1.0\build\bin
D:\opencv4.1.0\build\x64\vc15\bin
② Download opencv_contrib to get a compressed package, and unzip it in a directory at the same level as the previous folder opencv, as shown in the figure (in principle, you can actually unzip it anywhere).
2.4. cmake
cmake can directly download the compressed package without installation, or you can download its installation program, depending on your personal situation. What I downloaded here is cmake-3.15.0-win64-x64.msi. Double-click to run and you will enter the installation program. Make sure to check Add Cmake to the sysyem PATH for all users and change the installation path.
3. Start compiling
3.1 cmake compilation
Open the CMake software:
source code 选择 E:/opencv/sources,
build the binaries 选择 E:/opencv/cudabuild
Among them, cudabuild is the path to be built by yourself (it is recommended to create a new one). Configure it after the selection is completed:
① First time configuration
- Click Configuration to configure, and select VS2017 and x64 in the pop-up window. When the configuration is completed, the words Configuration done will appear. There is no need to click Generate at this time.
② Second configuration
- 勾选 BUILD_EXAMPLES, BUILD_opencv_world, WITH_CUDA, OPENCV_ENABLE_NONFREE。
- Select E:/opencv_contrib-4.1.0/modules for OPENCV_EXTRA_MODULES_PATH.
- Click Configuration to configure and wait for Configuration done to appear.
③ Third configuration
- In CUDA_ARCH_BIN, only the computing power corresponding to your own graphics card is kept. For the specific correspondence, you can check the data given on the official website . My graphics card is 1050Ti, and the computing power is 6.1.
- Click Configuration to configure and wait for Configuration done to appear.
④ The fourth configuration
- In the second configuration, there will be problems with some packages not being downloaded successfully. You only need to manually download and put them in the corresponding location. For detailed links and download content, you can open E:\opencv\cudabuild\CMakeDownloadLog.txt to view. If manual downloading is slow, You can try ladder or continuous configuration, etc.
- I have uploaded the corresponding package that needs to be downloaded. If you fail to download it yourself, you can click the link to download.
- After the configuration is completed, the words Configuration done will appear. Click Generate to generate. After the generation is completed, the words Generattion done will appear.
Check whether the OpenCV.sln file has appeared in the path E:\opencv\cudabuild. If it exists, the opencv environment with cuda has been compiled successfully. Next, generate it in VS.
3.2 VS2017 generation
Close the CMake software, open the OpenCV.sln file with VS2017, wait for the loading to succeed, click Generate on the menu bar, and select batch generation:
In the pop-up window, select Debug|x64 and Release|x64 l of ALL_BUILDD and INSTALL respectively, a total of four items, as shown in the figure:
Then we will wait for several hours. Many warnings will appear during the process. As long as no error occurs, there is no problem. Screenshot after success:
After the generation is successful, open the E:\opencv\cudabuild\install folder and you will see the following content:
3.3 Test environment
Next, you can simply use the test code to test whether the configured environment is available.
① Create a new empty project
Click File → New → Project.
Select Visual C++ → Empty Project, modify the project name and project path, and click OK.
② Configure new project properties
The empty project is created successfully. Click View → Other Windows → Property Manager on the menu bar to open the Property Manager.
Select Debug|x64 → Add new project attribute table, and select Add directly in the pop-up window. You can also create a new attribute table. It is also possible to directly configure Microsoft.Cpp and x64.user under Debug|x64. The reason for creating a new one is mainly to consider that new projects can directly import the attribute table in the future without having to configure it separately.
At this time, the property sheet PropetySheet will appear under Debug|x64, right-click and select Properties.
In the pop-up property page, select VC++ Directory → Include Directory → Drop-down Edit, add cuda path and opencv related path:
E:\opencv\cudabuild\install\include\opencv2
E:\opencv\cudabuild\install\include C:\Program Files\NVIDIA GPU
Computing Toolkit\CUDA\v10.0\include
Click VC++ Directory → Library Directory → Drop-down Edit and add the path:
E:\opencv\cudabuild\install\x64\vc15\lib
Click Linker → Input → Additional Dependencies, add:
opencv_world410d.lib
After everything is completed, you must click Apply to apply this project property sheet in the project, and then click Solution Explorer → Source File → Add → New Item.
In the pop-up Add New Item window, select Visual C++ → C++ File, change the name and click Add.
At this point, you can write code for testing. The test code is below. Its function is to call the camera and dynamically display the captured image on a window. Please select Debug|x64 before running.
Test code example:
#include<iostream>
#include<stdlib.h>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
using namespace cv::cuda;
int main()
{
Mat demo;
VideoCapture capture(0);
while (1) {
capture >> demo;
imshow("demoWindow", demo);
waitKey(1);
}
system("pause"); // 暂停
return 0;
}
Running results (graffiti warning):
4. Some possible errors
① When CMake clicks Configuration, some packages or libraries and other files are not successfully downloaded.
- Click Configuration multiple times until successful;
- Use the ladder to manually download the required package and put it in the corresponding path. The download content, link and path can be viewed in E:\opencv\cudabuild\CMakeDownloadLog.txt;
- Click the link to download directly and put it in the corresponding path.
② Cmake cannot find cuda or cudnn when compiling, or the version found is incorrect.
- Use the command nvcc -version in cmd to check whether the current cuda version is correct. If cuda has been successfully installed, it is mostly an
issue with environment variables; - Check whether the application version in the environment variable is the expected version, if not, modify it.
- Uninstall and install cuda again.
③ Opencv_world410d.lib, kernel32.lib and other libraries cannot be found during VS2017 testing.
- Confirm that opencv_world410d.lib has been added to Linker → Input → Additional Dependencies;
- Kernel32.lib is in the path
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\um\x64
and added to the VC++ directory → library directory; - Select Inherit from parent or project default settings in the VC++ Directory → Library Directory → drop-down.