OpenCV4.8 GPU version CMake compilation detailed steps and CUDA code demonstration

Introduction

    This article will introduce in detail how to use CMake to compile the OpenCV4.8 CUDA version and give a demo demonstration for everyone to learn and use.

CMake compilation detailed steps

    Without further ado, let's get straight to the point!

  【1】The tool version I use is VS2017 + CMake3.18.2 + OpenCV4.8.0 + CUDA11.2

   Generally, VS version ≥ VS2017 is acceptable, CMake version ≥ 3.18.2, OpenCV4.8.0 is currently the latest, and the CUDA version remains the same or higher.

  【2】OpenCV source code download and CUDA installation

   First download the OpenCV4.8.0 source code and Contrib part of the source code:

https://github.com/opencv/opencvhttps://github.com/opencv/opencv_contrib

  After downloading, extract it to the specified directory:

picture

  Create a new build folder under the opencv-4.8.0 folder to save the compiled files:

picture

  Install CUDA related dependencies yourself, including CUDA and CUDNN:

picture

  【3】CMake configuration option settings

   Things to note when setting CMake configuration options are as follows:

   ① Choose which version to compile according to your own VS version, here VS2017 64-bit

   ② Configure the source code path and generated file path:

picture

   ③ Configure the contrib source code directory:

picture

  ④ The CUDA path does not need to be configured, it will be automatically identified and filled in. There are also the following options that need to be checked. Remember to check:

picture

picture

  ⑤ Other options that need to be checked:

picture

picture

  【4】CMake Config and problem solving

   The next normal step is to click Configure and Generate to generate the OpenCV.sln project. However, warnings or errors often appear, mostly due to failure to download some dependent files:

picture

picture

  Although it is a warning, it must be resolved, otherwise it will still fail when compiling the OpenCV.sln project later!

  Solution: Follow the prompts, open the corresponding CMakeDownloadLog.txt, then download the corresponding file and put it in the specified directory, with everything written in it, as shown in the figure below:

picture

If the file is downloaded successfully and matches, the following corresponding prompt will appear:

picture

    After the file is downloaded successfully, click Configure and there will be no red warning prompt. Then click Generate to generate the OpenCV.sln project.

picture

  [5] VS compiles and generates dependencies

   Open OpenCV.sln, compile INSTALL, then right-click only for the project--generate INSTALL only

picture

    The following is the generated file. The opencv_world480.dll file is 1.06G normal:

picture

picture

picture

      

OpenCV CUDA programming example

    When writing code using C++ OpenCV and CUDA, the general steps are as follows:

  【1】Include the necessary header files: First, include the relevant header files to use the OpenCV and CUDA libraries. ​​​​​​​

#include <opencv2/opencv.hpp>#include <opencv2/cudaimgproc.hpp>

  【2】Load images or videos: Use OpenCV functions to load input images or videos. For example, you can use cv::imread() to load images or cv::VideoCapture to load videos. ​​​​​​​

cv::Mat image = cv::imread("image.jpg");// 或者cv::VideoCapture video("video.mp4");

  【3】Allocate GPU memory and copy data to device memory: If you plan to perform acceleration operations on CUDA, you need to allocate memory for images or videos on the GPU and copy data from host memory to device memory. ​​​​​​​

cv::cuda::GpuMat gpuImage;gpuImage.upload(image);// 或者cv::cuda::GpuMat gpuVideo;cv::Mat frame;video.read(frame);gpuVideo.upload(frame);

  【4】Perform CUDA acceleration operations: Call the appropriate OpenCV CUDA function to perform CUDA acceleration operations on the GPU. ​​​​​​​

cv::cuda::cvtColor(gpuImage, gpuImage, cv::COLOR_BGR2GRAY);// 或者cv::cuda::blur(gpuVideo, gpuVideo, cv::Size(3, 3));

  【5】Copy the results from device memory to host memory: Use the download() method to copy the results from device memory to host memory. ​​​​​​​

cv::Mat result;gpuImage.download(result);// 或者cv::cuda::GpuMat result;cv::cuda::bitwise_not(gpuImage, result);cv::Mat result_host;result.download(result_host);

  【6】Display results: Use OpenCV functions to display result images or video frames on the screen. ​​​​​​​

cv::imshow("Result", result);cv::waitKey(0);cv::destroyAllWindows();

    These are general steps, specific code implementation and operations may vary as needed. Please make sure you have the correct versions of OpenCV and CUDA installed and configured, and adjust them appropriately for your needs.

    As for the acceleration effect, it needs to be analyzed based on the actual situation. The following is the official acceleration comparison of individual methods:

picture

Guess you like

Origin blog.csdn.net/stq054188/article/details/132766965