Skip to content

Instantly share code, notes, and snippets.

@Maghin
Last active May 11, 2023 04:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Maghin/8bc7fca086361158c648837be5e894a0 to your computer and use it in GitHub Desktop.
Save Maghin/8bc7fca086361158c648837be5e894a0 to your computer and use it in GitHub Desktop.
Build OpenCV 4.5.5 on Windows VS2019 with CUDA, DNN, Qt, TBB and more

Build OpenCV 4.5.5 on Windows VS2019 with CUDA, DNN, Qt, OpenGL, TBB, VTK.

This documentation explain how to build the latest OpenCV version - 4.4.5 20/02/2022 - from sources.

We will focus on C++ and not build any Python libs.

Prerequisites

Install theses tools with common option :

  • Visual Studio 2019 link
  • cmake link
  • QT 5.15.2 link
    • Select Custom install, tick MSVC2019 and untick MINGW64.
    • Do not take QT6, uninstall any other Qt installation to avoid wrong version usage. (Qt6 wants C++17 but OpenCV force C++11 usage, so we use Qt5)
    • Add PATH_TO_Qt/5.15.2/msvc2019_64/bin to your %PATH% environnement variable
  • CUDNN link
    • Check the latest available version before installing CUDA because you need compatibility with your installed CUDA version
  • CUDA link
    • Your %PATH% and other envvars are updated automaticly.
  • TBB link
    • Add PATH_TO_TBB/redist/intel64/vc14 to your %PATH% environnement variable.
  • VTK link
    • Build from sources, tick imaging and QT options
    • Build Release and Debug configurations

Optionnal :

  • OGRE3D is optionnal to generate nices graphs
  • Anaconda if you want Python but its not describe here.

CMAKE Configuration

Download opencv 4.5.5 Download opencv-contrib

Open CMAKE-GUI

Select OpenCV 4.5.5 source folder and set a build folder. Use a fast SSD drive for the build folder.

Click Configure (#1]

Keep default option for compiler, Visual Studio 16 2019 should be selected.

Look for "python" and untick everything.

Set OPENCV_EXTRA_MODULES_PATH to OpenCV 4.5.5 source folder /modules (ex: D:/opencv-contrib/modules).

Set CMAKE_INSTALL_PREFIX to a folder outside the build folder. This way it will export all necessary files and you will be able to delete the build folder to free up some diskspace.

Prepare OpenCV Environnement Variables according to the CMAKE_INSTALL_PREFIX you defined:

  • Add an env var OPENCV_DIR with the value CMAKE_INSTALL_PREFIX.
  • Add to %PATH% to value %OPENCV_DIR%\bin.

Tick:

  • WITH_CUDA
  • OPENCV_DNN_CUDA
  • WITH_QT
  • WITH_OPENGL
  • ENABLE_FAST_MATH
  • WITH_TBB

Untick:

  • ENABLE_PRECOMPILED_HEADERS
  • OPENCV_GENERATE_SETUPVARS
  • BUILD_PERF_TESTS
  • BUILD_TESTS

Click Configure (#2)

Set CUDA_ARCH_BIN to 6.1 (GeForce1070)

Check CUDA wiki to know the value you need to use depending on your graphic card.

Tick:

  • WITH_CUDNN
  • CUDA_FAST_MATH

Important: Check Qt paths ! Be sure to use the one you installed previously. Edit each path if needed.

Click Configure (#3)

Click Generate

BUILD

Click Open project (in Visual Studio 16 2019)

Select Debug Configuration

Expand modules and look for opencv_highgui. Right click on it and go to "Link Editor -> Input" and add opengl32.lib to the existing additionnals libs. You will need to do it again for the Release configuration.

Expand CMakeTargets and Build the projet ALL_BUILD. It takes lots of time, be sure to not let your computer go to sleep. When done you should not have any error.

Then build the project INSTALL.

Here you may want to do a first test with a C++ project compiled in Debug mode. Do some image processing and create an OpenCV Window to check if everything is working. The Debug mode will help you to troubleshout issues.

Select Release Configuration

Same steps as Debug configuration: check libs, build then install.

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