Computer Vision with OpenCV 3 and Qt5 - Amin Ahmadi Tazehkandi - E-Book

Computer Vision with OpenCV 3 and Qt5 E-Book

Amin Ahmadi Tazehkandi

0,0
40,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Blend the power of Qt with OpenCV to build cross-platform computer vision applications

Key Features

  • ● Start creating robust applications with the power of OpenCV and Qt combined
  • ● Learn from scratch how to develop cross-platform computer vision applications
  • ● Accentuate your OpenCV applications by developing them with Qt

Book Description

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.

What you will learn

  • ● Get an introduction to Qt IDE and SDK
  • ● Be introduced to OpenCV and see how to communicate between OpenCV and Qt
  • ● Understand how to create UI using Qt Widgets
  • ● Learn to develop cross-platform applications using OpenCV 3 and Qt 5
  • ● Explore the multithreaded application development features of Qt5
  • ● Improve OpenCV 3 application development using Qt5
  • ● Build, test, and deploy Qt and OpenCV apps, either dynamically or statically
  • ● See Computer Vision technologies such as filtering and transformation of images, detecting and matching objects, template matching, object tracking, video and motion analysis, and much more
  • ● Be introduced to QML and Qt Quick for iOS and Android application development

Who this book is for

This book is for readers interested in building computer vision applications. Intermediate knowledge of C++ programming is expected. Even though no knowledge of Qt5 and OpenCV 3 is assumed, if you’re familiar with these frameworks, you’ll benefit.

Amin Ahmadi Tazehkandi is an Iranian developer and a computer vision expert. He completed his computer software engineering studies in Iran and has worked for numerous software and industrial companies, including Paxan (Turkey) and Petroleum Software (UK). Amin is a passionate blogger and a longtime volunteer and supporter of the Computer Vision community. He currently resides in Vienna, Austria, where he continues to research, develop, and write about cross-platform computer vision software development.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 597

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Computer Vision with OpenCV 3 and Qt5

 

 

 

 

 

 

 

 

 

 

Build visually appealing, multithreaded, cross-platform computer vision applications

 

 

 

 

 

 

 

 

 

 

Amin Ahmadi Tazehkandi

 

 

 

 

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Computer Vision with OpenCV 3 and Qt5

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Aaron LazarAcquisition Editor: Parth KothariContent Development Editor: Lawrence VeigasTechnical Editor: Supriya ThabeCopy Editor: Zainab BootwalaProject Coordinator: Prajakta NaikProofreader: Safis EditingIndexers: Rekha NairGraphics: Jason MonteiroProduction Coordinator: Nilesh Mohite

First published: January 2018

Production reference: 1291217

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78847-239-5

www.packtpub.com

This book is dedicated to the open source communities and you, the bright minded developers, working hard to build a better world for all of us.
mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Foreword

Around 20 years ago, when I was graduating from university, development of large and complex applications that included a graphical user interface was a time-consuming and difficult task. The APIs and tools that existed at that time to create those applications were difficult to use and understand. Creating applications for multiple platforms required writing a large part of it several times.

It was at that time that I discovered Qt, a framework that fixed both of these problems. It came with an easy-to-use, intuitive API and worked across all major desktop operating systems. Suddenly, programming these applications went from being hard work to something I really enjoyed. I wasn't limited to one operating system anymore—I could have my application running on multiple operating systems with a simple recompile. Since then, many things have improved for application developers. Frameworks have put a lot more effort into having easy-to-use APIs. The operating system landscape has changed, and having APIs that are available cross-platform is more important than ever.

OpenCV has, over the last few years, evolved into the leading API for computer vision. It contains a large set of functionalities and algorithms that can be used for things such as face recognition, tracking camera or eye movements, track markers for augmented reality, and much more.

Qt has also, over the same period, turned into one of the leading cross-platform frameworks for application development. Its comprehensive feature set contains most of the functionality you will need to develop a complex graphical application.

Making Qt the best technology to create cross-platform applications has been my mission for the last 17 years. One of the goals has always been to make it easy to combine Qt with other technologies. This book gives you a great example on how this can be done.

Both Qt and OpenCV feature cross-platform C++ APIs, making it straightforward to use them together. By combining them, you will have a powerful set of tools at hand, making it easy to create applications that combine computer vision with a graphical user interface. I hope that this book will help you on your way to becoming an expert in both Qt and OpenCV.

Lars KnollQt Chief Maintainer and CTO at The Qt Company

Contributors

About the author

Amin Ahmadi Tazehkandi is an Iranian developer and a computer vision expert. He completed his computer software engineering studies in Iran and has worked for numerous software and industrial companies, including Paxan (Turkey) and Petroleum Software (UK). Amin is a passionate blogger and a longtime volunteer and supporter of the Computer Vision community. He currently resides in Vienna, Austria, where he continues to research, develop, and write about cross-platform computer vision software development.

I would like to thank my wife, Senem, who is a symbol of passion, love, and strength for me. I would also like to thank my family, a big pack of brilliant engineers born to an Iranian inventor father and a loving mother, for all of their unconditional love and support.

About the reviewers

Karl Phillip Buhr has a BSc in computer science (2006), an MSc in applied computing (2010), and is a Data Science and Machine Learning enthusiast. He developed many cross-platform and computer vision systems for the private sector and enjoys answering questions on Stack Overflow. He spent years lecturing classes and doing research projects for a computing engineering course in Santa Catarina, Brazil. Nowadays, he runs a software company focused on developing solutions for challenging industry problems.

Vinícius Godoy is a professor at PUCPR and a proud owner of a game development website, Ponto V!. He has a master's degree in computer vision and image processing (PUCPR), a specialization degree in game development (Universidade Positivo), and has graduation in Technology in Informatics—Networking (UFPR). He has been in the software development field for more than 20 years and is the author of the book OpenCV by Example by Packt Publishing. 

 

 

 

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Get in touch

Reviews

Introduction to OpenCV and Qt

What is required?

Introduction to Qt

Qt Essentials

Qt Add-Ons

Value-Add Modules

Technology Preview Modules

Platforms supported by Qt

Qt Creator

Introduction to OpenCV

Main modules

Extra modules

Installing Qt

Preparing for Qt installation

Where to get it?

How to install?

Windows users

macOS users

Linux users

Testing Qt installation

Installing OpenCV

Preparing for an OpenCV build

Where to get OpenCV?

How to build?

Configuring OpenCV installation

Testing OpenCV installation

Summary

Creating Our First Qt and OpenCV Project

What is Qt Creator?

A tour of the IDE

Qt Creator modes

The Welcome mode

Projects

Examples

Tutorials

Edit mode

Design mode

Designing user interfaces

Hello Qt and OpenCV

Writing the code for a Qt project

Help mode

The Qt Creator Options window

Summary

Creating a Comprehensive Qt+OpenCV Project

Behind the scenes

The qmake tool

Meta-Object Compiler (moc)

User Interface Compiler (uic)

Design patterns

Qt Resource System

Styling applications

Selector types

Sub-controls

Pseudo-states

Cascading

Multi-language support

Creating and using plugins

The interface

The plugin

The plugin loader and user

Creating the foundations

Summary

Mat and QImage

All about the Mat class

Constructors, properties, and methods

The Mat_<_Tp> class

Matx< _Tp, m, n >

The UMat class

InputArray, OutputArry, InputOutputArray

Reading images using OpenCV

Writing images using OpenCV

Reading and writing videos in OpenCV

The HighGUI module in OpenCV

Image and video handling in Qt

The QImage class

The QPixmap class

The QImageReader and QImageWriter classes

The QPainter class

Camera and video handling in Qt

Summary

The Graphics View Framework

The Scene-View-Item architecture

The Scene, QGraphicsScene

The Items, QGraphicsItem

The View, QGraphicsView

Summary

Image Processing in OpenCV

Image filtering

Filtering functions in OpenCV

Image transformation capabilities

Geometric transformations

Miscellaneous transformations

Colors and color spaces

Image thresholding

Discrete Fourier transform

Drawing in OpenCV

Template matching

Summary

Features and Descriptors

Base of all algorithms – the Algorithm class

The 2D Features Framework

Detecting features

KAZE and AKAZE

The BRISK class

FAST

GFTT (Good Features to Track)

ORB

Extracting and matching descriptors

How to choose an algorithm

Accuracy

Speed

Resource usage

Availability

Summary

Multithreading

Multithreading in Qt

Low-level multithreading using QThread

Subclassing QThread

Using the moveToThread function

Thread synchronization tools

Mutexes

Read-write locks

Semaphores

Wait conditions

High-level multithreading using QtConcurrent

Summary

Video Analysis

Understanding histograms

Understanding back-projection images

Histogram comparison

Histogram equalization

MeanShift and CamShift

Background/foreground detection

Summary

Debugging and Testing

Debugging with Qt Creator

Getting started with the debugging mode

Qt Test Framework

Creating a Unit Test

Data-driven testing

Benchmarking

GUI testing

testcase projects

Summary

Linking and Deployment

The build process, behind the scenes

Preprocessing

Compiling

Linking

Building OpenCV static libraries

Building Qt static libraries

Deploying Qt+OpenCV applications

Deploying using static linking

Deploying using dynamic linking

Qt Installer Framework

Summary

Qt Quick Applications

Introduction to QML

User interaction and scripting in QML

Using Qt Quick Designer

Structure of a Qt Quick Application

Integrating C++ and QML codes

Qt and OpenCV apps on Android and iOS

Summary

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

There has never been a better time to be a software developer than now. Just look around you and, most probably, you'll see at least a couple of different devices such as computers, smartphones, smart watches, or tablets running some applications on them that help you with various daily tasks or entertain you with music, movies, video games, and so on. Every year, hundreds of new devices are introduced into the market and new versions of operating systems are required to keep up with them in order to provide better interfaces for application developers to create software that makes better use of the underlying resources such as high-resolution displays, various sensors, and so on. As a consequence, software development frameworks have to adapt to and support the ever growing number of platforms. Considering this, Qt is probably one of the most successful cross-platform software development frameworks that offers power, speed, flexibility and ease of use, all at the same time, and it is a top choice when it comes to creating software that needs to look appealing and consistent across a wide range of platforms.

In recent years, and especially with the rise of more powerful processors at lower costs, the role of desktop computers and their handheld counterparts has shifted toward performing the more demanding and complex tasks such as computer vision. Whether it is for intelligent movie or photo editing, securing a sensitive building, counting objects in a production line, or detecting traffic signs, lanes, or pedestrians by an autonomous car, computer vision is being used more and more to solve such real-time problems that once were expected to be solved only by humans. This is where OpenCV Framework enters the scene. In the past few years, OpenCV has grown into a full-fledged cross-platform computer vision framework, with a focus on speed and performance. All around the world, developers and researchers are using OpenCV to realize their computer vision application ideas and algorithms.

This book aims to get you to grips with both Qt and OpenCV frameworks by taking you through their basic concepts and taking you up to the point where you can easily continue on your own to develop and deliver computer vision applications across a wide range of platforms. The only assumption made for being able to easily follow the topics covered in this book is that you are familiar and comfortable with C++ programming concepts such as classes, templates, inheritance, and so on. Even though the tutorials, screenshots, and examples covered throughout the book are based on the Windows operating system, the difference on macOS and Linux operating systems are also mentioned wherever necessary.

This book is the result of months of hard work, and it would have not been possible without the invaluable help of Lawrence Veigas, for his perfect editing; Karl Phillip Buhr, for his honest and insightful reviews and comments; Parth Kothari, without whom this book would have not been a reality; and Zainab Bootwala, Prajakta Naik, Aaron Lazar, Supriya Thabe, Tiksha Sarang, Rekha Nair, Jason Monteiro, Nilesh Mohite, and everyone at Packt Publishing who helped create and deliver this book as it is to you, our readers from all around the world.

Who this book is for

This book is for readers interested in building computer vision applications. Intermediate knowledge of C++ programming is expected. Even though no knowledge of Qt5 and OpenCV 3 is assumed, if you're familiar with these frameworks, you'll benefit.

What this book covers

Chapter 1, Introduction to OpenCV and Qt, goes through all the required initializations. Starting with from where and how to get Qt and OpenCV frameworks, this chapter will describe how to install, configure, and make sure that everything is correctly set in your development environment.

Chapter 2, Creating Our First Qt and OpenCV Project, takes you through the Qt Creator IDE, which we'll use to develop all of our applications. In this chapter, you'll learn how to create and run your application projects.

Chapter 3, Creating a Comprehensive Qt+OpenCV Project, goes through the most common capabilities required for a comprehensive application, including styles, internationalization, and support for various languages, plugins, and so on. Through this process, we'll create a comprehensive computer vision application ourselves.

Chapter 4, Mat and QImage, lays out the foundation and teaches you the basic concepts needed to write computer vision applications. In this chapter, you'll learn all about the OpenCV Mat class and the Qt QImage class, how to convert and pass them between the two frameworks, and a lot more.

Chapter 5, The Graphics View Framework, teaches you how to use the Qt Graphics View framework and its underlying classes in order to easily and efficiently display and manipulate graphics in an application.

Chapter 6, Image Processing in OpenCV, takes you through the image processing capabilities offered by OpenCV framework. You'll learn about transformations, filters, color spaces, template matching, and so on.

Chapter 7, Features and Descriptors, is all about detecting keypoints from images, extracting descriptors from keypoints, and matching them with each other. In this chapter, you'll learn about various keypoint and descriptor extraction algorithms and end up using them to detect and locate a known object inside an image.

Chapter 8, Multithreading, teaches you all about the multithreading capabilities offered by the Qt framework. You'll learn about mutexes, read-write locks, semaphores, and various thread synchronization tools. This chapter will also teach you about both the low-level (QThread) and high-level (QtConcurrent) multithreading technologies in Qt.

Chapter 9, Video Analysis, covers how to correctly process videos using Qt and OpenCV frameworks. You'll learn about object tracking using MeanShift and CAMShift algorithms and other video processing functionalities. This chapter also includes a comprehensive overview of all the essential and basic concepts for video processing, such as Histograms and back-projection images.

Chapter 10, Debugging and Testing, takes you through the debugging capabilities of the Qt Creator IDE and how it is configured and set up. In this chapter, you'll also learn about the unit testing capabilities offered by the Qt framework by writing example unit tests that can be run manually or automatically every time our project is built.

Chapter 11, Linking and Deployment, teaches you to build both OpenCV and Qt frameworks dynamically or statically. In this chapter, you'll also learn about deploying Qt and OpenCV applications on various platforms. At the end of this chapter, we'll create an installer using Qt Installer Framework.

Chapter 12, Qt Quick Applications, introduces you to Qt Quick Applications and the QML language. In this chapter, you'll learn about the QML language syntax and how to use it along with Qt Quick Designer to create beautiful Qt Quick Applications for desktop and mobile platform. You'll also learn about integrating QML and C++ in this chapter.

To get the most out of this book

Although every required tool and software, the correct version, and how it is installed and configured is covered in the initial chapters of the book, the following is a list that can be used as a quick reference:

A regular computer with a more recent version of Windows, macOS, or Linux (such as Ubuntu) operating system installed on it.

Microsoft Visual Studio (on Windows)

Xcode (on macOS)

CMake

Qt Framework

OpenCV Framework

To get an idea of what a regular computer is these days, you can search online or ask a local shop; however, the one you already have is most probably enough to get you started.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Computer-Vision-with-OpenCV-3-and-Qt5. We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/ComputerVisionwithOpenCV3andQt5_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The QApplication class is the main class responsible for controlling the application's control flow, settings, and so on."

A block of code is set as follows:

#include "mainwindow.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

#include "mainwindow.h" #include int main(int argc, char *argv[]) {

QApplication a(argc, argv);

MainWindow w; w.show(); return a.exec(); }

Any command-line input or output is written as follows:

binarycreator -p packages -c config.xml myinstaller

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Clicking the Next button moves you to the next screen."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

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 without 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 existing 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 more 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 more 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 more 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 usually 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 usually 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

Qt Creator is the name of the IDE (Integrated Development Environment) used to develop Qt 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 OpenCV 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 with 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

C

hapter 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

,

Feature

s 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

C

hapter 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 Detection 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 updated list of Extra modules and how they are used.

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

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 Development Kit) on your computer. We'll start by setting up Qt on Windows OS and 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 need 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 need 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 need 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 Qt Downloads main page (https://download.qt.io/archive/) to always access previous versions of Qt.

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)

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 installation 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 Components 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.

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 faced 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 Components 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 IDE that we'll be using throughout the book to build applications.

Qt Assistant:

This application is used 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 used 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 excellent 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 distribution 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 create 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 create 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 account 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 provide 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 ones 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?