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.

[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!

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 page

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

and

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

Linker → General → Additional Library Directories

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\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.

[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!

Vangos Pterneas

Author Vangos Pterneas

Vangos Pterneas is an award-winning Microsoft Most Valuable Professional. He is helping companies from all over the world grow their revenue by creating profitable software products. He loves Motion Technology and Mixed Reality. Vangos is the CEO of LightBuzz Inc and author of The Dark Art Of Freelancing.

More posts by Vangos Pterneas

Join the discussion 28 Comments

  • blaisexen says:

    hi,
    could you update your binaries from opencv400 and cuda10,

    thanks

    • blaisexen says:

      opencv4.0.1 rather? I meant the latest.

      The tutorial helpful,
      I’m going to use for OpenCv(contrib->face) LBPH face recognition.

    • Sure, I’m planning to update the blog post with the latest release in the next weeks!

      • blaisexen says:

        hi,
        I successfully compiled(core2duo processor) with 0 failed,

        opencv_401_with_cuda BUT the file is too large, I mean Opencv_World401.dll = almost 1gb(916mb)

        I include this “modules”,
        cudev, cudaarithm, cudabgsegm, cudacodec, cudafeatures2d, cudafilters, cudaimgproc, cudalegacy, cudaobjdetect, cudaoptflow, cudastereo, cudawarping,
        bioinspired,
        face,
        dnn_objdetect,
        surface_matching,
        superres

        I have to use cudaobjdetect.hpp for “cuda::haarcascade…”(for face recognition with cuda enabled) that’s why I include “cudaobjdetect”

        did I do something wrong? I have no viruses here! or that was it?

        This are all the modules I had included:
        bioinspired, calib3d, core, cudaarithm, cudabgsegm, cudacodec, cudafeatures2d, cudafilters, cudaimgproc, cudalegacy, cudaobjdetect, cudaoptflow, cudastereo, cudawarping, cudev, dnn, dnn_objdetect, face, features2d, flann, gapi, highgui, imgcodecs, imgproc, java_bindings_generator, ml, objdetect, photo, python_bindings_generator, shape, stitching, superres, surface_matching, video, videoio, world

        • Hello! The file size is normal. Usually, the file size is bigger than 800MB, so I would consider that normal.

          • blaisexen says:

            hi,
            my problem is building opencv 3.0.0++ or 4.0.0++ with cuda in 32 bit x86, I tried cuda toolkit 6.5.19 32 bit in windows 7 32 bit system, but it wouldn’t work.

            any ideas how to build opencv with cuda in 32 bit,

            here are the results that I have from cmake 3.13.2,
            OpenNI2: YES (ver 2.2.0, build 33)

            NVIDIA CUDA: YES (ver 6.5, CUFFT CUBLAS NVCUVID)
            NVIDIA GPU arch: 20 30 35 37 50 52 60 61
            NVIDIA PTX archs:

            I hope you could also include the latest possible opencv and cuda in 32bit binaries with this modules, poor I have only core2duo processor.

            here are I want to build in x64(possible) and x86(impossible += ERRORS coming out):
            cudev, cudalegacy, cudaarithm, cudaimgproc, cudaobjdetect, cudawarping,
            bioinspired, dnn_objdetect, face, stitching, tracking, plot, superres, surface_matching, shape,
            calib3d, core, dnn, features2d, flann, highgui, imgcodecs, imgproc, ml, objdetect, photo, video, videoio, videostab,
            world

            thanks you

          • Building for 32-bit architecture is has limited support. Not sure how that could be accomplished.

          • blaisexen says:

            Hi,
            I have manage to build OpenCv_World345 and Cuda in 32 Bit in Windows 7 32 bit with MsVs 2013
            and Yes I’ve noticed the file size is normal, but I only pick up some modules that are important I think and are listed from my previous reply.
            I also included OpenNi2 in-case I would like to explore it.

            Actually I’m using it for C++Builder with face recognition(DNN+LBP) in a latest OpenCv,

            thank you so much for this tutorial,
            Blaise

            p.s
            it takes hours to build, lol

          • That’s great to hear! It’s definitely a pain, but worths it — given the performance advantages 🙂

          • blaisexen says:

            Hi,
            I created a very basic project file from sourceforge.net,

            here for the curiosity of others how did I do it or used it, that includes cuda version with Opencv _World345

            https://sourceforge.net/projects/opencv-world345-cbuilder-proj/

            thank you again
            blaise

          • Awesome! Thank you very much for sharing with me!

  • farshad says:

    Thanks a lot. Could you please update the cuda binaries for opencv 4.0.1? I tried but got many strange errors|:

  • Yuzhen Lu says:

    Very good post! I am curious running Cuda-enabled OpenCV in Qt5. I am working on Qt project with OpenCV3.4.5 in windows, but encountered many compiling errors in building my Qt project. You may check my post at Stack Overflow (https://stackoverflow.com/questions/54130898/cuda-related-compling-errors-in-qt5-with-opencv3-4-5) for these errors. I wonder if you have some ideas about that. Thanks!

  • Ken says:

    Thanks for this work!

    I’d love to be able to use CUDA support on OpenCV 4.0… (which is recently out…)

    Is there a reason to stick with the 3.4 series?

  • Tilo says:

    Hi Vangos, thanks a lot for this informative article!
    However, I tried opencv_traincascade from your download-binaries. It seems not to use the GPU. GPU-Shark shows no activity, training seems to run via CPU. Did I miss something?

  • Wawan says:

    Nice post. But, are you sure you are building OpenCV with CUDA Toolkit 9.1? When I run my code using your binaries, I got error “…because cudaart64_92.dll was not found…” and another *_92.dll error. I believe that “92” means “CUDA Toolkit 9.2”.
    Or, I dont understand naming convention correctly? Thank you.

    • Hello Wawan. Thank you for your message. Originally, the post (and binaries) was written for CUDA 9.1 and was later updated for CUDA 9.2. I assume that if you hard-refresh the page, you’ll be able to see the updated 9.2 images.

  • NitinH says:

    Vangos, do you have prebuilt OpenCV 4.0.0/1 with CUDA 10.1/2 uploaded ?
    I am having nightmare of a time to get them to build.
    Let us know, if you have fought and won the battles we are about to fight.

  • nicerova says:

    Hi, bro :
    I followed this tutorial but when I built INSTALL in openCV.snl I found this message 24>C:/opencv4/sources/opencv/modules/core/include\opencv2/core/cuda.hpp(723): warning : field of class type without a DLL interface used in a class with a DLL interface many times…
    I install with opencv_contrib and cmake but I couldn’t solve my problem, in addition to this , I couldn’t find opencv_world401.lib manually in my opencv folder … Please Help me !

  • Da says:

    Hi, thank you for this post. I’ve been trying to figure this out for a while.

    But I think you skipped over an entire step where you clone the contrib modules from the github. Otherwise, some of the options you mentioned in your notes don’t make sense, such as the cudafastmath.

    Also, where do I put the cudnn?

  • Andrei says:

    Hi, thank you for this post.

    I want to ask if I still need to install the CUDA toolkit if I am using the pre-build binaries? I haven’t installed it and I get cudart64_92.dll missing. I am new to CUDA and sorry if my question was dumb.

Leave a Reply

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