Skip to content

Instantly share code, notes, and snippets.

@Harold2017
Created April 23, 2022 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Harold2017/e1c2d50146d02fb461a7e016adb20907 to your computer and use it in GitHub Desktop.
Save Harold2017/e1c2d50146d02fb461a7e016adb20907 to your computer and use it in GitHub Desktop.
build OpenCV with cuDNN on windows

Install NVIDIA toolkits

follow this NVIDIA instructions: https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#install-windows

Notice: need to ensure zlibwapi.dll in system path (e.g. put it in C:/Windows/system32 or add it to system path) to avoid the following error:

Could not locate zlibwapi.dll. Please make sure it is in your library path!

Build OpenCV

use the following installOCV.sh script, and revise the CMAKE_OPTIONS to meet your customized requirements:

#!/bin/bash -e

# https://docs.opencv.org/4.x/d3/d52/tutorial_windows_install.html

myRepo=$(pwd)
CMAKE_GENERATOR_OPTIONS=-G"Visual Studio 16 2019"
#CMAKE_GENERATOR_OPTIONS=-G"Visual Studio 15 2017 Win64"
#CMAKE_GENERATOR_OPTIONS=(-G"Visual Studio 16 2019" -A x64)  # CMake 3.14+ is required
if [  ! -d "$myRepo/opencv"  ]; then
    echo "cloning opencv"
    git clone https://github.com/opencv/opencv.git
else
    cd opencv
    git pull --rebase
    cd ..
fi
if [  ! -d "$myRepo/opencv_contrib"  ]; then
    echo "cloning opencv_contrib"
    git clone https://github.com/opencv/opencv_contrib.git
else
    cd opencv_contrib
    git pull --rebase
    cd ..
fi
RepoSource=opencv
mkdir -p build_opencv
pushd build_opencv
CMAKE_OPTIONS=(-DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DWITH_CUDA:BOOL=ON -DWITH_CUDNN:BOOL=ON -DOPENCV_DNN_CUDA:BOOL=ON -DBUILD_CUDA_STUBS:BOOL=ON -DOPENCV_ENABLE_NONFREE:BOOL=ON -DENABLE_FAST_MATH:BOOL=ON -DCUDA_FAST_MATH:BOOL=ON -DCUDA_ARCH_BIN=6.1 -DWITH_CUBLAS:BOOL=ON -DBUILD_opencv_python:BOOL=OFF -DBUILD_JAVA:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF -DINSTALL_CREATE_DISTRIB=ON)
set -x
cmake "${CMAKE_GENERATOR_OPTIONS[@]}" "${CMAKE_OPTIONS[@]}" -DOPENCV_EXTRA_MODULES_PATH="$myRepo"/opencv_contrib/modules -DCMAKE_INSTALL_PREFIX="$myRepo/install/$RepoSource" "$myRepo/$RepoSource"
echo "************************* $Source_DIR -->debug"
cmake --build .  --config debug
echo "************************* $Source_DIR -->release"
cmake --build .  --config release
cmake --build .  --target install --config release
cmake --build .  --target install --config debug
popd

Or build with cmake gui by setting those options manually.

Notice: set the CUDNN_LIBRARY to the path of your cudnn.lib (C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/lib/x64/cudnn.lib if followed the NVIDIA intructions).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment