How to start programming Computer Vision with OpenCV and CUDA in Windows 10, Visual Studio 2019 C++. Part 2

Kirill Mityugin
3 min readNov 6, 2020

In Part 1 we have created first app for Computer Vision in C++ Visual Studio 2019 streaming live video from IP Camera and detecting objects but perfomance on CPU about 1–2 FPS is not acceptible for real time processing.

Let’s add GPU acceleration with CUDA technology in our project and compare how it works on sample video.

  1. Install latest Nvidia driver for your graphics card
  2. Install CUDA toolkit and cuDNN dll’s — here is full guide:

3. Now you need to build OpenCV from source with CUDA support (it’s a long story) or just download ready builds for your version. This guy doing great job:

https://jamesbowley.co.uk/downloads/

Extract both DEBUG and RELEASE archives to separate folders in C:\opencvcuda:

4. Change system PATH variable and add dependencies to your project:

C:\opencvcuda\DEBUG\install\x64\vc16\bin and C:\opencvcuda\RELEASE\install\x64\vc16\bin

Add to PATH:

C:\opencvcuda\DEBUG\install\x64\vc16\bin

C:\opencvcuda\RELEASE\install\x64\vc16\bin

Add to Visual Studio project properties separately for DEBUG and RELEASE configs. Example for RELEASE:

Visual Studio project properties

Include Directories:

C:\opencvcuda\RELEASE\install\include

Library Directories:

C:\opencvcuda\RELEASE\lib

Linker / Input / Additional Dependencies

for RELEASE:

opencv_world450.lib

for DEBUG:

opencv_world450d.lib

Now we can run our project with OpenCV CUDA backend:

Here we can change using CUDA or CPU in our source code

I have changed YOLOv4 models to full and input sample video in FullHD res to compare perfomance:

and also you can set CONFIDENCE_THRESHOLD = 0.3 parameter to filter detected objects.

Let’s test now.

CPU only Intel Core i5 9400f:

GPU Geforce RTX 2060 OC:

We have significant acceleration from 1–2 FPS to 22 FPS* that’s awesome!

*Update 1: Use DNN_TARGET_CUDA_FP16 instead of DNN_TARGET_CUDA it will increase rate from 25 FPS to 50–60 FPS

And what about testing again live video from IP camera with minimal settings?

We can set back tiny models and low res and even smaller input res for DNN:

blobFromImage(frame, blob, 0.00392, Size(inpWidth, inpHeight), Scalar(), true, false, CV_32F);

Width, Height of network’s input image 608 you can change to 416 or 320 it will increase rate but also decrease quality of detection.

What a dramatic change to 125–166 FPS*!

*Update 1: Use DNN_TARGET_CUDA_FP16 instead of DNN_TARGET_CUDA it will increase rate to 166–333 FPS

So using CUDA is a must for Computer Vision projects and next you can compare different DNN frameworks or detection quality settings.

Good luck with your experiments and Clap here…

--

--