Install and configure Qt with OpenCV

From wiki.ferrari.mo.it
Jump to navigation Jump to search

Install Qt with OpenCV (Windows 10)[edit]

1. Download and install the file

qt-unified-windows-x86-3.1.1-online.exe

During installation (or you can restart the installation wizard from the Control Panel - Programs and Functionalities, select the following:

MinGW 7.3.0 64 bit
Qt 5.13.0 64 bit

2. To test the environment, go to File - New File or Project, then select 'Qt Console Application'. Give it a name, and leave the default build System 'qmake'.

In the next screen, deselect the default Kit, and instead, select the previously installed MinGW 7.3.0 64 bit.

Then, modify the main.cpp file in order to have this:

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << "Hello World";
    return a.exec();
}

Click on the debug icon. It should appear a text window with the output.

3. Compiler setup

Download and install CMake from cmake.org. During installation, select the option 'Add CMake to the PATH for all users'.

Go to Control Panel - System, and then to 'Advanced System Setting'. Go to the 'Advanced' tab and click on 'Environment Variables'. Add the MinGW path to the Path variable (in this case, it is C:\Qt\Tools\mingw730_64\bin).

4. Install OpenCV and compile its libraries

Download OpenCV from Sourceforge, then install it (file opencv-4.1.1-vc14_vc15.exe) into the C:\OpenCV\ directory.

Then, go to the CMake directory (C:\Program Files\CMake\bin) and execute cmake-gui.exe.

  • Where is the source code: C:/OpenCV/sources
  • Where to build the binaries: C:/OpenCV/opencv-build

Click next.

  • Specify the generator for this project: MinGW Makefiles
  • Specify native compilers
  • Compilers C: C:\Qt\Tools\mingw730_64\bin\gcc.exe
  • Compilers C++: C:\Qt\Tools\mingw730_64\bin\g++.exe

Click configure for the first time.

Then, a variable list should appear.

  • Check the box [X]WITH_QT
  • Check the box [X]WITH_OPENGL

Click configure for the second time. If the compiler complains about not finding the Qt5Config.cmake file, add the following variable:

  • Qt5_DIR = C:/Qt/5.13.0/mingw73_64/lib/cmake/Qt5

and click configure for the third time.

Some other variables should appear:

  • Qt5Concurrent_DIR = C:\Qt\5.13.0\mingw73_64\lib\cmake\Qt5Concurrent
  • Qt5Core_DIR = C:\Qt\5.13.0\mingw73_64\lib\cmake\Qt5Core
  • Qt5Gui_DIR = C:\Qt\5.13.0\mingw73_64\lib\cmake\Qt5Gui
  • Qt5Test_DIR = C:\Qt\5.13.0\mingw73_64\lib\cmake\Qt5Test
  • Qt5Widgets_DIR = C:\Qt\5.13.0\mingw73_64\lib\cmake\Qt5Widgets
  • Qt5OpenGL_DIR = C:\Qt\5.13.0\mingw73_64\lib\cmake\Qt5OpenGL

and others should be added:

  • CMAKE_BUILD_TYPE = Release
  • QT_MAKE_EXECUTABLE = C:\Qt\5.13.0\mingw73_64\bin\qmake.exe
  • Unckeck the box [ ]ENABLE_PRECOMPILED_HEADERS

then click configure for the fourth time. When finished, click generate.

Then, open a command prompt and, from the C:\OpenCV\opencv-build directory, execute

mingw32-make -j 4
mingw32-make install

Then, copy all the *.dll from C:\OpenCV\opencv-build\bin\ to C:\Qt\5.13.0\mingw73_64\bin\

5. Test on FriendlyCore (Ubuntu)

Follow the instructions about http://wiki.ferrari.mo.it/index.php/Installing_OpenCV_on_NanoPI_Neo to have a build environment.

Then, copy the Qt directory from Windows to the Nanopi.

Edit the Makefile, and change the Windows references, in this case:

INCPATH = -I. -IC:\OpenCV\build\include -isystem /usr/include/aarch64-linux-gnu/qt5 -isystem /usr/include/aarch64-linux-gnu/qt5/QtWidgets -isystem /usr/include/aarch64-linux-gnu/qt5/QtGui -isystem /usr/include/aarch64-linux-gnu/qt5/QtCore -I. -I. -I/usr/lib/aarch64-linux-gnu/qt5/mkspecs/linux-g++

to

INCPATH = -I. -I/usr/local/include/opencv4 -isystem /usr/include/aarch64-linux-gnu/qt5 -isystem /usr/include/aarch64-linux-gnu/qt5/QtWidgets -isystem /usr/include/aarch64-linux-gnu/qt5/QtGui -isystem /usr/include/aarch64-linux-gnu/qt5/QtCore -I. -I. -I/usr/lib/aarch64-linux-gnu/qt5/mkspecs/linux-g++

and

LIBS = $(SUBLIBS) C:\OpenCV\opencv-build\bin\libopencv_core411.dll C:\OpenCV\opencv-build\bin\libopencv_highgui411.dll C:\OpenCV\opencv-build\bin\libopencv_imgcodecs411.dll C:\OpenCV\opencv-build\bin\libopencv_imgproc411.dll C:\OpenCV\opencv-build\bin\libopencv_features2d411.dll C:\OpenCV\opencv-build\bin\libopencv_calib3d411.dll -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread

to

LIBS = $(SUBLIBS) /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgcodecs.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_calib3d.so -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread

then, run

make
/sbin/ldconfig -v

and test it.

Note: if the Makefile is not present, then run

qmake

to create it. If it's present and you want to avoid editing it, rename it and recreate.