Build OpenCV 4.5.1 with GPU (CUDA) Support on Windows 10 (without tears)
Hello everyone! Today we are going to build OpenCV 4.5.1 with CUDA on Windows 10. I will try to keep it as simple as possible. Note that this quick guide is for the base environment of Anaconda. However, it can easily be adapted to the native Python.
Step 1: Download and install Anaconda with default options.
Step 2: Download and install Visual Studio Community Edition 2019 from here.
Step 3: Download and install CUDA 11.0 with default options.
Step 4: Download cuDNN version 8.05 for CUDA 11.0. Extract the file. Copy all the contents of the extracted folder and replace at:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0
Step 5: Download and install CMake GUI with default options.
Step 6: Download OpenCV source from here and download OpenCV contrib from here. Make sure the version matches with OpenCV.
Step 7: Extract OpenCV and OpenCV contrib zip files. Create an empty folder called build.
Step 8: Open CMake GUI and browse for OpenCV source folder that we extracted above. Browse for build folder that we created above.
Step 9: Click on Configure and select X64 platform and hit Finish.
Step 10: New options will appear in CMake in red color. Tick these checkboxes there: WITH_CUDA, OPENCV_DNN_CUDA, ENABLE_FAST_MATH, BUILD_OPENCV_WORLD
Step 10: On the same window, go to OPENCV_EXTRA_MODULES_PATH and browse for OpenCV contrib directory and point to the modules subfolder. Delete Debug from CMAKE_CONFIGURATION_TYPES.
Step 11: Hit Configure again. You will see new options in red color. Tick CUDA_FAST_MATH checkbox. From CUDA_ARCH_BIN property, remove any compute architecture that your model of nVidia GPU does not support. You can find a list of compatible compute architectures for your model of GPU here.
Step 12: Hit Configure and then Generate.
Once the build is finished, open Command Prompt as Administrator and type the following command (With quotes and everything but change the path to build folder):
"C:\Program Files\CMake\bin\cmake.exe" --build "C:\OpenCV_Build\build" --target INSTALL --config Release
The installation will take a while! Once finished, you can import OpenCV in the base environment of Anaconda.
If you would like to know how to build for virtual environment and solution to the possible errors, refer to this step by step video tutorial.