How to build OpenCV with Cuda and cuDNN support in Windows

Hey guys, in this blog we will see how we can build OpenCV with Cuda and cuDNN support in Windows. This process can be very tricky and tedious if doing it for the very first time. So without any further due, let’s do it.

Read the full blog with a detailed explanation here —

  • These are the versions we will be installing.
  • Opencv 4.5.5
  • Opencv-contrib 4.5.5
  • Visual Studio 2019

Step 0 — Uninstall Python and Anaconda.

  • You need to uninstall previous installations of python and Anaconda.
  • Install NumPy using pip install numpy.

Step 1 — Download Cmake

Step 2 — Download Visual Studio

  • We will download Visual Studio 2019.
  • Install “Desktop development with C++”.


Step 3 — Download OpenCV

Step 4 — Download opencv-contrib

Step 5 — Extract files.

  • Extract the OpenCV zip and OpenCV-contrib zip.
  • Create an empty folder named build.

Step 6 — Run Cmake GUI.

  • In Where is the source code, Select the main opencv extracted folder.
  • In Where to build the binaries, Select the empty build folder we created above.
  • Click on Configure button.
  • I have Visual Studio 2019 installed, so I selected Visual Studio 16 2019.
  • And in the Optional platform for generator, select x64.
  • Click on Finish and it will start configuring.
  • Once done you will see a screen like this.

Now search the following

  • WITH_CUDA and tick/check it.
  • OPENCV_DNN_CUDA and tick/check it.
  • ENABLE_FAST_MATH and tick/check it.
  • OPENCV_EXTRA_MODULES_PATH and browse to the module folder in opencv-contrib we extracted in step 5.
  • And now again hit on Configure button.
  • Once you see, Configuring done, check CUDA_FAST_MATH also.
  • Now go to this link and check your compute capability against your graphic card.
  • Mine is Nvidia GTX 1050Ti, so my arch is 6.1.
  • In the CMake window, search for CUDA_ARCH_BIN.
  • You will see something like this.
  • Delete all values except your Compute Capability.
  • Again hit on Configure button for the final time.
  • Once done, finally hit the Generate button.
  • Now you will see a lot of files in your build folder.

Step 7 — Build OpenCV with CUDA

  • We will have a file like this OpenCV.sln.
  • Now open the CMD Terminal in Administrator mode from start menu.
  • Change directory to your build folder usind cd command and run OpenCV.sln.
  • It will open up Visual Studio.
  • Once Visual Studio is opened, change Debug to Release.
  • Now in the right sidebar, open the CMake Targets dropdown and you will see ALL_BUILD.
  • Right click on it and select build and it will start building our binaries.
  • This process will take some time.
  • Once done it will prompt this.
  • Now right click on INSTALL and build.

Step 8 — Check OpenCV Installation.

  • Congratulations, you have successfully installed OpenCV with Cuda support.
  • Now let’s check if python is detecting cv2 or not.
  • Open cmd and paste the following commands.
import cv2
  • Now let’s check if cv2 is detecting CUDA or not.
  • Create a file and paste the following code in it and run it.
import numpy as np
import cv2 as cv
import time
npTmp = np.random.random((1024, 1024)).astype(np.float32)npMat1 = np.stack([npTmp,npTmp],axis=2)
npMat2 = npMat1
cuMat1 = cv.cuda_GpuMat()
cuMat2 = cv.cuda_GpuMat()
start_time = time.time()
cv.cuda.gemm(cuMat1, cuMat2,1,None,0,None,1)
print("CUDA --- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
print("CPU --- %s seconds ---" % (time.time() - start_time))

NOTE — If you are facing the below error while running cv2, follow these steps.

  • Download this file, extract it in a folder and copy the path to the folder where zlibwapi.dll file is located.
  • I extracted it in the opencv_build folder, you can extract it anywhere else also.
  • Now open the start menu and search env, then open “Edit the system Environment Variable”, click on Environment Variables…, then double click on the Path under System variables Section.
  • Click on New and paste the path you copied above.
  • And then simply click OK, OK and OK.

And now everything should work like charm!!!

So this is how you will install build OpenCV with CUDA and cuDNN support in windows, thanks for reading it and I hope you are taking something with you after reading this and till the next time 👋…

Check out my other machine learning projects, deep learning projects, computer vision projects, NLP projects, Flask projects at




Data Scientist || Blogger ||

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Make Text Bigger In Outlook For Mac

What Is NAS (Network Attached Storage)?

Bad Memory as a Software Engineer

Upskilling on Azure B2C custom policies — Introduction

Let’s talk HTTP: Get, Post, Put, and Delete

Stripe Webhooks in Phoenix with Elixir Pattern Matching

Obsession and Curiosity in a Career in Software Engineering

Do you know what happens when do you type a URL?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Sharma

Abhishek Sharma

Data Scientist || Blogger ||

More from Medium


Object Detection

Jump start in object detection with Detectron2 (license plate detection)

Histogram of an Image with python