Compiling OpenCV with CUDA support


Alight, so you have the NVIDIA CUDA Toolkit and cuDNN library installed on your GPU-enabled system.

What next?

Let’s get OpenCV installed with CUDA support as well.

While OpenCV itself doesn’t play a critical role in deep learning, it is used by other deep learning libraries such as Caffe, specifically in “utility” programs (such as building a dataset of images). Simply put, having OpenCV installed makes it easier to write code to facilitate the procedure of pre-processing images prior to feeding them into deep neural networks.

Because of this, we should install OpenCV into the same environment as our deep learning libraries, to at the very least, make our lives easier.

Furthermore, in a GPU-enabled CUDA environment, there are a number of compile-time optimizations we can make to OpenCV, allowing it to take advantage of the GPU for faster computation (but mainly for C++ applications, not so much for Python, at least at the present time).

I’ll be making the assumption that you’ll be installing OpenCV into the same environment as last week’s blog post — in this case, I’ll be continuing my example of using the Ubuntu 14.04 g2.2xlarge instance on Amazon EC2.

Truth be told, I’ve already covered installing OpenCV on Ubuntu in many previous blog posts, but I’ll explain the process here as well. Overall, the instructions are near identical, but with a few important changes inside the cmake  command, allowing us to compile OpenCV with CUDA support.

By the time you finish reading this blog post, you’ll have OpenCV with CUDA support compiled and installed in your deep learning development environment.

Installing OpenCV with CUDA support

Before we can compile OpenCV with CUDA support, we first need to install some prerequisites:

If you’re a follower of the PyImageSearch blog, then you’ll also know that I’m a big fan of using pip , virtualenv , and virtualenvwrapper  to create sequestered, independent Python virtual environments for each of our projects. You can install the virtual environment packages using the commands listed below (or you can skip this step if you already have Python virtual environments setup on your machine):

If this is your first time using Python virtual environments, I would suggest reading the first half of this blog post to familiarize yourself with them. The blog also has an excellent article on Python virtual environments for the uninitiated.

Next, let’s use update our ~/.bashrc  file. Open this file using your favorite command line text editor (such as nano , vi , or emacs ):

Then, scroll down to the bottom of the file, append the following lines, and save + exit the editor:

At this point, we can create our cv  virtual environment:

Note: Again, you’ll want to read the first half of this blog post to better understand Python virtual environments if this is your first time using them. I also explain them more thoroughly and how to properly use them in other OpenCV installation guides on this website.

Now, let’s download and unpack OpenCV. If you’re using the default Amazon EC2 g2.2xlarge instance, then I highly suggest that you download the OpenCV sources and do your compiling on /mnt .

The default g2.2xlarge instance has only ~8GB of space, which once you factor in the system files, NVIDIA drivers, etc., is not enough room to compile OpenCV from source:

Figure 1: The default disk size for the g2.2xlarge instance is only 8GB, which doesn't leave enough space to compile OpenCV from source.

Figure 1: The default disk size for the g2.2xlarge instance is only 8GB, which doesn’t leave enough space to compile OpenCV from source.

However, the /mnt  volume has 64GB of space, which is more than enough for our compile:

Figure 2: However, if we use the '/mnt' volume instead, we have 64GB -- far more than what is required to compile OpenCV.

Figure 2: However, if we use the ‘/mnt’ volume instead, we have 64GB — far more than what is required to compile OpenCV.

If you are indeed on an Amazon EC2 instance, be sure to change directory to /mnt  and create a directory specifically for your OpenCV compile prior to downloading the source:

The above command will create a new directory named opencv_compile  in the /mnt  volume, followed by giving the ubuntu  user permission to modify it at their will.

Note: The /mnt  volume is what Amazon calls “ephemeral storage”. Any data put on this volume will be lost when your system is stopped/rebooted. You don’t want to use /mnt  to store long-term data, but it’s perfectly fine to use /mnt  to compile OpenCV. Once OpenCV is compiled, it will be installed to the system drive — your OpenCV installation will not disappear between reboots.

For this tutorial, I’ll be using OpenCV 3.1. But you could also use OpenCV 2.4.X or OpenCV 3.0. Use the following commands to download the source:

In case the URLs of the .zip  archives are cutoff, I’ve included them below:

We are now ready to use cmake  to configure our build. Take special care when running this command, as I’m introducing some configuration variables you may not be familiar with:

To start, take note of the WITH_CUDA=ON  flag. Technically, this flag will be set to ON  by default since CMake is smart enough to detect that the CUDA Toolkit is installed. But, just in case, we’ll manually set the variable to WITH_CUDA=ON  to ensure CUDA support is compiled.

From there, we add in a few more optimizations, mainly around using cuBLAS, an implementation of the BLAS (Basic Linear Algebra Subprograms) library in the CUDA runtime.

We also indicate that we want to utilize the “fast math” optimizations, a series of extremely fast mathematical routines that are optimized for speed (they are written in Assembly) — and essentially perform little-to-no error checking. Again, the FastMath libraries are geared towards pure speed and nothing else.

After running cmake , take a look at the “NVIDIA CUDA” section — it should look similar to mine, which I have included below:

Figure 3: Examining the output of CMake to ensure OpenCV will be compiled with CUDA support.

Figure 3: Examining the output of CMake to ensure OpenCV will be compiled with CUDA support.

Notice how CUDA support is going to be compiled using both cuBLAS and “fast math” optimizations.

Provided that your own CMake command exited without error, you can now compile and install OpenCV:

If all goes well, the make  command should run successfully:

Figure 4: OpenCV with CUDA support has successfully compiled.

Figure 4: OpenCV with CUDA support has successfully compiled.

Again, assuming your compile finished without error, OpenCV should now be installed in /usr/local/lib/python2.7/site-packages . You can verify this using the ls  command:

Note: You’ll want to find and make note of where your  file is on your system! Whenever we create a virtual environment (which we’ll be doing lots of to explore various deep learning libraries), you’ll want to sym-link the  file into the site-packages  directory of your Python virtual environment so you have access to OpenCV.

The last step is to sym-link the  file (our Python bindings) into the cv  virtual environment:

To verify our installation, open up a new terminal, access the cv  virtual environment using the workon  command, fire up a Python shell, and then import OpenCV:

Finally, now that OpenCV is installed, let’s perform a bit of cleanup and remove the source files used for installation:

Again, I can’t stress this point enough — you need to get comfortable working with Python virtual environments, the site-packages  directory, and how to use symbolic links. I recommend the following tutorials to help understand each of them:


In today’s blog post, I detailed how to install OpenCV into our deep learning environment with CUDA support. While OpenCV itself isn’t directly used for deep learning, other deep learning libraries (for example, Caffe) indirectly use OpenCV.

Furthermore, by installing OpenCV with CUDA support, we can take advantage of the GPU for further optimized operations (at least from within C++ applications — there isn’t much support for Python + OpenCV + GPU, yet).

Next week, I’ll detail how to install the Keras Python package for deep learning and Convolutional Neural Networks — from there, the real fun will start!

, , , , , ,

23 Responses to Compiling OpenCV with CUDA support

  1. Mahendra Prabhu July 11, 2016 at 12:51 pm #


    I have struggling to compile OpenCV on EC2. Thank you for your detailed walk through. I will try this.

    Have you tried installing OpenCV on Google compute engine IaaS platform? I was initially leaning on that route to leverage using TensorFlow.

    • Adrian Rosebrock July 12, 2016 at 4:36 pm #

      I have not tried compiling OpenCV on any of Google’s services yet. I’ll consider that in the future.

  2. Hide Sirai July 24, 2016 at 4:28 am #

    Thanks for your tutorial.

    I just tried, and I think one thing should be noted for beginners.

    After making OpenCV, is located under build/lib.
    So you should move it to /usr/local/lib/python2.7/site-packages/
    with the following:
    sudo cp lib/ /usr/local/lib/python2.7/site-packages/

    • Adrian Rosebrock July 27, 2016 at 2:41 pm #

      After running sudo make install your file should be automatically copied to /usr/local/lib/python2.7/site-packages/. But if not, yes, absolutely manually copy it there.

      • Grant October 1, 2016 at 12:50 pm #

        I also have to copy the manually.

  3. Grant October 1, 2016 at 12:54 pm #

    Need to manually download ippicv_linux_20151201.tgz and copy to
    Else build will report error, said download failed. (maybe because of poor net connection? but when I manually download the file, the speed is fast)

    • Adrian Rosebrock October 2, 2016 at 8:58 am #

      This sounds like a poor internet connection issue. Normally when compiling OpenCV this file is automatically downloaded via the CMake process.

  4. Marcin October 4, 2016 at 7:16 am #

    Hi everyone,

    i just want to add if u have an issue with “‘memcpy’ was not declared in this scope” just add to cmake before running :cmake …. -DCUDA_NVCC_FLAGS=”-D_FORCE_INLINES” ….

    worked on ubt 16.04, cuda 7.5 gcc 4.9


    • Adrian Rosebrock October 6, 2016 at 7:03 am #

      Thanks for sharing Marcin!

  5. brocolpif December 2, 2016 at 7:37 am #

    Hi all,

    I had to apply these patches manually

    Don’t know why the 3..1 do not contains these patches.

  6. Joseph March 22, 2017 at 8:06 am #

    I don’t understand why you give python examples while OpenCV will NOT use Cuda functions via python

    • Adrian Rosebrock March 22, 2017 at 8:28 am #

      Correct, the GPU bindings with OpenCV are not available with Python, but you could call them within a Python script as a compiled C++ extension or you might be using CUDA support in another program. In either case, it’s important to see how OpenCV is hand compiled with CUDA support.

  7. Naif Allehyani April 1, 2017 at 6:51 pm #

    hello.. its been a rough day with opencv … cuda is installed and when i run nvcc -V it prints the cuda 7.5 that i am using.. then i tried to compile opencv with cuda by following this tutorial.. i had no problem and no errors and followed all the steps, cmake, make -j4, and sudo make install.. all worked fine.. but when i try to import cv2 it seems that its not installed.. when i list the files that are in /usr/local/lib/python2.7/site-packages its zero no files are there.. so i ran this command to locate the file and this was the results:
    naif@naif-Z170-D3H:~$ sudo find / -name “**”
    [sudo] password for naif:

    what went wrong?? any suggestions?

    • Adrian Rosebrock April 3, 2017 at 2:05 pm #

      Are you in the “cv” virtual environment before importing?

      Make sure you are in the “cv” virtual environment and that should resolve the issue.

      • naif allehyani April 6, 2017 at 12:29 pm #


        actually i didnt set up a virtual environment so i didnt need it, i reinstall and compile it , and got error is cmake, and found this in opencv docs that in some systems it might need to fix cmake macro, so i ran

        $ git cherry pick cdb9c

        and it worked fine.


  8. Moe June 25, 2017 at 7:18 pm #

    $ are so stupid and annoying.

    • Adrian Rosebrock June 27, 2017 at 6:30 am #

      The “$” sign in the commands simply indicates the terminal prompt. This is a standard practice when including and discussing commands that need to be executed inside a shell.

  9. Mustafa July 7, 2017 at 10:32 am #

    No module named cv2


    When I tried to show Total, it is 0…. and there is no What should i do? Platform: JetsonTK1, JetPack 3.0

    • Adrian Rosebrock July 11, 2017 at 6:56 am #

      1. Make sure you are in the “cv” virtual environment before importing cv2.
      2. Check the “build/lib” directory to ensure your Python bindings are compiled.
      3. Read through the troubleshooting suggestions in this guide.

  10. Melvyn Drag July 30, 2017 at 2:22 pm #

    For me installed in dist-packages. Seems fine as per

    I’m on Ubuntu16.10

    • Melvyn Drag July 30, 2017 at 2:30 pm #

      Oh and its worth mentioning that I installed 3.2 from the github source

      • Adrian Rosebrock August 1, 2017 at 9:43 am #

        Thanks for sharing Melvyn!


  1. Installing Keras for deep learning - PyImageSearch - July 24, 2016

    […] Compiling OpenCV with CUDA support […]

Leave a Reply