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.

Two of these tools are OpenCV and CUDA.

  • 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.

James Bowley has published a detailed performance comparison, where you can see the impact of CUDA on OpenCV.

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.

Download OpenCV CUDA binaries

Remember: if you download the binaries, share this post to help other developers, too!

System Requirements

Hardware

Ensure you have a computer with the following specifications:

  • GPU with CUDA support (tested on Nvidia 1060)
  • CPU Intel Core i7 (recommended)

Software

Download and install the following software:

Nvidia CUDA download page:

Nvidia CUDA 9.2 download

Nvidia cuDNN download page:

Nvidia CuDNN 7.1 downloadBuild/Compile OpenCV

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:

C:\Users\Vangos\Projects\OpenCV

Also, switch to switch to the latest stable branch, e.g. “3.4”.
OpenCV repository

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

Environment Variables - Windows Kits

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

3) Compile!

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

Visual Studio Configuration Manager - OpenCV CUDA

In your Solution Explorer, find the project named INSTALL.

Visual Studio OpenCV CUDA build/compile

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.

OpenCV CUDA windows binaries

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.1\include

and

C:\Users\Vangos\Projects\OpenCV\build\install\include

Linker → General → Additional Library Directories

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64

and

C:\Users\Vangos\Projects\OpenCV\build\install\x64\vc14\lib

Linker → General → Additional Dependencies

opencv_world400.lib

and

cudnn.lib

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.

Download OpenCV CUDA binaries

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!

Vangos Pterneas

Vangos Pterneas

Vangos is helping innovative businesses and Fortune-500 companies create amazing digital products. He's an award-winning Microsoft Most Valuable Professional.

16 Comments

  • bit-scientist says:

    thank you for this post! I wouldl ike to use opencv with python in anaonda env, using Spyder IDE. Could you please share your knowledge on that if possible. Thanks

  • we says:

    Hello,
    could you share your opencv vs2015 proejct? i would like to build cuda8 version of opencv.
    thank you !!!

  • Pierre FOUBERT says:

    Hi Vangos ! Are you able to estimate body angulations from your work on detection of skeleton with a single camera ? Great work !

  • Batu says:

    Hello,
    I am getting lots of “lnk2019 unresolved external symbol …….” errors. I’ve followed all your steps correctly but i getting thoose errors. Do you have an idea about the error? Thank you.

  • Robert says:

    Hello,
    First of all thanks a lot for this amazing tutorial. I followed all the steps you have mentioned.
    I am using opencv 3.4.0, cuda 9.1, visual studio community 2015, cmake 3.10.1

    Building the OpenCV.sln file resulted in success.

    The problem I am facing is that I am getting many “LNK 2019 unresolved external symbol” errors.

    I tried building the image.cpp file which is one of the samples provided by opencv and I get these errors

    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol “void __cdecl cv::merge(class cv::debug_build_guard::_InputArray const &,class cv::debug_build_guard::_OutputArray const &)” (?merge@cv@@YAXAEBV_InputArray@debug_build_guard@1@AEBV_OutputArray@31@@Z) referenced in function main trial_1 c:\Users\Nicky\documents\visual studio 2015\Projects\trial_1\trial_1\image.obj 1

    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol “void __cdecl cv::split(class cv::debug_build_guard::_InputArray const &,class cv::debug_build_guard::_OutputArray const &)” (?split@cv@@YAXAEBV_InputArray@debug_build_guard@1@AEBV_OutputArray@31@@Z) referenced in function main trial_1 c:\Users\Nicky\documents\visual studio 2015\Projects\trial_1\trial_1\image.obj 1

    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol “void __cdecl cv::cvtColor(class cv::debug_build_guard::_InputArray const &,class cv::debug_build_guard::_OutputArray const &,int,int)” (?cvtColor@cv@@YAXAEBV_InputArray@debug_build_guard@1@AEBV_OutputArray@31@HH@Z) referenced in function main trial_1 c:\Users\Nicky\documents\visual studio 2015\Projects\trial_1\trial_1\image.obj 1

    Severity Code Description Project File Line Suppression State
    Error LNK1120 3 unresolved externals trial_1 c:\users\nicky\documents\visual studio 2015\Projects\trial_1\x64\Debug\trial_1.exe 1

    Can you please help me in resolving these error. Thank you for your time !

  • Neil Anthony says:

    Hi Vangos, great tutorial.
    As the new branch of OpenCV is 4.X (with 3.4 being switched to maintanence mode), but does not have a build folder… Can you advise on how to proceed? Or is the above only valid for 3.4?
    Thanks in advance for your time.
    Neil

  • Lay khatri says:

    Hello, i appreciate the tutorial… But i was wondering, i work on opencv with Python in windows 10. So how to make that opencv enabled with GPU for Python in windows.
    Thanks in advance.

  • Anna C says:

    Hi Vangos.
    Thanks for your post! I am trying to build from sources openCV to exploit CUDA acceleration. I would like to use opencv4.1 and visual studio2019, but the linking does not seem to work.
    Do you have any suggestion on this regard?

    Thanks in advance
    Anna

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.