How to setup Qt and openCV on Windows with MSVC2017

From Qt Wiki
Revision as of 18:53, 31 October 2019 by Bgallois (talk | contribs) (Creating How to setup Qt and openCV on Windows with MSVC2017)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

This article shows how to build OpenCV (or any external library using cmake) on Windows with the MSVC2017 compiler and using the QtCreator to build applications using the OpenCV library. This can be necessary if you want to use qtwebengine currently not supported by MinGW compiler. This article assumes a standard installation of Windows 10 and Qt.

Installing MSVC2017

The MSVC 2017 compiler can be installed as a part of Visual Studio Community 2017 (version 15.9) available at this link. During the installation process do not forget to select "Visual Studio Build Tools 2019" to install the build tools.

Building OpenCV

CMake

Download CMake from https://cmake.org/download/ and install it.

Getting openCV

Download the latest version of openCV at https://sourceforge.net/projects/opencvlibrary/files/. In this example, we will be using version 4.1.0. Execute opencv-4.1.0-vc14_vc15.exe and extract to C:\.

Create a new folder C:\opencv_build.

Compiling OpenCV

Start CMake-gui and then choose the following settings:

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

Click the Configure button and choose:

  Visual Studio 15 2017
  You can choose x64 if you want to build a 64bit application, in this tutorial will be using a 32bit version.

Use default native compilers.

Click on Finish.

You can then change the default settings by selecting options and then click on Configure.

Then click on Generate.

Open the Windows PowerShell and type:

  cd 'C:\opencv_build\'
  cmake.exe --build . --config Debug --target INSTALL

Or if you want to be in release mode:

  cmake.exe --build . --config Release --target INSTALL

Then add OpenCV to the Path variable:

  Type "Path" in the Windows search bar and select "Edit the system environment variables"
  Click on "Environment Variables"
  Double click on "Path"

And add C:\opencv_build\install\x86\vc15\bin to the Path variable (necessary to find OpenCV dll at runtime).

Configuring Qt

In your project, select the Desktop Qt 5.13.1 MSVC2017 32bit kit (or Desktop Qt 5.13.1 MSVC2017 64bit if you have compiled openCV in 64bit).

Now add the OpenCV library in your project.pro file (unlike MinGW you link against .lib and not .dll):

In debug mode:

  LIBS += -LC:\opencv_build\install\x86\vqtc15\lib -lopencv_core410d -lopencv_imgproc410d -lopencv_highgui410d -lopencv_imgcodecs410d -lopencv_videoio410d -lopencv_video410d -lopencv_calib3d410d -lopencv_photo410d -lopencv_features2d410d
  INCLUDEPATH += C:\opencv_build\install\include
  DEPENDPATH += C:\opencv_build\install\include

In release mode:

  LIBS += -LC:\opencv_build\install\x86\vc15\lib -lopencv_core410 -lopencv_imgproc410 -lopencv_highgui410 -lopencv_imgcodecs410 -lopencv_videoio410 -lopencv_video410 -lopencv_calib3d410 -lopencv_photo410 -lopencv_features2d410
  INCLUDEPATH += C:\opencv_build\install\include
  DEPENDPATH += C:\opencv_build\install\include

Your OpenCV/Qt application should compile and run without problems.