Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Unbinilium/13495c2fa236cc31c1b2bfb929c7e0db to your computer and use it in GitHub Desktop.
Save Unbinilium/13495c2fa236cc31c1b2bfb929c7e0db to your computer and use it in GitHub Desktop.
Build OpenCV with CUDA and TBB Visual Studio

OpenCV is a library of programming functions mainly aimed at real-time computer vision and it officially compiled without support for NVIDIA CUDA, INTEL TTB and OpenCL library, that's why we need to rebuild OpenCV with a custom configuration manually.

CUDA is a parallel computing platform and programming model developed by Nvidia for general computing on its own GPUs (graphics processing units). CUDA enables developers to speed up compute-intensive applications by harnessing the power of GPUs for the parallelizable part of the computation.

Intel TBB (Threading Building Blocks) makes parallel performance and scalability accessible to software developers who are writing loop- and task-based applications. Build robust applications that abstract platform details and threading mechanisms while achieving performance that scales with increasing core count.

To begin with, these software libraries should be pre-installed and configured correctly:

If there's a Anaconda user, make sure Anaconda is your default Python. You can also try tick Register Anaconda as my default Python button when installing Anaconda.

To accelerate C++ build speed, try Ninja build system as optional software:

For system, I recommend you at least meet the minimal requirements below:

  • CPU: Core i7 9750H or higher
  • RAM: 8G DDR4 or bigger
  • STROAGE: 60GB or bigger
  • NETWORK: 10Mbps or higher with no dependencies' host blocked or proxy passthrough

Then download both OpenCV and OpenCV Contrib same version source from Github release and unzip them after download finished with MD5 checked.

To compile by MSBuild with CMake configured, open CMake-GUI first. Firstly click Browse Source button, select path to the folder OpenCV unzipped folder. Secondly click Browse Build, create a folder named build in the path to the folder OpenCV unzipped folder, then select path to the build folder. For example the path should be like this:

Where is the source code: C
Where to build the binaries: C:/opencv/build

Click Configure button then it pop-up a window, you have to change Specify the generator for the project to Visual Studio XX 20XX (See the newest Visual Studio you have installed, If you want to use Ninja to accelerate build you have to set generator to Ninja) and keep other selection as default if it had already configured.

Then click Finish button. The missing or unconfigured part will covered with RED background, the other error is in RED text in output window, click Configure button again to check if it solved.

Next we add OPEVCV_EXTRA_MODULES_PATH with path to the folder OpenCV Contrib unzipped, use the modules folder ~/modules, for example my own path listed here:

C:/opencv_contrib/modules

Next click Configure button again and fix missing path or any error until there is no errors displayed.

When finished default build configuration, it's time to add our custom configuration. Use Search bar which located on the top of CMake-GUI to search flags WITH_CUDA, WITH_TBB and WITH_OPENGL, all tick selected(If you want to build OpenCV Python version, tick OPENCV_PYTHON3_VERSION selected).

Since NVIDA Developer removed cudacodec, for OpenGL support you have to unzip NVIDIA Video Codec SDK, then rename Lib folder to lib and copy include & lib folder to NVIDIA GPU Computing Toolkit install path like C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.X. Last you have to tick WITH_NVCUVID selected in CMake-GUI.

Then click Configure button again and find error the RED background covered. Generally the Intel TBB install path is in C:\Program Files(x86)\IntelSWTools\compilers_and_libraries\windows. The configurations example here:

  • TBB_DIR
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\redist\intel64_win\tbb\vc_mt
  • TBB_ENV_INCLUDE
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\tbb\include
  • TBB_ENV_LIB
C:/Program Files (x86)/IntelSWTools/compilers_and_libraries/windows/tbb/lib/intel64/vc_mt/tbb.lib
  • TBB_ENV_LIB_DEBUG
C:/Program Files (x86)/IntelSWTools/compilers_and_libraries/windows/tbb/lib/intel64/vc_mt/tbb_debug.lib
  • TBB_VER_FILE (If TBB_DIR not found)
C:/Program Files (x86)/IntelSWTools/compilers_and_libraries/windows/tbb/include/tbb/tbb_stddef.h

Next click Configure button again and fix missing path or error until there is no error in output as RED background covered. Now click Generate button to generate INSTALL.vcxproj in folder ~\opencv\build and make there's no error displayed.

Now we start build with MSBuild and we'd better declare how many CPU cores in order to increase build speed. Firstly we open CMD and enter wmic cpu get NumberOfLogicalProcessors and outputs like this:

NumberOfLogicalProcessors
12

It means we have 12 logical processor for compiling if 1 thread uses 1 logical processor. Secondly we open xXX Native Tools Command Prompt for VS 20XX and use cd <~\opencv\build> to enter build folder. For example:

cd C:\opencv\build

Then build by msbuild INSTALL.vcxproj /m:12 /p:Configuration=Release to build OpenCV for RELEASE use. The arguments /m:12 means use 12 threads to build. Be patient, it takes a long time to build.

At last the build finished and shows:

9232 Warning(s)
0 Error(s
Time Elapsed 02:06:53.91

Don't mind the Warnings. Take a look in folder~\opencv\build\install, all the compiled files here are ready to use. Just set up OpenCV variables path in your VS project and build you project with Release Windows Local Debugger.

Add .lib may be annoying, try ls '~\opencv\install\x64\vc16\lib' | Select-Object -Property Name in PowerShell and copy it when editing Property Pages->Linker->Input->Additional Dependencies in Visual Studio project.

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