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:
$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev $ sudo apt-get install libgtk2.0-dev $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libatlas-base-dev gfortran $ sudo apt-get install libhdf5-serial-dev $ sudo apt-get install python2.7-dev
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):
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py $ sudo pip install virtualenv virtualenvwrapper $ sudo rm -rf get-pip.py ~/.cache/pip
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 RealPython.com 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
):
$ nano ~/.bashrc
Then, scroll down to the bottom of the file, append the following lines, and save + exit the editor:
# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
At this point, we can create our cv
virtual environment:
$ source ~/.bashrc $ mkvirtualenv cv $ pip install numpy
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:
However, the /mnt
volume has 64GB of space, which is more than enough for our compile:
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:
$ cd /mnt $ sudo mkdir opencv_compile $ sudo chown -R ubuntu opencv_compile $ cd opencv_compile
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:
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip $ unzip opencv.zip $ unzip opencv_contrib.zip
In case the URLs of the .zip
archives are cutoff, I’ve included them below:
- https://github.com/Itseez/opencv/archive/3.1.0.zip
- https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
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:
$ cd opencv-3.1.0 $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D WITH_CUDA=ON \ -D ENABLE_FAST_MATH=1 \ -D CUDA_FAST_MATH=1 \ -D WITH_CUBLAS=1 \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.1.0/modules \ -D BUILD_EXAMPLES=ON ..
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:
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:
$ make -j8 $ sudo make install $ sudo ldconfig
If all goes well, the make
command should run successfully:
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:
$ ls -l /usr/local/lib/python2.7/site-packages total 2092 -rw-r--r-- 1 root staff 2138812 Jun 2 14:11 cv2.so
Note: You’ll want to find and make note of where your cv2.so
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 cv2.so
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 cv2.so
file (our Python bindings) into the cv
virtual environment:
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ $ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
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:
$ cd ~ $ workon cv $ python >>> import cv2 >>> cv2.__version__ '3.1.0' >>>
Finally, now that OpenCV is installed, let’s perform a bit of cleanup and remove the source files used for installation:
$ cd /mnt $ sudo rm -rf opencv_compile
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:
- Python Virtual Environments: https://realpython.com/blog/python/python-virtual-environments-a-primer/
- About site-packages: https://python4astronomers.github.io/installation/packages.html
- Symbolic links: https://kb.iu.edu/d/abbe
What's next? We recommend PyImageSearch University.
86+ total classes • 115+ hours hours of on-demand code walkthrough videos • Last updated: February 2025
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
That’s not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 86+ courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 86 Certificates of Completion
- ✓ 115+ hours hours of on-demand video
- ✓ Brand new courses released regularly, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 540+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
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!
Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF
Enter your email address below to join the PyImageSearch Newsletter and download my FREE 17-page Resource Guide PDF on Computer Vision, OpenCV, and Deep Learning.
Adriane,
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.
I have not tried compiling OpenCV on any of Google’s services yet. I’ll consider that in the future.
Thanks for your tutorial.
I just tried, and I think one thing should be noted for beginners.
After making OpenCV, cv2.so is located under build/lib.
So you should move it to /usr/local/lib/python2.7/site-packages/
with the following:
sudo cp lib/cv2.so /usr/local/lib/python2.7/site-packages/
After running
sudo make install
yourcv2.so
file should be automatically copied to/usr/local/lib/python2.7/site-packages/
. But if not, yes, absolutely manually copy it there.I also have to copy the cv2.so manually.
Need to manually download ippicv_linux_20151201.tgz and copy to
opencv-3.1.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e#
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)
This sounds like a poor internet connection issue. Normally when compiling OpenCV this file is automatically downloaded via the CMake process.
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
gl
Thanks for sharing Marcin!
Hi all,
I had to apply these patches manually
https://github.com/opencv/opencv/pull/6510/files
https://github.com/opencv/opencv_contrib/pull/532/files
Don’t know why the 3..1 do not contains these patches.
I don’t understand why you give python examples while OpenCV will NOT use Cuda functions via python
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.
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 cv2.so file and this was the results:
naif@naif-Z170-D3H:~$ sudo find / -name “*cv2.so*”
[sudo] password for naif:
/home/naif/opencv_compile/opencv-3.1.0/build/lib/cv2.so
/home/naif/.virtualenvs/cv/lib/python2.7/site-packages/cv2.so
naif@naif-Z170-D3H:~$
what went wrong?? any suggestions?
Are you in the “cv” virtual environment before importing?
Make sure you are in the “cv” virtual environment and that should resolve the issue.
hello
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.
Cheers
$ are so stupid and annoying.
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.
No module named cv2
:\
When I tried to show Total, it is 0…. and there is no cv2.so. What should i do? Platform: JetsonTK1, JetPack 3.0
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.
Hi!
For me cv2.so installed in dist-packages. Seems fine as per
https://stackoverflow.com/questions/9387928/whats-the-difference-between-dist-packages-and-site-packages
I’m on Ubuntu16.10
Oh and its worth mentioning that I installed 3.2 from the github source
Thanks for sharing Melvyn!
does this work for CUDA 9.1 too? i’m getting errors.
Hi ! I’m having some trouble compiling openCV with CUDA.
I am on Ubuntu 17.10 with python 3.6.4, environment managed by anaconda. My CUDA version is 9.1
I followed your instructions, expecting some errors as I don’t have the same setup.
My cmake command looks like this
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.1.0/modules \
-D CUDA_GENERATION=”” \
-D CUDA_ARCH_BIN=5.2 \
-D CUDA_NVCC_FLAGS=”-ccbin gcc-6″ \
-D ENABLE_PRECOMPILED_HEADERS=OFF ..
I manually added my cuda arch because I had some trouble with the autodetect parameter.
I set the compiler to gcc-6 because I read cuda doesn’t support newer versions.
Also, after wandering on loads of forums including one of your other tutorial, I read that enabling precompiled headers could leads to some problems so I deactivated it. My cmake doesnt show any error but I still cant compile opencv. Currently, the error is the following one, which doesn’t help me much…
/home/martin/opencv/opencv-3.1.0/modules/core/include/opencv2/core/cuda/vec_math.hpp(205): error: calling a constexpr __host__ function(“abs”) from a __device__ function(“abs”) is not allowed. The experimental flag ‘–expt-relaxed-constexpr’ can be used to allow this.
Any idea or advice taking in account my set up ? cheers
Hey Martin — thanks for the comment. I haven’t tested this tutorial with Ubuntu 17.04 or Python 3.6 so I’m not sure what the issue is off the top of my head. Which version of CUDA are you using? From there I might be able to point you in the right direction.
I installed cuda 9.1 and I have been able to use it with pyTorch so I assume it is correctly installed on my machine.
If you can use CUDA 9 with Pytorch then it certainly sounds like CUDA is properly configured on your machine. Unfortunately I’m not sure what the exact problem is. I would post on the official OpenCV forums as well as open a issue on the OpenCV GitHub. It sounds like it may be a CUDA + OpenCV incompatibility error.
After you post on both places try to compile with OpenCV 3.3 and OpenCV 3.4 to see if the newer versions are compatible with CUDA 9.
yeah ok I’ll try that. Thanks a lot !
I have successfully compiled opencv 3.4.1 version with CUDA 9.1. My cmake build looks like:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-9.1 \
-D WITH_CUBLAS=1 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.1/modules \
-D BUILD_EXAMPLES=ON ..
Hi, do you have to specify gcc-6 as well for CUDA?
And have you tried running and deep models using cv2.dnn.readNetFromCaffe(protoFile, weightsFile)?
Do they run at the rates you would expect from CUDA?
Folks, Need an advice, I am using OpenCV 3.4, Python 3, CUDA enabled and 1080 TI GPU, i am executing caffe model for object detection using a CCTV camera, but I see instead of GPU, Intel i7 is taking the processing 13% … can you please help me find a solution to only use GPU for processing …
Hi Adrian,
Can you please enlighten me, if opencv 3.4 python has CUDA support now; as i see when i execute caffe model in opencv for object detection using webcam, only proccessor is used and not the GPU and T-API UMat(OpenCL) also not faster. Can you please help me.
Thanks
Guru
OpenCV, CUDA, and OpenCL are different beasts. OpenCV hasn’t supported CUDA as a backend to their “dnn” module as far as I know.
Could you please explain what is OpenCL, I have an Nvidia GPU and I am confused about how OpenCL is related to Nvidia?
Thanks in advance.
I don’t have any tutorials on OpenCL at the moment but I will certainly consider it for a future post.
Sorry if I am repeating this post. I thought that I had sent it but I do not see it as having been posted.
My virtual environment does not find cv2.so. My normal shell does find it. cv2.so is in the proper directory, at least I think so.
/.virtualenvs/cv/lib/python2.7/site-packages
When in the non virtual python environment, cv2 is imported properly. When in the virtual environment I get:
“ImportError: No module named cv2”
Where has the link gone wrong.
Thank You
Tom
There are a few reasons why this may happen, but unfortunately it’s incredibly hard to diagnose. Make sure you refer to the “Troubleshooting and FAQ” section of this tutorial.
Thank you for the useful information.
If I install OpenCV with this method would I be able to run YOLO v3 (dnn- implementation- your blog post)on GPU?
I have an Nvidia GPU, and I’d like to run YOLO’s OpenCV implementation on GPU
Unfortunately, no. OpenCV’s “dnn” module does not yet support NVIDIA GPUs. Support is hopefully coming this summer though.
How come you do not include the full CMAKE command? Would be useful.
I’m not sure what you mean Luca, the full “cmake” command is included in the tutorial.
Thanks for this tutorial. I have a problem with using DNN_BACKEND_CUDA,
when I build OpenCV ver. 4.1.1 from sources,
I added all the CUDA options, include OPENCV_EXTRA_MODULES_PATH
opencv works till I try to use
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
then python says:
AttributeError: module ‘cv2.dnn’ has no attribute ‘DNN_BACKEND_CUDA’
all the recommends in the ML resources, they say
pip install python-opencv-contrib.
It helps, but this command installs complete opencv package with no cuda support at all. I’m stuck. How can I engage DNN_BACKEND_CUDA support? Please help.
Thanks again.
As I mention in the tutorial, you need to compile OpenCV from source if you want NVIDIA GPU support — you cannot use a pip install. I would suggest checking your CUDA_ARCH_BIN variable as that may be causing the problem for you.
I found… I use cv2.__version__ == 4.1.1 , which doesn’t support CUDA backend. Will try 4.2.0. It does.