Home Data Computer Vision with OpenCV 3 and Qt5

Computer Vision with OpenCV 3 and Qt5

By Amin Ahmadi Tazehkandi
books-svg-icon Book
eBook $43.99 $29.99
Print $54.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $43.99 $29.99
Print $54.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Introduction to OpenCV and Qt
About 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.
Publication date:
January 2018
Publisher
Packt
Pages
486
ISBN
9781788472395

 

Chapter 1. Introduction to OpenCV and Qt

In its most basic form and shape, Computer Vision is the term that is used to identify all methods and algorithms that are used to empower digital devices with a sense of vision. What does that mean? Well, it means exactly what it sounds like. Ideally, computers should be able to see the world through the lens of a standard camera (or any other type of camera for that matter), and by applying various Computer Vision algorithms, they should be able to detect faces, even recognize them, count objects in an image, detect motion in video feeds, and do many more, which, at first guess, would only be expected of a human being. So, to understand what Computer Vision really is, it's better to know that Computer Vision aims to develop methods to achieve the ideal that was mentioned, empowering digital devices with the power of seeing and understanding the surrounding environment. It is worth noting that most of the time Computer Vision and Image Processing are used interchangeably (although, a historical study of the subject may prove that it should be otherwise). But nevertheless, throughout this book we'll stick to the term Computer Vision since that is the more popular and widely used term nowadays in computer science communities and also because, as we'll see later in this chapter, Image Processing is a module of OpenCV library that we'll also be introduced to in the upcoming pages of this chapter, and it will also be covered in a complete chapter of its own.

Computer Vision is one of the today's most popular subjects in computer science and it's used in a variety of applications ranging from medical tools that detect cancerous tissues to video editing software that helps make all those shiny music videos and movies, and from military grade target detectors that help locate a specific location on the map to traffic sign detectors that help driverless cars find their way around. Well, it's obvious that we can't finish naming all possibilities for Computer Vision, but we can be sure it's an interesting subject that will be around for a long time. It's also worth mentioning that there is a fast-expanding market for jobs and careers in the field of Computer Vision and it's growing day by day.

Among the most popular tools used by Computer Vision developers and experts, come two of the most prominent frameworks of open source community, which are also in the title of the book you have in hand, OpenCV and Qt. Every day, literally thousands of developers around the world, ranging from established corporations to innovative startup companies, work with these two frameworks to build applications for various sets of industries, such as the ones we mentioned, and that is exactly what you will learn in this book.

In this chapter, we will cover the following topics:

  • Introducing Qt, an open source, and cross-platform application development framework
  • Introducing OpenCV, an open source, and cross-platform computer vision framework
  • How to install Qt on Windows, macOS, and Linux operating systems
  • How to build OpenCV from sources on Windows, macOS, and Linux operating systems
  • Configuring your development environment to build applications using a combination of both Qt and OpenCV frameworks
  • Build your very first application using Qt and OpenCV
 

What is required?


This is the most obvious question after what was said in the introduction of this chapter, but the answer for it is also the first step in our journey to learn Computer Vision. This book is intended for developers who are familiar with C++ programming language and want to develop powerful and good-looking computer vision applications that perform well on different operating systems without much effort. This book aims to take you on an exciting journey through different topics of Computer Vision with a focus on hands-on exercises and developing whatever you learn, one step at a time.

Anyone with enough C++ experience knows that it's not an easy task to write visually rich applications using raw C++ code and depending on OS-specific APIs. So, almost every C++ developer (or at least serious developers with an active career in C++), use one or another framework to ease the process. Among the most widely known frameworks for C++ comes Qt. It is, in fact, one of the top choices, if not the top choice for that matter. On the other hand, if your aim is to develop an application dealing with images or visualized datasets, the OpenCV Framework is perhaps the first (and perhaps the most popular) address to visit. So, that's why this book focuses on the combination of Qt and OpenCV. Developing Computer Vision applications for different desktop and mobile platforms that perform with the highest possible performance would not be possible without using a combination of powerful frameworks such as Qt and OpenCV.

To summarize what was said, make sure you have at least an intermediate level of knowledge in C++ programming language. If terms such as class, abstract class, inheritance, templates, or pointers sound unfamiliar to you, then consider reading a book on C++ first. For all the rest of the topics, especially all of the hands-on topics that are covered, this book promises you crystal clear explanations (or reference to specific documentation pages) for all of the examples and tutorials included. Of course, to get a very detailed and deep understanding of how modules and classes are implemented in Qt and OpenCV, you need to be familiar with many more resources, studies, sometimes even hard-core mathematical calculations or low-level understanding of how a computer or operating system performs in the real world, which is totally out of the scope of this book. However, for all the algorithms and methods covered in this book, you will get a brief description of what they are, how and when and where they are used, and enough guidelines to let you continue digging deeper if you feel like it.

 

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:

 

Introduction to OpenCV


Now, it's time to introduce OpenCV, or Open Source Computer Vision library, or framework if you will, since itself uses them interchangeably and that may also happen throughout this book. However, for the most part we'll simply stick to OpenCV. Well, let's first hear what it really is and then break it down where needed.

OpenCV is an open source and cross-platform library that is used to develop computer vision applications. With a focus on speed and performance, it includes hundreds of algorithms within a variety of modules. These modules are also categorized into two types: the Main and Extra modules. Main OpenCV modules are simply all modules that are built and maintained within OpenCV community, and they are a part of the default package provided by OpenCV.

This is in contrast to the Extra modules of OpenCV, which are more or less wrappers for third-party libraries and interfaces required to integrate them into an OpenCV build. The following are some examples of different module types with a brief description for each. It is worth noting that the number of (and sometimes even the order of) modules within OpenCV can be changed over time, so the best thing to keep in mind about this is to just pay a visit to the OpenCV documentation pages whenever something seems to be out of place, or if something is not where it used to be.

Main modules

Here are some examples of OpenCV main modules. Note that they're just a few (and probably the most widely used) of modules within OpenCV, and covering all of them is out of the scope of this book, but it makes sense to have an idea of what OpenCV contains, just like what we saw Qt earlier in this chapter. Here they are:

  • Core functionality or simply core module contains all basic structures, constants, and functions used by all other OpenCV modules. For instance, the infamous OpenCV Mat class, which we'll use almost in every OpenCV example for the rest of the book, is defined in this module. Chapter 4, Mat and QImage, will cover this and closely-related OpenCV modules along with corresponding parts of the Qt framework.
  • Image processing or imgproc module contains many different algorithms for image filtering, image transformation, and as the name implies, it's used for general image processing use. We'll be introduced to this module and its functions in Chapter 6, Image Processing in OpenCV.
  • The 2D Features Framework module or features2d includes classes and methods used for feature extraction and matching. They'll be covered in more detail in Chapter 7, Features and Descriptors.
  • The video module contains algorithms that are used for topics such as motion estimation, background subtraction, and tracking. This module, along with other similar modules of OpenCV, will be covered in Chapter 9, Video Analysis.

Extra modules

As it was mentioned before, Extra modules are mostly wrappers for third-party libraries, that means they only contain interfaces or methods needed to integrate those modules. An example Extra module would be the text module. This module contains interfaces to use Text in images or OCR (Optical Character Recognition), and you'll also need those third-party modules for this work, and they are not covered as a part of this book, but you can always check the OpenCV documentation for an list of Extra modules how they are used.

For more on this you can refer to http://docs.opencv.org/master/index.html.

Note

Platforms Supported by OpenCV: As it was mentioned before, Platform is not just the operating system in case of application development. So, we need to know which operating systems, processor architectures, and the compiler is supported by OpenCV. OpenCV is highly cross-platform, and, almost like Qt, you can develop OpenCV applications for all major operating systems, including Windows, Linux, macOS, Android, and iOS. As we'll see later on, we'll use the MSVC 2015 (32-bit) compiler on Windows, GCC on Linux, and Clang on macOS. It's also important to note that we'll need to build OpenCV using its source code by ourselves since at the moment, prebuilt binaries are not provided for the mentioned compilers. However, as you'll see later on, OpenCV is fairly easy to build for any operating system if you have the right tools and instructions.

 

Installing Qt


In this section, we'll go through the required steps to set up the complete Qt SDK (Software Kit) on your computer. We'll start by setting up Qt on Windows OS make a note for Linux (Ubuntu in our case, but it's almost the same for all Linux distributions), and macOS operating system, wherever required. So, let's start.

Preparing for Qt installation

To be able to install and use Qt, we to first create a Qt account. Although it is not mandatory to do this, it still is highly recommended since you can get access to everything Qt related to this single, unified and free account. For any recent version of Qt that you want to install, you will need your Qt Account credentials, which you will only have if you have created a Qt account. To do this, first, you to go to the Qt website using your favorite browser. Here is the link:

https://login.qt.io/login

 Here is the screenshot of it:

Here, you have to use your email address using the Create Qt Account page just under the Sign in button. The process is almost identical to any similar account creation on the web. You might be asked to enter captcha images to prove you are not a robot or click on an activation link in your email. After going through the procedures required by Qt, you'll have your very own Qt Account user, which is your email, and password. Make a note of that since you'll be needing it later on. We'll refer to it as your Qt Account Credentials from here on.

Where to get it?

This is the point where we start downloading the required tools for Qt development. However, where to start? Qt maintains all officially released versions through the Qt Downloads web page. Here's a link: https://download.qt.io/official_releases/.

If you open your browser and navigate to the preceding webpage, you'll be presented with a very simple web page (similar to your file explorer program), and from there, you to choose the right file yourself:

Qt releases all its official tools in here and, as you'll see, the Last modified column will be changing all the time. Some entries not so often, some more often. For now, we will not go into details of what each of these folders contains and what they are used for, but as you'll see later in this book, almost all of the tools required by us are in a single installer file and it's under the qt folder. So, by clicking on each entry, navigate to the following folder: qt/5.9/5.9.1/

You'll notice the same is added to the web address in your browser: https://download.qt.io/official_releases/qt/5.9/5.9.1/

You should note that there might be a newer version at the time when you visit this page, or this version may simply not be available anymore, so you need to start from the Qt Download page mentioned before, and work your way into the latest Qt version folder. Or, you can use the archive link in the  Downloads main page (https://download.qt.io/archive/) to always access previous versions of Qt.

Note

Here are the files you need to download from the preceding folder:                      For Windows:qt-opensource-windows-x86-5.9.1.exe                              For macOS:qt-opensource-mac-x64-5.9.1.dmg                                          For Linux:qt-opensource-linux-x64-5.9.1.run  

These are pre-built Qt libraries and contain the complete Qt SDK for each of the mentioned operating systems. This means you don't need to build Qt libraries by yourself to be able to use them. Here's what these installation files include in general and the tools we'll use:

  • Qt Creator (version 4.3.1)
  • Pre-Built libraries for all compilers and architecture supported on each OS:
    • Windows Desktop, Windows Mobile (on Windows)
    • Desktop (on Linux)
    • Desktop and iOS (on macOS)
    • Android (on all platforms)

Note

Windows users: The Qt installation package also includes the MinGW compiler included in it, but since we will use another compiler, namely MSVC 2015, you don't really have anything to do with it. Although installing it shouldn't cause any harm.

How to install?

You need to start the installation by executing the files you downloaded. If you are on a Windows or macOS operating system, then you just need to run the downloaded file. However, if you are using Linux, then you may need to make your downloaded .run file executable first before being able to actually run it. The following command can be executed on Linux to make your installer file executable:

chmod +x qt-opensource-linux-x64-5.9.1.run

Or, you can simply right-click on the .run file and make it executable using the properties dialog:

Note that you still need a working internet connection even though nothing will be downloaded and it's just to confirm your Qt Account Credentials. Running the installer will present you with the following series of dialogs that you need to get through. Wherever instructions on the dialog boxes are enough, just make sure you read them and provide what is required and press Next, Agree, or similar buttons to proceed forward. As you see in the following screenshots, you need to provide your Qt Account Credentials in order to proceed with the installation. These dialogs are identical on all operating systems:

The rest of the dialog boxes are not shown here, but they are pretty much self-explanatory, and if you have ever installed any app on any computer, then you have definitely seen similar dialogs and they need no introduction in general.

Windows users

When installing Qt for Windows, on the Select  dialog, make sure you check the checkbox next to the msvc2015 32-bit option. The rest is optional, but it's worth noting that installing all platforms (or Kits as they are called in Qt) usually requires too much space and can affect the Qt Creator performance in some cases. So, just make sure to select anything that you will really use. For the purpose of this book, it's just the msvc2015 32-bit option that you absolutely require.

Note

An important thing to note for Windows users: You need to also install Visual Studio 2015 with at least C++ desktop development features enabled in it. Microsoft offers different types of licenses for Visual Studio. You can download the Community edition for educational purposes, which is definitely enough for this book's examples, and it's provided free of charge, but using Enterprise, Professional, or other types of Visual Studio should also be fine as long as they have the MSVC 2015 32-bit compiler.

macOS users

When installing Qt for macOS, you will be with the following dialog (or a quite similar one depending on the version of macOS you are using) box if you don't have XCode installed on your Mac:

Unfortunately, it's not enough to follow the Install button, which takes a lot less time than installing Xcode, even though it might look like the obvious choice. You still need to make sure you get Xcode installed on your Mac either by pressing the Get Xcode button, directly getting it from App Store, or you'll face the following while installing Qt:

Use App Store to install the latest version of Xcode (at the moment of writing this book, Xcode 8.3.3 is available) then continue with Qt installation.

On the Select Components dialog, make sure you select at least macOS version. You won't need the rest of the components but installing them won't harm, other than the fact that it might take a lot of space on your computer.

Linux users

When installing Qt for Linux, on the Select  dialog, make sure you select (at least) Desktop GCC (32 bit or 64 bit, depending on your OS). You'll notice that the Qt Creator will be installed by default, and you don't need to check any options for that.

When the installation is completed, you'll have the following applications installed on your computer:

  • Qt Creator: This is the main that we'll be using throughout the book to build applications.
  • Qt Assistant: This application is to view Qt help files. It provides useful functionality to view Qt documentation. Nevertheless, the Qt Creator also provides a context-sensitive help, and it also has its own built-in and very handy help viewer.
  • Qt Designer: This is to design GUIs using Qt Widgets. Again, Qt Creator also has this designer built-in, but in case you prefer using other IDEs rather than the Qt Creator, then you can still use the Designer to help with the GUI design process.
  • Qt Linguist: This is an aid if you will be building multilingual applications. Qt Linguist helps with easier translation and integration of translated files into your build.

For Windows and macOS users, this is the end of the story for Qt installation, but Linux users still need to take care of a few more things, namely installing the application development, building tools, and some required runtime libraries for Linux. Qt always uses the compiler and build tools provided by the operating system. Linux distributions usually do not, by default, include those tools since they're only used by developers and not used by regular users. So, to install them (if they're not installed already) you can run the following command from a terminal:

sudo apt-get install build-essential libgl1-mesa-dev

You can always refer to the Qt documentation pages for required commands by all Linux distributions, but, in this book, we'll assume the to be Ubuntu/Debian; however, note that usually, the commands are very similar in the pattern for all Linux distros.

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

Testing Qt installation

You can now safely run Qt Creator and wonderful applications with it. For now, let's just make sure our Qt installation is working correctly. Don't bother with the details now, since we'll be covering it all during the course of the book, and especially don't worry if you think you don't understand what's really going on behind the scenes. Just run Qt Creator and press the big New Project button seen, as follows:

In the window that appears next, choose Application, Qt Widgets Application, and then click on Choose, as shown in the following screenshot:

In the next window, you need to provide a name and folder (where your test project will be created) and then click Next to proceed forward. Make sure to check the Use as default project location checkbox if you want to have a dedicated folder for your Qt projects. You only need to do this once and afterward all of your projects will be created in that folder. For now, let's just put a name and path since we're only going to test our Qt installation, and click Next. You will see something similar to what is shown in the following screenshot:

In the next window, you need to select a so-called Kit to build your application with. Choose the one that has a name starting with Desktop Qt 5.9.1 and click Next. Depending on what components you selected during the installation of Qt, you may have more than one choice here, and depending on the operating system and compilers installed on your system, you may have more than one Kit with a name that starts with Desktop, so make sure you select the compilers we'll use in this book, which would be the following:

  • msvc2015 32-bit on Windows
  • Clang on macOS
  • GCC on Linux

After you have selected the correct Kit according to the ones mentioned earlier, you can click on Next to proceed forward:

You don't really need to bother with the next two windows that appear and just clicking on Next should be enough for our test of Qt installation. The first window makes it easier to a new class and the second one allows you to select a version control tool and track changes in your code:

After you click on the Finish button on the last window, you will be taken to the Edit mode within Qt Creator. We'll cover different aspects of the Qt Creator in the next chapter, so for now, just click on the Run button (or press Ctrl +60;R) to start compiling your test (and empty) application, as shown here:

Depending on the speed of your computer, it will take some time for the build process to be finished. After a few moments, you should see your test (and first) Qt application running. It's just an empty application similar to what is seen in the following screenshot, and the purpose of doing this was to make sure our Qt installation is working as we want it to. Obviously, your empty Qt application may look a little different than this on different operating systems, and different visual options may affect the whole coloring or the way windows are displayed. Nevertheless, your newly built application should look exactly the same (or quite similar) to the window seen here:

In case your application doesn't show up, make sure to go over the instructions once again. Also, make sure you don't have any conflicting installations of Qt or other settings that may interfere with Qt installation. Always refer to documentation pages and the Qt community for answers to unexpected behavior of Qt Creator or other Qt tools. Being an open source project for such a long time, Qt has grown a huge and loyal set of users who are eager to share their knowledge on the internet and answer issues faced by fellow Qt users. So, it's a good idea to keep an eye on the Qt community since you already have a unified Qt that you can use to access Qt forums. This is the same user and password that you created to proceed with the Qt installation process.

 

Installing OpenCV


In this section of the chapter, you'll learn how to build OpenCV using its source codes. As you'll see later on, and as opposed to the title of this section, we're not really installing OpenCV in a way similar to what we experienced with Qt installation. That's because OpenCV usually doesn't pre-built binaries for all compilers and platforms, and in fact it provides no pre-built binaries for macOS and Linux at all. In the most recent Win pack of the OpenCV, only pre-built binaries for MSVC 2015 64-bit are included, which are not compatible with the 32-bit version that we'll be using, so it's a very good idea to learn how to build OpenCV yourself. It also has the advantage of building an OpenCV framework library that is suitable for what you need. You may want to exclude some options to make your OpenCV installation lighter, or you may want to build for another compiler such as MSVC 2013. So, there are quite a lot of reasons to build OpenCV from sources by yourself.

Preparing for an OpenCV build

Most of the open source frameworks and libraries on the internet, or at least the that want to remain IDE neutral (this means, a project that can be configured and built using any IDE and a project that does not depend on a specific IDE to be able to work), use CMake or similar so-called make systems. I guess this also answers questions such as Why do I need CMake at all?, Why can't they just give the libraries and be done with it? or any other questions like these. So, we need CMake to be able to configure and build OpenCV using sources. CMake is an open source and cross-platform application that allows for configuring and building open source projects (or apps, libraries, or so on), and you can download and use it on all operating systems that were mentioned in the previous sections. At the moment of writing this book, CMake version 3.9.1 can be downloaded from the website download page (https://cmake.org/download/).

Make sure you download and install it on your computer before proceeding forward. There's nothing special about the CMake installation that needs to be taken note of, except for the fact that you should make sure you install the GUI version since that's what we'll be using in the next section and it's the default option in the link provided earlier.

Where to get OpenCV?

maintains its official and stable releases under the  page at their website (http://opencv.org/releases.html):

This is where you can always find the most recent release of OpenCV sources, documentation, and pre-built binaries for Windows, Android, and iOS. As new versions are released, they are added at the top of the page. At the moment of writing this book, version 3.3.0 is the most recent release of OpenCV and that's the version we'll use. So, without further ado, you should go ahead and download the Sources by clicking on the Sources link for version 3.3.0. Download the source zip file to a folder of your choice, extract it, and make a note of the extracted path since we'll use it in a few moments.

How to build?

Now that we have all required tools and files to build OpenCV, we can start the by running the CMake GUI application. If you've installed CMake correctly, then you should be able to run it from your desktop, start menu, or dock, depending on your operating system.

Note

Linux users should run the following command in a terminal before proceeding with the OpenCV build. These are basically dependencies by OpenCV itself, which need to be in place before it's configured and built:

sudp apt-get install libgtk2.0-dev and pkg-config  

After you run the CMake GUI application, you need to set the following two folders:

  • The Where is the source code folder should be set to where you downloaded and extracted OpenCV source code
  • The Where to build the binaries folder can be set to any folder, but it's common to create a subfolder named build under the source codes folder and select that as the binaries folder

After these two folders are set, you can proceed forward by clicking on the Configure button, as shown in the following screenshot:

Clicking on the Configure button will start the configuration process. You may be asked to create the build folder if it does not already exist, to which you need to answer by clicking on the Yes button. Don't worry if you still feel like you're just repeating what's in the book. This will all sink in as you move forward with the book and the instructions. For now, let's just focus on getting OpenCV built and installed on your computer. Consider this an installation that is not as easy as just clicking on a few Next buttons, and as soon as you start using OpenCV, it will all make sense. So, in the window that will appear next, select the correct generator and click Finish. See the following instructions for the correct generator type on each operating system:

Note

Windows users: You need to select Visual Studio 14 2015. Make sure you don't select the ARM or Win64 versions or a different Visual Studio version.                   macOS and Linux users: You need to select Unix Makefiles.

You'll see a short process in CMake, and when it's finished, you'll be able to set various parameters to configure your OpenCV build. There are many parameters to configure, so we'll just bother with the ones that directly affect us.

Make sure you check the checkbox next to the BUILD_opencv_world option. This will allow building all OpenCV modules into a single library. So, if you're on Windows, you'll have only a single DLL file that includes all OpenCV functions. As you'll see later on, this has the advantage of bothering with only a single DLL file when you want to deploy your computer vision applications. Of course, the obvious downside to this is that your application installer size will be a little bit larger. But again, ease of deployment,  will prove to be much more useful later on.

You need to click on the Configure button again after changing the build parameters. Wait for the reconfiguration to finish and finally click on the Generate button. This will make your OpenCV build ready to compile. For the next part, you'll need to execute somewhat different commands if you're using Windows, macOS, or Linux operating systems. So, here they are:

Windows users: Go to the OpenCV build folder that you set earlier in CMake (in our case it was c:\dev\opencv\build). There should be a Visual Studio 2015 Solution (that is, the type of MSVC projects) that you can easily execute and build OpenCV with. You can also immediately click on the Open Project button, which is right next to the Generate button on CMake. You can also just run Visual Studio 2015 and open the Solution file you just created for OpenCV.

After Visual Studio is opened, you need to select Batch Build from the Visual Studio main menu. It's right under Build:

 

Make sure checkboxes in the Build column are enabled for ALL_BUILD and INSTALL, as shown in the following screenshot:

For macOS and Linux users: Run a terminal instance and execute the following commands after you switch to the Binaries folder you chose in CMake. To switch to a specific folder, you need to use the cd command. After you are in the OpenCV build folder (it should be the folder in your home that you chose when you opened CMake), you need to execute the following command. You'll be asked to provide the administrative password, just provide your password and press Enter to proceed forward with building OpenCV:

    sudo make

This will trigger the build process, and depending on your speed this can take quite some time. Wait until building all libraries are finished and you see the progress bar reach 100%.

After the long wait, there's only one more command left to execute for macOS and Linux users. If you are on Windows, you can close Visual Studio IDE and proceed to the next step.

Note

macOS and Linux users: After the building is completed, before closing the terminal instance, execute the following command while you're still in the OpenCV build folder:sudo make install

This last command for non-Windows users will make sure OpenCV is installed on your computer and is completely ready to use. You're fine to proceed forward if you didn't miss any commands in this section. You have an OpenCV framework ready to build computer vision applications with.

Configuring OpenCV installation

Remember we mentioned that is a framework and you will learn how to use it with Qt? Well, Qt offers a very easy-to-use method to include any third-party library, such as OpenCV, in your Qt projects. To be able to use OpenCV in Qt, you need to use a special kind of file which is called a PRI file. PRI files are files that are used to add third-party modules and include them in your Qt projects. Note that you only need to do this once, and for the rest of the book you will use this file in all your projects, so it's a very crucial (yet very easy) part of the Qt configuration.

Start by creating a text file in a folder of your choice. I recommend using the same folder you used for OpenCV build because that can help make sure you have all your OpenCV related files in a single folder. However, technically speaking, this file can be located anywhere on your computer. Rename the file to opencv.pri and open it using any text editor and write the following inside this PRI file:

Note

Windows users: By now, your OpenCV library files should be inside the OpenCV build folder that you set on CMake previously. There should be a subfolder called install inside the build folder with all required OpenCV files inside it. In fact, now you can delete everything else and just keep these files if you need some space on your computer, but it's always a good idea to keep OpenCV sources on your computer, and we'll specifically need it in the final chapters where we'll cover more advanced OpenCV subjects. So, here's what you need inside the PRI file (be careful about path separators, you always need to use / in PRI files regardless of the operating system):

INCLUDEPATH += c:/dev/opencv/build/install/include 
Debug: { 
LIBS += -lc:/dev/opencv/build/install/x86/vc14/lib/opencv_world330d 
} 
Release: { 
LIBS += -lc:/dev/opencv/build/install/x86/vc14/lib/opencv_world330 
} 

Note

There is no need to say that in the preceding code, you need to replace the paths if you have used a different folder during the CMake configuration. macOS and Linux users: Simply put the following inside your opencv.pri file:

 INCLUDEPATH += /usr/local/include 
 LIBS += -L/usr/local/lib \ 
    -lopencv_world 

There's one more thing left for Windows users, and that is adding OpenCV DLLs folder to the PATH environment variable. Simply open the System properties window and add a new entry in your PATH. They are usually separated by ; so just add a new one after that. Note that this path is related to the Windows operating system only, and where it can find your DLL files for OpenCV for an easier build process; users of Linux and macOS don't need to do anything regarding this.

Testing OpenCV installation

The worst is past us, and we are now ready to dig into the world of Computer Vision and start building exciting applications using Qt and OpenCV. Although this one last step is called Testing OpenCV, it's actually the first Qt+OpenCV application that you'll write, as simple as it may seem at first. Our aim in this section is not to bother with any details of how things are working and what goes on behind the scenes, but just to make sure we have configured everything correctly and avoid wasting time with configuration-related issues later in the book. If you have followed everything as it was described and executed all instructions in the correct order, then by now, you shouldn't be worrying about anything, but it's always best to verify, and that is what we'll do now.

So, we'll verify our installation with a very simple application that reads an image file from the hard disk and just displays it. Again, don't bother with any code related details since we'll cover it all in the upcoming chapters and just focus on the task at hand, that is testing our OpenCV installation. Start by running Qt Creator and creating a new Console Application. You have already done a very similar task before when you were testing your Qt installation. You need to follow exactly the same instructions, except instead of Qt Widgets, you have to make sure you select Qt Console Application. Repeat all of the similar steps like before, until you end up in Qt Creator edit mode. If you are asked about build system just select qmake, which should be selected by default, so you just need to move forward. Make sure to give a name such as QtCvTest to your project. This time, instead of clicking on the Run button, double-click on your project's PRO file, which you can find in the explorer at the left side of the Qt Creator screen, and add the following line at the end of your project's PRO file:

    include(c:/dev/opencv/opencv.pri) 

Note that this, in fact, is a type of hard-coding that should always be avoided, and as we'll see in the later chapters, we will write more sophisticated PRO files that work on all operating systems without changing a single line; however, since we're just testing our OpenCV installation, it is OK for now to go ahead with a little bit of hard-coding to simplify things a little bit and not overwhelm you with more configuration details.

So, back to what we were doing, the moment you save your PRO file by pressing Ctrl S, you will notice a quick process and update in the project explorer and the opencv.pri file will appear in the explorer. You can always change the contents of opencv.pri from here, but you will probably never need to do it. Ignore the Comment-Like lines and make sure your PRO file is similar to what I have here:

 QT += core 
 QT -= gui 
 CONFIG += c++11 
 TARGET = QtCvTest 
 CONFIG += console 
 CONFIG -= app_bundle 
 TEMPLATE = app 
 SOURCES += main.cpp 
 DEFINES += QT_DEPRECATED_WARNINGS 
 include(c:/dev/opencv/opencv.pri) 

This simple line of code in our project's PRO file is basically the result of all of our efforts in this chapter. Now, we are able to add OpenCV to our Qt projects by simply including this simple piece of code in every Computer Vision project that we want to build using Qt and OpenCV.

In the upcoming chapters, we'll learn about PRO files in Qt and everything about the preceding code; however, for now let's just move on knowing that this file is responsible for our project's configurations. So, the last line, which is pretty much self-explanatory, simply means we want to add include headers and libraries to our Qt project.

Now, you can actually write some OpenCV code. Open up your main.cpp file and change the contents of it to make it similar to this:

 #include <QCoreApplication> 
 #include "opencv2/opencv.hpp" 
 int main(int argc, char *argv[]) 
 { 
    QCoreApplication a(argc, argv); 
    using namespace cv; 
    Mat image = imread("c:/dev/test.jpg"); 
    imshow("Output", image); 
    return a.exec(); 
 } 

By default, your main.cpp file should already have most of what is in the preceding code, but you'll notice the include line at the top and the three lines responsible for reading and displaying a test image from my computer. You can replace the path to any other image (just make sure you stick to JPG or PNG files for now), and it's very important to make sure the image file exists and it's accessible, otherwise, our test might fail, even though our installation is still correct. The whole code is almost self-explanatory but once again, you shouldn't bother with the codes for now since we're just testing our build, so just press the Run button to have your image file displayed. You should see something similar to the following screenshot on your computer:

 

Summary


In this chapter, you were introduced to the concept of Computer Vision in general and to the Qt and OpenCV frameworks, and you learned about their overall modular structure and also got a brief look into how seriously cross-platform they are with all the platforms they both support. You also learned how to install Qt on a computer and how to build OpenCV using its source codes. By now, you should have enough confidence to even try out a few different configurations to build OpenCV, other than the standard build mentioned in this chapter. It's always a good idea to explore some unknown and deep parts of these huge frameworks by simply taking a look in the folders and files they contain. Finally, you learned how to configure your development computer to build applications using Qt and OpenCV, and you even built your first application. In the next chapter, you'll learn more about Qt Creator by first building a console application and then moving on to building a Qt widgets application. You'll also learn about the structure of Qt projects and how to create a cross-platform integration between Qt and OpenCV frameworks. The next chapter will be the start of actual computer vision development and programming examples in this book and will lay out the foundations of our hands-on examples throughout the book.

About the Author
  • Amin Ahmadi Tazehkandi

    Amin Ahmadi Tazehkandi is an Iranian author, developer, and a computer vision expert.Amin Ahmadi Tazehkandi is an Iranian author, developer, and a computer vision expert. He completed his computer software engineering studies in Iran and has worked for numerous software and industrial companies around the world.

    Browse publications by this author
Latest Reviews (12 reviews total)
Its a good book for learners.
A very good book covering the union of Graphical User Interfaces developed using Qt5 C++ framework and the OpenCV 3 for computer vision.
Tazehkandi "Computer Vision with OpenCV 3 and Qt5" is a welcome addition in that it covers the difficult subject of merging Qt5 with OpenCV (different build systems). Valuable for that alone, but this is a well-written book.
Computer Vision with OpenCV 3 and Qt5
Unlock this book and the full library FREE for 7 days
Start now