Book Image

Computer Vision with OpenCV 3 and Qt5

By : Amin Ahmadi Tazehkandi
4 (1)
Book Image

Computer Vision with OpenCV 3 and Qt5

4 (1)
By: Amin Ahmadi Tazehkandi

Overview of this book

Developers have been using OpenCV library to develop computer vision applications for a long time. However, they now need a more effective tool to get the job done and in a much better and modern way. Qt is one of the major frameworks available for this task at the moment. This book will teach you to develop applications with the combination of OpenCV 3 and Qt5, and how to create cross-platform computer vision applications. We’ll begin by introducing Qt, its IDE, and its SDK. Next you’ll learn how to use the OpenCV API to integrate both tools, and see how to configure Qt to use OpenCV. You’ll go on to build a full-fledged computer vision application throughout the book. Later, you’ll create a stunning UI application using the Qt widgets technology, where you’ll display the images after they are processed in an efficient way. At the end of the book, you’ll learn how to convert OpenCV Mat to Qt QImage. You’ll also see how to efficiently process images to filter them, transform them, detect or track objects as well as analyze video. You’ll become better at developing OpenCV applications.
Table of Contents (19 chapters)
Title Page
Dedication
Packt Upsell
Foreword
Contributors
Preface

Introduction to Qt


You have heard about it or maybe even used it knowing it. It's the foundation of many world famous commercial and open source applications, such as VLC Player, Calibre, and so many more. The Qt Framework is used by a majority of the so-called Fortune 500 companies, and we can't even begin to define how widely used and popular it is among the many application development teams and companies in the world. So, we'll start with an introduction and take it from there.

Let's first go through a brief introduction to the Qt Framework to get us on our feet. Nothing can make you feel more comfortable with a framework than having a clear picture of the whole thing in your mind. So, here we go, currently built and managed by The Qt Company, Qt Framework is an open source application development framework that is widely used to create visually rich and cross-platform applications that can be run on different operating systems or devices with very little or no effort at all. To break it down further, open source is the most obvious part of it. It means you can access all of the source code for Qt. By visually rich, we mean enough resources and capabilities are present in Qt Framework to write very beautiful applications. As for the last part, cross-platform, this basically means that, if you develop an application using Qt Framework modules and classes for Microsoft Windows operating system, for instance, then it can be compiled and built for macOS or Linux exactly as it is, without changing a single line of code (almost), provided that you don't use any non-Qt or platform-specific libraries in your application.

At the time of writing this book, Qt framework (or simply Qt from here on) is at version 5.9.X and it contains many modules for almost any purpose in developing applications. Qt divides those modules into these four main categories:

  • Qt Essentials
  • Qt Add-Ons
  • Value-Add Modules
  • Technology Preview Modules

Let's see what they are and what they include since we'll be dealing with them a lot throughout this book.

Qt Essentials

These are the modules promised by Qt to be available on all supported platforms. They are basically the foundation of Qt and they contain the majority of the classes that are used by almost all Qt applications. Qt Essential modules include all of the general-purpose modules and classes. Pay real attention to the words general-purpose, since it's exactly what these modules are used for. The following is a brief list for a quick study of the modules and for later reference purposes:

Module

Description

Qt Core

These are core non-graphical classes used by other modules.

Qt GUI

These are base classes for graphical user interface (GUI) components. These include OpenGL.

Qt Multimedia

These are classes for audio, video, radio, and camera functionality.

Qt Multimedia Widgets

These are widget-based classes to implement multimedia functionality.

Qt Network

These are classes to make network programming easier and more portable.

Qt QML

These are classes for QML and JavaScript languages.

Qt Quick

This is a declarative framework to build highly dynamic applications with custom user interfaces.

Qt Quick Controls

These are reusable Qt Quick based UI controls to create classic desktop-style user interfaces.

Qt Quick Dialogs

These are types to create and interact with system dialogs from a Qt Quick application.

Qt Quick Layouts

These layouts are items that are used to arrange Qt Quick 2 based items in the user interface.

Qt SQL

These are classes for database integration using SQL.

Qt Test

These are classes for unit testing Qt applications and libraries.

Qt Widgets

These are classes to extend Qt GUI with C++ widgets.

 

For information, refer to http://doc.qt.io/qt-5/qtmodules.html.

Note that it's not possible and perhaps not a good idea to cover all modules and all classes in this book, and, for the most part we will stick to whatever module and class we require; however, by the end of the book you'll feel comfortable enough exploring all the numerous and powerful modules and classes within Qt by yourself. You'll learn how to include a module and class in your projects in the upcoming chapters, so for now, let's not bother with too much detail and just focus on having a picture of what Qt really is and what it contains in our minds.

Qt Add-Ons

These modules may or may not be available on all platforms. This means they are used for developing specific capabilities as opposed to the general-purpose nature of Qt Essentials. A few examples of these type of modules are Qt 3D, Qt Print Support, Qt WebEngine, Qt Bluetooth, and many more. You can always refer to the Qt documentation for a complete list of these modules, and in fact, they are too many to be just listed here. For the most part, you can get a brief idea of what a module is used for by just looking.

For information on this you can refer to http://doc.qt.io/qt-5/qtmodules.html.

Value-Add Modules

These modules offer additional capabilities and are offered with a commercial license from Qt. Yes, you guessed it right, these are the modules that are only available in a paid version of Qt and are not offered in the open source and free version of the Qt, but they're mostly aimed to help with very specific tasks that we don't need at all for the purpose of this book. You can get a list using the Qt documentation pages.

For information on this you can refer to http://doc.qt.io/qt-5/qtmodules.html.

Technology Preview Modules

Exactly as their name implies, these modules are the ones that are offered in a state that is not guaranteed to work for all cases; they may or may not contain bugs or other issues, and they are simply still in development and are offered as a preview for test and feedback purposes. As soon as a module is developed and becomes mature enough, it becomes available in other categories mentioned before, and it's taken out of the technology preview category. An example of these types of modules, at the moment of writing this book, is Qt Speech, which is a module that is aimed to add support for text-to-speech in Qt applications. It's always a good idea to keep an eye on these modules if you are looking forward to becoming a fully-fledged Qt developer.

For more information on this, you can refer to http://doc.qt.io/qt-5/qtmodules.html.

Platforms supported by Qt

When we are talking about developing applications, the platform can have many different meanings, including the OS type, OS version, Compiler type, Compiler version, and Architecture of the processor (32-bit, 64-bit, Arm, and more). Qt supports many (if not all) of the well-known platforms and is quick enough to catch up with new platforms when they are released. The following is the list of platforms supported by Qt at the moment of writing this book (Qt 5.9). Note that you will probably not use all of the platforms mentioned here, but it gives you a sense of how powerful and cross-platform Qt really is:

Platform

Compiler

Notes

Windows

Windows 10 (64-bit)

MSVC 2017, MSVC 2015, MSVC 2013, MinGW 5.3

Windows 10 (32-bit)

MSVC 2017, MSVC 2015, MSVC 2013, MinGW 5.3

Windows 8.1 (64-bit)

MSVC 2017, MSVC 2015, MSVC 2013, MinGW 5.3

Windows 8.1 (32-bit)

MSVC 2017, MSVC 2015, MSVC 2013, MinGW 5.3

Windows 7 (64-bit)

MSVC 2017, MSVC 2015, MSVC 2013, MinGW 5.3

Windows 7 (32-bit)

MSVC 2017, MSVC 2015, MSVC 2013, MinGW 5.3

MinGW-builds gcc 5.3.0 (32-bit)

Linux/X11

openSUSE 42.1 (64-bit)

GCC 4.8.5

Red Hat Enterprise Linux 6.6 (64-bit)

GCC 4.9.1

devtoolset-3

Red Hat Enterprise Linux 7.2 (64-bit)

GCC 5.3.1

devtoolset-4

Ubuntu 16.04 (64-bit)

GCC as provided by Canonical

(Linux 32/64-bit)

GCC 4.8, GCC 4.9, GCC 5.3

macOS

macOS 10.10, 10.11, 10.12

Clang as provided by Apple

Embedded Platforms: Embedded Linux, QNX, INTEGRITY

Embedded Linux

GCC

ARM Cortex-A, Intel boards with GCC-based toolchains

QNX 6.6.0, 7.0 (armv7le and x86)

GCC as provided by QNX

Hosts: RHEL 6.6 (64-bit), RHEL 7.2 (64-bit), Windows 10 (64-bit), Windows 7 (32-bit)

INTEGRITY 11.4.x

As provided by Green Hills INTEGRITY

Hosts: 64-bit Linux

Mobile Platforms: Android, iOS, Universal Windows Platform (UWP)

Universal Windows Platform (UWP) (x86, x86_64, armv7)

MSVC 2017, MSVC 2015

Hosts: Windows 10

iOS 8, 9, 10 (armv7, arm64)

Clang as provided by Apple

macOS 10.10 host

Android (API Level: 16)

GCC as provided by Google, MinGW 5.3

Hosts: RHEL 7.2 (64-bit), macOS 10.12, Windows 7 (64-bit)

 

Reference: http://doc.qt.io/qt-5/supported-platforms.html

As you'll see in the next sections, we'll use the Microsoft Visual C++ 2015 (or from here on, simply MSVC 2015) compiler on Windows since both Qt and OpenCV (which you'll learn about later) highly support it. We'll also use GCC on Linux and Clang on macOS operating systems. All of which are tools that are either free and open source, or they are provided by the operating system provider. Although our main development system will be Windows, we'll cover Linux and macOS operating systems whenever there is a difference between Windows and other versions. So, the default screenshots throughout the book will be that of Windows, with Linux and macOS screenshots provided wherever there's any serious difference between them and not just simply a slight difference between paths, coloring of buttons, and so on.

Qt Creator

Creator is the name of the IDE (Integrated Development Environment) used to develop applications. It's also the IDE that we will use throughout this book to create and build our projects. It is worth noting that Qt applications can be created using any other IDE (such as Visual Studio or Xcode), and Qt Creator is not a requirement to build Qt applications, but it's a lightweight and powerful IDE that comes with the Qt Framework installer by default. So, the biggest advantage it has is easy integration with the Qt framework.

The following is a screenshot of the Qt Creator, which shows the IDE in code editing mode. Details on how to use the Qt Creator will be covered in the next chapter, although we'll give it a try for a couple of tests later on in this chapter too, without going into too much detail about it: