Build OpenCV on Windows with CUDA!

Scott Jin
6 min readJul 26, 2023

--

If you are ambitious to do many different things related to images, Opencv is the go-to library to cover them all. It can provide you from plain image processing all the way to inferencing machine learning modules. However, to exploit opencv’s full capability, you should build the library on your own.

Why do I bother to do this?

There is an Opencv DLL offered on the official Git Hub ready to be downloaded and installed without any hassle. But what if we need it as a static library? Or maybe we want to process images with Nvidia GPU, which is also the reason why GPUs exist in the first place. If you want a clean and neat program that runs as fast as possible, you will definitely want to write in C/C++. This tutorial will help you build the library with CMake, Visual Studio, and CUDA. Let’s begin with installing CUDA which is the library that lets you inference with Nvidia GPUs.

Which CUDA version should I install?

Although it is convenient enough to have opencv DNN(deep neural network), you sometimes need other libraries to inference neural networks, due to the opencv::dnn’s support on different formats can be meager. Consequently, the actual CUDA version you use depends on these other libraries. For example, if I’m going to use ONNXRuntime accompanied with OpenCV, since at the time I’m writing this tutorial ONNXRuntime 1.14 is the newest and it is supported with CUDA 11.6. So I should use CUDA 11.6. I’m also using cuDNN version 8.5.0.

Always check which CUDA and cuDNN versions are recommended and install the best one.

You can download CUDA and cuDNN from the links below. You may need an NVIDIA account to download it.

Install it as prompted by the installer. Copy cuDNN inside cuda, bin into bin, include into include, lib into lib.

Finally, add all these folders and the folder x64 inside lib into the environmental variable.

“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin”

“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\lib”

“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include”

“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\lib\x64”

Your’s should be something similar.

Download OpenCV source

Download the newest versions available, remember both of them should have matching version numbers. I’m going to use version 4.8.0. Put them into one folder and decompress them. Now we can move on to the part most prone to error.

Cmake and configs

Most problems you will meet are due to this part, especially if you are ambitious to install all possible dependencies to speed up OpenCV as much as it can.

To begin with, set “Where is the source code” to the root folder of the OpenCV source code directory, where there is a file called “CMakeLists.txt”

Set the “Where to build the binaries” to your desired directory to build.

Then click configure, you’ll see this

Choose your visual studio version and set the platform as x64 as above. Click finish then wait and see. If it is a success, you will see a “configure done” like below.

Something like this

First, we add the extra modules, type extra in search and find “OPENCV_EXTRA_MODULES_PATH”, and change its value to where your OpenCV contrib modules folder is. Then again press configure again.

Then we search for cuda and select “OPENCV_DNN_CUDA” and “WITH_CUDA”, and configure again. If configure fails here, check whether you have put the aforementioned four directories inside environmental variables.

If you are only aiming your programs to your own computer or only some of the NVIDIA GPUs you change the value listed in “CUDA_ARCH_BIN” to only some of the architectures, you can search for architectures in the list below.

This will save you a lot of time building the library.

If you want to build it as a static library(.lib) deselect the option “BUILD_SHARED_LIBS”. Remember to configure every time after you make major selections so you can backtrack easily. Also, the “BUILD_opencv_world” option will let create a single, cover-all library, so you don’t have to add them one by one.

Now click generate to create the build pipeline. Luckily enough, you will see “generate done below”.

Build the library

Now we can start building the library. Go to the directory you set in “Where to build the binaries” and find “OpenCV.sln”. Open it.

Remember to change to release mode. Then click CMakeTargets, right-click on “ALL_BUILD” and then build. If something fails here, you must clean the entire project. You will also have to go back to cmake to change configurations. If you have failed in cmake, it is impossible to compile here in Visual Studio. The building process can be very long, so try to find something else to do, that doesn’t interfere with the tranquility of the program being built.

After the long wait, right-click on install and then build. If both of them are successful, you will get your own built library in the install directory.

The install directory

If you have built the library to a DLL you should add the opencv\install\x64\vc17\bin inside your system variable path.

Opencv DNN in C/C++

To use this library we have to first include the headers in VC++ Directories like below.

Then we add the linker directory in Linker->Additional Library Directory.

Then we go to Linker->Input->Additional dependencies and add OpenCV world DLL or lib inside.

Test it

Finally, we can test it! This script uses cudaimgproc to utilize GPU for image processing. Make sure you have set the environmental variables correctly.

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


int main() {

cv::cuda::setDevice(0);
cv::VideoCapture cap(0);
cv::Mat frame;
cv::cuda::GpuMat Gframe;

if (!cap.isOpened())
{
std::cout << "No camera exist\n";
return -1;
}

while (1) {
cap >> frame;
Gframe.upload(frame);
cv::cuda::cvtColor(Gframe, Gframe, cv::COLOR_BGR2GRAY);
Gframe.download(frame);
cv::imshow("Gray webcam", frame);
if (cv::waitKey(1) == 27) { // Press esc
std::cout << "End camera loop\n";
return 1;
}
}
return 0;
}

--

--