Install OpenCV GPU with CUDA for Windows 10

pip install opencv gpu

You must install OpenCV for GPU if your system allows you. OpenCV library can be used for both CPU and GPU, but if you just install OpenCV by “pip” or “conda” command (pip install opencv-python) it will use CPU as a backend by default. Now if you are working on deep learning or video processing project like Object Detection, Social Distance detection, you will face lags in the output video (less frame rate per second), you can fix this lag using GPU if your system has NVIDIA GPU (NVIDIA Graphics card).

It’s easy to install OpenCV for GPU on the Linux machine but it is hard for Windows. That is why I am making this post.

To install OpenCV GPU on windows we have to compile or build the source code of Opencv with CUDA, cuDNN, and Nvidia GPU. To do that we need to use some tools like Visual Studio (C++’s GCC compiler), CMake, etc.

Must Read

System Requirement to build OpenCV with Cuda windows

Before you start to build OpenCV with Cuda for windows, make sure you have NVIDIA graphics in your system. To know about your graphics:

  • Go to “Device Manager” of your system
  • Click on “Display adapters
install opencv gpu
You must have NVIDIA graphics card

As you can see I have two graphics cards:

  1. NVIDIA GeForce GTX 1050 Ti
  2. Intel (R) UHD Graphics 630

Now visit below Wikipedia page to know your graphics card is eligible for CUDA Toolkit framework or not:

https://en.wikipedia.org/wiki/CUDA

opencv cuda python windows
CUDA eligibility for your NVIDIA GPU

Now search for your graphics card model name (in my case GTX 1050 Ti) on this page. As you can see my graphics card (GeForce GTX 1050 Ti) is listed on that Wikipedia page. So I can install OpenCV with Cuda for GPU access in my system.

Course for You: CUDA Programming Masterclass with C++

Steps to build OpenCV with Cuda for Windows

Below are the steps we are going to follow to install OpenCV with CUDA for windows operating system.

Note: I am using Windows 10 operation system. This tutorial is tested on windows 10 operation system.

  1. Uninstall anaconda or python and install fresh python
  2. Install “numpy” and uninstall “opencv-python”, “opencv-contrib-python”
  3. Download & install Visual Studio
  4. Download and install CUDA according to your GPU
  5. Download and install cuDNN according to CUDA
  6. Download & extract Opencv-4.4 Source from Github
  7. Download & extract Opencv-contrib-4.4 from Github
  8. Download & install CMake
  9. Make a new folder
  10. Make changes in CMake file “OpenCVDetectPython.cmake”
  11. Configure Opencv and Opencv-contrib using Cmake
  12. Build The project created by Cmake with Visual Studio
  13. Verify whether installations are correctly done

Step1 | Uninstall anaconda or python and install fresh python for all user

Follow this step otherwise there can be path issue later.

Also Read:  Find and Draw Contours with OpenCV in Python

1.1. Uninstall python or anaconda whatever you are using and install python a fresh python. I have downloaded python 3.9.5
1.2. Check the “system environment variables” make sure older path has been removed and current path is there

build opencv with cuda
Check variable path for Python

Step2 | Install “numpy” and uninstall “opencv-python”, “opencv-contrib-python”

Before compiling make sure “numpy” is installed. Make sure that “opencv-python” and “opencv-contrib-python” is uninstalled and will never be installed again using “pip” in this environment again

2.1. Install “numpy” (pip install numpy)
2.2. Uninstall opencv if you installed anaconda instead fresh Python (pip uninstall opencv-python)
2.3. Uninstall “opencv-contrib-python” if you installed anaconda instead fresh python (pip uninstall opencv-contrib-python)

In my case I have installed fresh python so I skipped step 2.2 and 2.3. I just followed step 2.1.

Step3 | Download & install Visual Studio

3.1. Download Community edition Visual Studio, In my case, I have downloaded Visual Studio 2019: https://visualstudio.microsoft.com/downloads/
3.2. Check on “Desktop development with C++”, and Continue with defaults and click on install

Download and Install Visual Studio 19
Install Visual Studio 19

Step4 | Download and install CUDA Toolkit according to your GPU

At this point hope you have already checked that your system allows you to install CUDA. See System Requirement section of this article, if you are missing anything.

Also Read: Install TensorFlow GPU with Jupiter notebook for Windows

4.1. Download CUDA from the below link. I have downloaded CUDA Toolkit 10.1
https://developer.nvidia.com/cuda-toolkit-archive

Download CUDA Toolkit
Download CUDA Toolkit

4.2. Once you downloaded, install CUDA Toolkit (keep everything default)
4.3. Make sure your CUDA_PATH & CUDA_PATH_V10_1 is there, if not then add those paths

Add CUDA variable path
CUDA variable path

Step5 | Download and install cuDNN according to CUDA

5.1 Download cuDNN according to CUDA

To download cuDNN you need to register on the NVIDIA website, then you can download cuDNN: https://developer.nvidia.com/rdp/cudnn-archive

5.1.1. Download and extract cuDNN (version which corresponds to your suitable Cuda version)

I have downloaded CUDA 10.1 so I am going to download cuDNN 7.6.5

Download cuDNN for cuda version
Download cuDNN

Note: If you click on a particular version of cuDNN, It will open various cuDNN libraries for the different operating systems. I am using windows 10 so I have downloaded cuDNN Library for Windows 10.

Download cuDNN for windows 10

5.2. Install cuDNN in Windows

5.2.1. Find CUDA installation folder, In my case: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1

5.2.2. Open downloaded and extracted cuDNN folder

5.2.3. Copy below files from cuDNN folder and paste on CUDA installation folder

  1. Copy all files (one file in my case) from CuDNN bin folder and paste inside CUDA (installation folder) bin folder
  2. Copy all files (one file in my case) from CuDNN include folder and paste inside CUDA (installation folder) include folder
  3. Copy all files (one file in my case) from CuDNN lib/x64 folder and paste inside CUDA (installation folder) lib/x64 folder

By doing that the installation of cuDNN is now finished.

Step6 | Download & extract Opencv-4.4 Source from Github

Before compiling make sure you have installed numpy library.

Also Read:  How to create boundary shape file from LiDAR data

6.1. Follow this link: https://opencv.org/releases/
6.2. Click on sources to download OpenCV source
6.3. Extract the downloaded folder

Download OpenCV 4.4.0 to build opencv with cudato
Download OpenCV 4.4.0

Step7 | Download & extract Opencv-contrib-4.4 from Github

We need one extra package which needs to be used along with opencv. This package is called Opencv-contrib.

7.1. Goto link https://github.com/opencv/opencv_contrib/tree/4.4.0 then click on code > download zip
7.2. Extract the downloaded folder

Step8 | Download & install CMake

8.1. Click this link to download CMake: https://cmake.org/download/

Step9 | Make a new folder

This is the folder where we will compile and save the object code. I am giving this folder name as “build”

9.1. Make a folder named “build”

create folder to build opencv with cuda
Create “build” folder

Step10 | Make changes in CMake file “OpenCVDetectPython.cmake”

If we try to compile OpenCV without any changes by default it will search for the python2 compiler. We should change the “OpenCVDetectPython.cmake” code so that it detects the python3 compiler by default.

10.1. Go inside extracted “opencv-4.4.0” folder
10.2. Find and open file “opencv-4.4.0\cmake\OpenCVDetectPython.cmake

edit code in Cmake
Edit code in Cmake file

10.3. Replace last code block of that Cmake file with below code:

Previous code:

if(PYTHON_DEFAULT_EXECUTABLE)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON2_EXECUTABLE AND PYTHON2INTERP_FOUND)
    # Use Python 2 as default Python interpreter
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
elseif(PYTHON3_EXECUTABLE AND PYTHON3INTERP_FOUND)
    # Use Python 3 as fallback Python interpreter (if there is no Python 2)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
endif()

Replace with this code:

if(PYTHON_DEFAULT_EXECUTABLE)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON3INTERP_FOUND) 
 # Use Python 3 as default Python interpreter
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
elseif(PYTHON2INTERP_FOUND) 
    # Use Python 2 as fallback Python interpreter (if there is no Python 3)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
endif()
replace code in Cmake file to install opencv for gpu
Replace this part

Step11 | Configure OpenCV and Opencv-contrib using Cmake

Now we need to configure OpenCV by providing paths of CUDA, cuDNN.

11.1. Open Cmake app which we have already installed in Step 8
11.2. Provide path of OpenCV source code which we have already downloaded in Step 6
11.3. Provide path of ‘build’ folder which we have already created in Step 9

In my case:

Cmake configuration to opencv cuda python windows

11.4. Click on “Configure” button
11.5. Then in Configure window select optional platform as x64
11.6. Click on “finish” button
11.7. Now configure following variables by searching and checking those variable in search tab:

WITH_CUDA — Check it
OPENCV_DNN_CUDA — Check it
ENABLE_FAST_MATH — Check it
OPENCV_EXTRA_MODULES_PATH — Provide path of “modules” directory from “opencv-contrib-4.4.0” directory(Step 7)
In my case: "C:\Users\Anindya\Downloads\opencv-contrib-4.4.0\modules"
check WITH_CUDA to build opencv with cuda windows 10
check OPENCV_DNN_CUDA to install opencv with cuda
check enable FAST_MATH to install opencv for gpu
provide EXTRA_MODULE_PATH to install opencv gpu

11.8. Hit configure button again wait for the “configuration done” output
11.9. Now we need to configure some more variables

CUDA_FAST_MATH — Check it
CUDA_ARCH_BIN — 6.1 (This is strictly for my case)

You can visit below Wikipedia page to know your GPU’s arch_bin

Wikipedia link: https://en.wikipedia.org/wiki/CUDA

You just visit that page and search for your graphics card model number (Check System Requirement section of this page). My Graphics card model is GTX 1050 Ti.

So on that Wikipedia page, I will search for GTX 1050 Ti, and in that row first column value is CUDA_ARCH_BIN value for my graphics card (GTX 1050 Ti). Which is 6.1

11.10. Click configure button again and wait for “configuration done” output
11.11. After that click the Generate button and wait for “Generating done” output

output message of Cmake to build opencv with cuda

11.12. Your configuration and generation of code are done. Now you can close the cmake-gui app

Also Read:  What is Reinforcement Learning in Machine Learning

Step12 | Build The project created by Cmake with Visual Studio

Now we need to build and compile the code configured by Cmake in our newly created folder named “build

12.1. Using cd command go to “build” folder
12.2. Type OpenCV.sln and hit enter. it will open Visual Studio for us

12.3. If you are getting error in Visual Studio saying:

Visual Studio error while building CUDA for installing Opencv GPU

12.3.1. In Visual Studio go to Tools > Options
12.3.2. In Options panel expand “Projects and Solutions” section > Web Projects
12.3.3. Uncheck the last option in that window

Fix visual studio error while building opencv with cuda

12.3.4. Click on OK button and restart Visual Studio by following Step 12.1 and 12.2

12.4. Now inside Visual Studio change “debug” mode to “release” Mode

12.5. Inside Visual Studio expand “CMakeTargets” (Located at right)
12.6. Right-click “ALL_BUILD” and click build. This may take around 30 minutes to complete.

12.7. Now right-click “INSTALL” (from same “CMakeTargets”) and click build. This wont take long time.

12.8. Make sure that, there is no error or failed in Step 12.6 and 12.7
12.9. Finally you built and compile opencv with cuda and cuDNN. Now you can utilize your NVIDIA GPU while working with opencv.

Step13 | Verify whether installations are correctly done

To check whether you have sucessfully installed opencv with cuda for windows or not.

13.1. Open the command prompt > type python > hit Enter
13.2. Type the below code in the python console

import cv2
cv2.__version__
cv2.cuda.getCudaEnabledDeviceCount()
Verify OpenCV GPU installation

Must Read

Conclusion

I know this is a very long and hectic process to build OpenCV with Cuda to install OpenCV for GPU. But I think it is worth doing this to minimize our day-to-day video analysis time, increase the durability of your system as while doing video analysis with CPU, your CPU utilization will be almost 100% all the time.

If you are aiming to build a solid career in the field of Computer Vision, having a clear understanding of the basics is crucial. That’s why I recommend taking this course – Python for Computer Vision with OpenCV and Deep Learning.

17 thoughts on “Install OpenCV GPU with CUDA for Windows 10”

  1. i had some problem after installing opencv cuda. i thought the process going smoothly bcos there isnt any error that stop the process. So i try to check whether the opencv is cuda supported or not with getCudaEnabled…(). the function return 0, my opencv isnt installed with cuda. any suggestions?

    Reply
  2. After Installation, can the ‘build’ folder be deleted?
    I am getting this error while importing cv2:

    “return _bootstrap._gcd_import(name[level:], package, level)
    ImportError: DLL load failed while importing cv2: The specified module could not be found.”

    Reply
  3. I followed every step but at the end when checking if i installed them
    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ModuleNotFoundError: No module named ‘cv2’
    >>> cv2.__version__
    Traceback (most recent call last):
    File “”, line 1, in
    NameError: name ‘cv2’ is not defined
    >>> cv2.cuda.getCudaEnabledDeviceCount()
    Traceback (most recent call last):
    File “”, line 1, in
    NameError: name ‘cv2’ is not defined

    I would appreciate it if you could reply

    Reply
  4. Thank you so much for this great tutorial! I just compiled opencv-4.7 using Visual Studio 2022 with only one minor adaptation:
    I needed to install also nasm (https://www.nasm.us/ — i used version 2.16.01) for libjpeg-turbo. Without it, almost everything failed building.
    Thanks again!

    Reply
  5. I have this error… i have installed everything still this error is there:

    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    File “C:\Users\sys\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py”, line 96, in
    bootstrap()
    File “C:\Users\sys\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py”, line 59, in bootstrap

    load_first_config([
    File “C:\Users\sys\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py”, line 56, in load_first_config
    raise ImportError(‘OpenCV loader: missing configuration file: {}. Check OpenCV installation.’.format(fnames))
    ImportError: OpenCV loader: missing configuration file: [‘config-3.10.py’, ‘config-3.py’]. Check OpenCV installation.
    >>>

    Reply
  6. this error is popping up

    C:\Users\sys>python
    Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)] on win32
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    File “C:\Users\sys\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py”, line 96, in
    bootstrap()
    File “C:\Users\sys\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py”, line 59, in bootstrap
    load_first_config([
    File “C:\Users\sys\AppData\Local\Programs\Python\Python310\lib\site-packages\cv2\__init__.py”, line 56, in load_first_config
    raise ImportError(‘OpenCV loader: missing configuration file: {}. Check OpenCV installation.’.format(fnames))
    ImportError: OpenCV loader: missing configuration file: [‘config-3.10.py’, ‘config-3.py’]. Check OpenCV installation.
    >>>

    Reply

Leave a comment