Building OpenCV for Windows with CUDA
-
Install CUDA from
https://developer.nvidia.com/cuda-downloads; -
Download cuDNN from
https://developer.nvidia.com/cudnn(this requires registration); -
Install git for windows; for example, use
https://gitforwindows.org/; -
From the command prompt, type
git clone https://github.com/opencv/opencv.git; this will create an opencv directory under the current directory (C:\Users\bookWriter, for example); -
From the command prompt, type
git clone https://github.com/opencv/opencv_contrib; this will create anopencv_contribdirectory under the current directory; -
Add
C:\Program Files (x86)\Windows Kits\8.1\bin\x86to thePathenvironment variables, if not yet included; -
Install CMake from
cmake.org/download; use the.msifile; -
From the command line, type
"C:\Program Files\CMake\bin\cmake.exe" -B"C:\Users\bookWriter\opencv\build" - H"C:\Users\bookWriter\opencv" - DOPENCV_EXTRA_MODULES_PATH="C:\Users\bookWriter\opencv_contrib\ modules" -G"Visual Studio 14 2015 Win64" - DBUILD_opencv_world=ON -DWITH_CUDA=ON -DCUDA_FAST_MATH=ON - DWITH_CUBLAS=ON -DINSTALL_TESTS=ON -DINSTALL_C_EXAMPLES=ON - DBUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release - DBUILD_opencv_gapi=OFF -DWITH_NVCUVID=OFF -DWITH_OPENGL=ON - DOPENCV_ENABLE_NONFREE=ON - DCUDA_ARCH_PTX=5.2C:\Users\bookWriteris the directory within whichopencvandopencv_contribhave been downloaded and created;OpenCVis built usingVisual Studio 2015(-G"Visual Studio 14 2015 Win64"option), but other versions of Visual Studio are possible; the code is built for a5.2architecture (optionDCUDA_ARCH_PTX=5.2); this operation may take several minutes and will create, among others, theOpenCV.slnfile underC:\Users\bookWriter\opencv\build; -
Double click on the
OpenCV.slnfile; -
Under
Solution'OpenCV' -> CMakeTargets -> INSTALL -> right click -> build; -
Add
C:\Users\bookWriter\opencv\build\install\x64\vc14\binto the environment variables.
Please, be aware that OpenCV 4.1.1 requires at least CUDA 10.1.
First OpenCV CUDA example
A first OpenCV CUDA example is reported in Listing 1.
#include <opencv2\opencv.hpp>
#include <opencv2\cudaimgproc.hpp>
#include <opencv2\opencv.hpp>
#include <opencv2\cudaimgproc.hpp>
/********/
/* MAIN */
/********/
int main(){
std::cout << cv::getBuildInformation() << std::endl;
auto idx = cv::cuda::getCudaEnabledDeviceCount();
if (idx > 0) cv::cuda::printCudaDeviceInfo(0);
else printf("OpenCV CUDA error!");
return 0;}
Listing 1. First OpenCV CUDA example.
The code in this example checks whether the OpenCV CUDA installation is in order.
To compile such an example under Visual Studio 2015, do not forget to:
-
add
Project -> Properties -> Configuration Properties -> VC++ Directories -> Include Directories -> C:\Users\bookWriter\opencv\build\install\include; -
add
Project -> Properties -> Configuration Properties -> Linker -> General -> C:\Users\bookWriter\opencv\build\install\x64\vc14\lib; -
add
Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies -> opencv_world411d.lib for OpenCV version 4.1.1;
Second OpenCV CUDA example
A second OpenCV CUDA example is reported in Listing 2.
#include <iostream>
#include "opencv2\opencv.hpp"
#include "opencv2\core.hpp"
#include "opencv2\highgui.hpp"
#include "opencv2\cudaarithm.hpp"
/********/
/* MAIN */
/********/
int main(){
cv::Mat h_src = cv::imread("C:\\Users\\bookWriter\\Packt\\
OpenCVImageTest\\file.png", cv::IMREAD_GRAYSCALE);
cv::cuda::GpuMat d_dst, d_src;
d_src.upload(h_src);
cv::cuda::threshold(d_src, d_dst, 128.0, 255.0, cv::THRESH_BINARY);
cv::Mat h_dst(d_dst);
cv::imshow("Processing result", h_dst);
cv::waitKey();
return 0; }
Listing 2. Second OpenCV CUDA example.
The code loads a .png image to CPU memory, transfers it to GPU, applies a threshold, moves the result back to the CPU and shows it.