A few days ago, I saw a tutorial on using OpenCV to call CUDA to accelerate target detection and key point detection on Bilibili. The inference speed is significantly improved compared to Pytorch. I recently installed an RTX4070, so I wanted to give it a try. Since I just changed my computer and have no relevant environment, I ran into some pitfalls during the configuration process, so I wrote this record article.
Table of contents
basic configuration
- System: Windows11-22H2
- Graphics card: RTX4070
- Driver: CUDA-11.8,cudnn-windows-x86_64-8.9.2.26
- Compilation software: CMake, Visual Studio 2019
- Version: OpenCV 4.7.0, Contrib 4.7.0
Preconditions
Install Visual Studio2019, CMake, CUDA and cuDNN
- Visual Studio download address
- CMake download link
- CUDA download link
- cuDNN download link
VS and CMake installation is very simple, just like ordinary software. For the download and installation of CUDA and cuDNN, please refer to this previous article.
Download and modify OpenCV
download
address
It should be noted that the versions of the downloaded OpenCV main package and Contrib package must be consistent. I used version 4.7.0.
After downloading, unzip the two packages to the same directory for easy search. At the same time, in order to store the compiled OpenCV library that supports CUDA, a new folder needs to be created. Here I created a folder named opencv_cuda_build in the same directory, as shown in Figure 1 .
Since the downloaded OpenCV compiled package does not have relevant support in the CUDA and contrib packages, source code compilation is required to generate a customized OpenCV library that supports Contrib and CUDA. The following steps are required between the downloaded OpenCV source code and the library files we need to accelerate inference:
- Use CMake to configure the OpenCV source code project and generate the project files required for VS compilation.
- Use VS to compile the source code into the corresponding library file
- Configure the OpenCV library file generated by compilation into VS before linking to our own compiled library in the VS project
Change setting
Due to some objective reasons, some libraries required for compiling OpenCV and contrib cannot be downloaded in China. Therefore, some modifications to the downloaded code configuration are required to avoid compilation failure caused by the lack of certain libraries.
Specifically, it is mainly OpenCV's ffmpeg, ippicv and contrib's face, xfeatures2d libraries.
The solution is to modify the download addresses of these libraries and add agents in front of the original download addresses.
For example, the original download address in the ippicv.cmake file of ippicv
https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/
Add a proxy address in front
https://ghproxy.com/
Just modify it like this
https://ghproxy.com/https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/
As shown in Figure 2
, by analogy, the ffmpeg configuration file is opencv\sources\3rdparty\ffmpeg\ffmpeg.cmake , and the xfeatures2d configuration file is in the opencv_contrib-4.7.0\opencv_contrib-4.7.0\modules\xfeatures2d\cmake** directory. Download_boostdesc.cmake and download_vgg.cmake**, the configuration file of face is F:\packages\opencv_contrib-4.7.0\opencv_contrib-4.7.0\modules\face\ CMakeLists .
Compile OpenCV
The preliminary work is completed and we start compiling the OpenCV we need.
1. Use CMake to configure the OpenCV source code project
-
Open the CMake software, and select the path of the OpenCV source code we decompressed in the Where is the source code column. In my case, it is F:/packages/opencv/sources. In the Where to build the binaries column, select the opencv_cuda_build folder we created. Then click Configure to perform Configure for the first time, select VS 2019 and x64 in the pop-up dialog box, and then click finish.
After the execution is completed, the interface is as shown below:
-
Next, search for CUDA in the CMake search box and check the pop-up CUDA option.
-
Search and check math related
-
Search and check NONFREE
-
Search and check BUILD_opencv_world
-
Search for GENERATE_SETUPVARS and uncheck
-
Finally, search for MODULES and add the path to the contrib package.
At this point, complete the configuration selection of this step. Click configure for the second time and wait for the configuration to be completed, as shown below:
-
After configure is completed, search for CUDA_ARCH_BIN and choose to retain the corresponding value based on your own GPU computing power. The values corresponding to the specific GPU can be found on the Nvidia official website . Since the computing power of the 40 series graphics card is 8.9, and the maximum computing power supported by the 4.7 version of OpenCV is 8.6, so I chose 8.6 here.
Next, click configure for the third time . After waiting for completion, click generate as shown below:
Then you can click Open Project to open Visual Studio 2019. At this point, CMake has completed its mission.
2.Visual Studio 2019 Compile OpenCV source code
- After opening the VS project, select the Release x64 version, right-click ALL_BUILD in the CMakeTargets directory in the Solution Manager , select Generate, and start compilation. (This process takes a long time, 13600K compilation takes about 50 minutes)
- After the ALL_BUILD compilation is completed, select INSTALL , and also right-click to perform the generation operation
. After this step is completed, the install folder will be generated under the opencv_cuda_build/ folder:
the files in the generated install folder are the files we need. At this point, OpenCV compilation Finish!
Configuring Visual Studio 2019
After compiling OpenCV, you need to configure VS in order to reference our compiled OpenCV library in our project. Specifically, there are mainly
- Add include directory
- Add library directory
- Add additional dependencies
- Add environment variables
- First, we create a blank VS C++ project and add the source file main.cpp, then select Release x64 , right-click on Release | Add these two include paths under the include directory:
F:\packages\opencv_cuda_build\install\include
F:\packages\opencv_cuda_build\install\include\opencv2
- Then add the lib path in the library directory below :
F:\packages\opencv_cuda_build\install\x64\vc16\lib
- Finally, add all the lib file names in the opencv_cuda_build\install\x64\vc16\lib folder in the linker->input->additional dependencies option. I only have opencv_img_hash470.lib and opencv_world470.lib here.
- Finally, you need to add the compiled binary file path to the environment variable, search and edit the environment variable in the system search bar, and add the bin path in the Path column:
F:\packages\opencv_cuda_build\install\x64\vc16\bin
At this point, the VS configuration is complete, and we can call the CUDA-supported OpenCV we just compiled in VS.
test program
Add the following sample program to the main.cpp you just created:
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/cudaarithm.hpp>
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::cuda;
int main()
{
// OpenCV版本号
cout << "OpenCV_Version: " << CV_VERSION << endl;
// CUDA
int num_devices = getCudaEnabledDeviceCount();
if (num_devices)
cout << "CUDA is available, num_devices:" << num_devices << endl;
else
cout << "CUDA is not available." << endl;
//读取图片
Mat img = imread("D:/CUDA_ARCH_BIN.png");
imshow("picture", img);
waitKey(0);
return 0;
}
If the picture and CUDA can be displayed correctly, the installation is successful!