Configuring OpenCV with CUDA under Win11

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.

basic configuration

  1. System: Windows11-22H2
  2. Graphics card: RTX4070
  3. Driver: CUDA-11.8,cudnn-windows-x86_64-8.9.2.26
  4. Compilation software: CMake, Visual Studio 2019
  5. Version: OpenCV 4.7.0, Contrib 4.7.0

Preconditions

Install Visual Studio2019, CMake, CUDA and cuDNN

  1. Visual Studio download address
  2. CMake download link
  3. CUDA download link
  4. 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

  1. OpenCV
  2. contrib package

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 .

Figure 1-Table of Contents
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:

  1. Use CMake to configure the OpenCV source code project and generate the project files required for VS compilation.
  2. Use VS to compile the source code into the corresponding library file
  3. 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
Figure 2-Modify ippicv download address
, 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.
    Figure 3 - Configuring CMake
    After the execution is completed, the interface is as shown below:
    Figure 4-The first Configure is completed

  • Next, search for CUDA in the CMake search box and check the pop-up CUDA option.
    Figure 5-CUDA

  • Search and check math related
    Figure 6 - Add math support

  • Search and check NONFREE
    Figure 7-NONFREE

  • Search and check BUILD_opencv_world
    Figure 8-BUILD_opencv_world

  • Search for GENERATE_SETUPVARS and uncheck
    Figure 9 - Cancel GENERATE_SETUPVARS

  • Finally, search for MODULES and add the path to the contrib package.
    Figure 10-Add contrib package path
    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:
    Figure 11-The second configure is completed

  • 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.

Figure 12-GPU computing power
Next, click configure for the third time . After waiting for completion, click generate as shown below:
Figure 13-configure is completed and generate is successful
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)

Figure 14-Compile ALL_BUILD

  • After the ALL_BUILD compilation is completed, select INSTALL , and also right-click to perform the generation operation
    Figure 14-Generating INSTALL
    . After this step is completed, the install folder will be generated under the opencv_cuda_build/ folder:
    Figure 15-Generated install 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

  1. Add include directory
  2. Add library directory
  3. Add additional dependencies
  4. 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

Figure 16-Add include directory
Figure 17-Add include directory

  • Then add the lib path in the library directory below :
F:\packages\opencv_cuda_build\install\x64\vc16\lib

Figure 18-Add library directory

  • 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.
    Figure 19 - Adding additional dependencies
  • 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

Insert image description here

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!
Figure 20-Test results

Reference tutorial

  1. Solve download problems such as ippicv
  2. OpenCV installation tutorial
  3. Previous installation articles

Guess you like

Origin blog.csdn.net/weixin_40313940/article/details/131265058