Working in the field of Computer Vision for a decade, I have been using popular application frameworks to help me accomplish complex tasks, such as image processing, object tracking, face detection, and more.
- OpenCV is a set of programming modules that help us accomplish complex real-time computer vision tasks easily. Due to its reliability and open-source philosophy, it’s been one of the most popular computer vision tools.
- CUDA is a parallel computing toolkit that allows us to use the power of an NVidia GPU to significantly accelerate the performance of our applications.
However, the official OpenCV binaries do not include GPU support out-of-the-box. To harness the full power of your GPU, you’ll need to build the library yourself.
So, the following guide will show you how to compile OpenCV with CUDA support.
For your convenience, I have uploaded the latest stable compiled binaries.[button color=”accent-color” hover_text_color_override=”#fff” image=”default-arrow” size=”large” url=”https://drive.google.com/uc?id=1D6daRs0D-CQAjGz-TMZV5OtSJQtqhHhi&export=download&confirm=21-y” text=”Download OpenCV CUDA binaries” color_override=””]
Remember: if you download the binaries, share this post to help other developers, too!
Ensure you have a computer with the following specifications:
- GPU with CUDA support (tested on Nvidia 1060)
- CPU Intel Core i7 (recommended)
Download and install the following software:
- Windows 10 Operating System
- Visual Studio 2015 Community or Professional
- CUDA Toolkit 9.2
- CUDA Toolkit 9.2 Patch 1
- cuDNN 7.1.4
- CMake 3.10.1
Nvidia CUDA download page:
Nvidia cuDNN download page:
This is how to download and compile the latest version of OpenCV with CUDA support.
1) Clone OpenCV
Clone OpenCV to the desired location in your disk:
git clone https://github.com/opencv/opencv.git
In my case:
Also, switch to switch to the latest stable branch, e.g. “3.4”.
2) Generate the Visual Studio solution
Before going on, ensure that your Path Environment Variable includes the following path:
C:\Program Files (x86)\Windows Kits\8.1\bin\x64
In case the path is not included, add it manually.
Open CMD and type the following command. Replace PATH_TO_SOURCE with the path of the OpenCV installation (e.g. C:\Users\Vangos\Projects\OpenCV).
"C:\Program Files\CMake\bin\cmake.exe" -B"PATH_TO_SOURCE\build" -H"PATH_TO_SOURCE" -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
In my case:
"C:\Program Files\CMake\bin\cmake.exe" -B"C:\Users\Vangos\Projects\OpenCV\build" -H"C:\Users\Vangos\Projects\OpenCV" -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
Navigate to the PATH_TO_SOURCE folder and open the build directory. Find the OpenCV.sln file and open it with Visual Studio 2015.
In the Configuration Manager, select the following:
- Active solution configuration: Release
- Active solution platform: x64
In your Solution Explorer, find the project named INSTALL.
Right-click the INSTALL project and select “Build”.
Then, wait patiently while Visual Studio builds the project. Depending on your computer specs, it may take lots of minutes or even hours!
4) Verify the installation
When the compilation process is finished, the binaries should be under the build\install directory.
You can now follow the official OpenCV guide and integrate OpenCV with CUDA support in your own applications!
Running OpenCV with Visual C++
When running your OpenCV projects using Visual Studio, you need to add the following information in your Project Properties window:
C/C++ → General → Additional Include Directories
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\include
Linker → General → Additional Library Directories
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\lib\x64
Linker → General → Additional Dependencies
This is it! You can now use the full power of a GPU-accelerated OpenCV build.
Again, if you just want the precompiled binaries, I have uploaded them for you. Before downloading, please do me a favor and share this post on social media. This way, you are going to help other developers access great resources.[button color=”accent-color” hover_text_color_override=”#fff” image=”default-arrow” size=”large” url=”https://drive.google.com/uc?id=1D6daRs0D-CQAjGz-TMZV5OtSJQtqhHhi&export=download&confirm=21-y” text=”Download OpenCV CUDA binaries” color_override=””]
So, did you find this tutorial helpful? How are you going to use OpenCV with GPU CUDA support? Let me know in the comments below!