Computer Vision with OpenCV 3 and Qt5
eBook - ePub

Computer Vision with OpenCV 3 and Qt5

Amin Ahmadi Tazehkandi, Vinícius G. Mendonça, Karl Phillip Buhr

Share book
  1. 448 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Computer Vision with OpenCV 3 and Qt5

Amin Ahmadi Tazehkandi, Vinícius G. Mendonça, Karl Phillip Buhr

Book details
Book preview
Table of contents
Citations

About This Book

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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Computer Vision with OpenCV 3 and Qt5 an online PDF/ePUB?
Yes, you can access Computer Vision with OpenCV 3 and Qt5 by Amin Ahmadi Tazehkandi, Vinícius G. Mendonça, Karl Phillip Buhr in PDF and/or ePUB format, as well as other popular books in Informatique & Vision par ordinateur et reconnaissance de formes. We have over one million books available in our catalogue for you to explore.

Information

Creating a Comprehensive Qt+OpenCV Project

Professional applications never end up being professional because of some random circumstances. They are designed like this from the very beginning. Of course, it is easier said than done, but it's still quite easy if you already know the golden rule of how to create applications that can be easily extended, maintained, scaled, and customized. The golden rule here is just one simple concept, which fortunately Qt framework already has the means to implement, and that is building applications in a modular fashion. Note that modular in this sense doesn't just mean libraries or different source code modules, but modular in the sense that each of the responsibilities and capabilities of the application is created and built independently of the others. This is, in fact, exactly the way Qt and OpenCV themselves are created. An application that is modularized can be extended very easily, even by different developers from different backgrounds. An application that is modularized can be extended to support many different languages, themes (styles or looks), or better yet, many different capabilities and functions.
In this chapter, we will take on a very important and crucial task, which is building an infrastructure (or architecture) for a comprehensive computer vision application that uses the Qt and OpenCV frameworks. You'll learn how to create Qt applications that can be extended even after they are deployed (shipped to the users). This actually means many things, including how to add new languages to applications, how to add new styles to applications, and most importantly, how to build a plugin-based Qt application that can be extended by adding new plugins to it.
We will start by learning about what goes behind the scenes in general when a Qt application is built, by going through a Qt project's structure and included files. Then, we'll learn about some of the most widely used design patterns in Qt and OpenCV, and how both these frameworks enjoy the advantages of using those design patterns. Then, we will learn how to create an application that can be extended with plugins. We'll also learn about adding new styles and new languages to our applications. By the end of this chapter, we will be able to create the base of a comprehensive computer vision application that is cross-platform, multi-language, plugin-based, and with a customizable look and feel. This base application will be extended in the next two chapters, Chapter 4, Mat and QImage, and Chapter 5, The Graphics View Framework, and later on using plugins for the rest of the book, especially after Chapter 6, Image Processing in OpenCV, when we'll start to really dig into computer vision subjects and OpenCV libraries.
In this chapter, we will cover the following topics:
  • Structure of a Qt project and Qt build process
  • Design patterns in Qt and OpenCV
  • Styling in Qt applications
  • Languages in Qt applications
  • How to use Qt Linguist tool
  • How to Create and Use plugins in Qt

Behind the scenes

In Chapter 2, Creating Our First Qt and OpenCV Project, you learned how to create a simple Qt+OpenCV application called Hello_Qt_OpenCV. This project included almost all of the basic features provided by Qt, although we didn't go into too much detail on how our project was built into an application with a user interface and an (almost acceptable) behavior. In this section, you will learn about what went on behind the scenes when we clicked on the Run button. This will help us with a better understanding of the structure of a Qt project and what the purpose of each file in the project folder is. Let's start by opening the project folder and going through the few files one by one. So, we have the following in the Hello_Qt_OpenCV folder:
 Hello_Qt_OpenCV.pro Hello_Qt_OpenCV.pro.user main.cpp mainwindow.cpp mainwindow.h mainwindow.ui 
The first file in the Hello_Qt_OpenCV.pro list is basically the first file that is processed by Qt when our project is built. This is called a Qt Project file and an internal Qt program called qmake is responsible for processing it. Let's see what it is.

The qmake tool

The qmake tool is a program that helps with creating makefiles using the information inside the *.pro files. This simply means, using a very simple syntax (as opposed to more complex syntax in other make systems), qmake generates all the necessary commands for correctly compiling and building an application, and puts all those generated files inside the Build folder.
When a Qt project is built, it first creates a new build folder, which by default, is on the same level as the project folder. In our case, this folder should have a name similar to build-Hello_Qt_OpenCV-Desktop_Qt_5_9_1_*-Debug, where * can be different, depending on the platform, and you can find it in the same folder where your project folder is located. All files generated by Qt (using qmake and some other tools that you'll learn about in this chapter) and C++ compilers are located in this folder and its subfolders. This is called the Build folder of your project. This is also the place where your application is created and executed from. For example, if you are on Windows, you can find the Hello_Qt_OpenCV.exe file (among many other files) inside the debug or release subfolders in the Build folder. So, from now on we will refer to this folder (and its subfolders) as the Build folder.
For instance, we already know that including the following line in our Qt Project file causes the addition of Qt's core and gui modules to our application:
 QT += core gui 
Let's look even further inside the Hello_Qt_OpenCV.pro file; the following lines are immediately noticeable:
 TARGET = Hello_Qt_OpenCV TEMPLATE = app 
These lines simply mean the TARGET name is Hello_Qt_OpenCV, which is the name of our project and the TEMPLATE type app means that our project is an application. We also have the following:
 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui 
Quite obviously, this is how header files, source files, and user interface files (forms) are included in our project. We even added our own code to the Project file, as follows:
 win32: { include("c:/dev/opencv/opencv.pri") } unix: !macx{ CONFIG += link_pkgconfig PKGCONFIG += opencv } unix: macx{ INCLUDEPATH += "/usr/local/include" LIBS += -L"/usr/local/lib" \ -lopencv_world } 
You already learned that this is how Qt can see OpenCV and use it in a Qt project. Search for the qmake Manual in the Qt help index for more information about all possible commands and functions in qmake and more detailed information about how it works.
After qmake has processed our Qt project file, it starts looking for the source files mentioned in the project. Naturally, every C++ program has a main function (a single and unique main function) in one of its source files (not in the header files), and our application is no exception. Our main function for the application is automatically generated by Qt Creator, and it's inside the main.cpp file. Let's open the main.cpp file and see what it contains:
 #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } 
The first two lines are used to include our current mainwindow.h header and QApplication header files. The QApplication class is the main class responsible for controlling the application's control flow, settings, and so on. What you see here, inside the main function, is the basis of how Qt creates an Event Loop and how its underlying signal/slot mechanism and event handling system works:
 QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); 
To describe it most simply, an instance of QApplication class is created and the application arguments (usually passed through the command line or terminal) are passed to the new instance named a. Then, an instance of our MainWindow class is created, and then it's shown. Finally, the exec() function of the QApplication class is called so that the application is entered into the main loop, and stays on until the window is closed.
To understand how the event loop really works, try removing the last line and see what happens. When you run your application, you may notice that the window is actually shown for a very brief moment, and then it's immediately closed. This is because our application no longer has an event loop and it immediately reaches the end of the application and everything is cleared out from the memory, thus the window is closed. Now, write that line back, and as you would expect, the window stays open because the exec() function only returns when the exit() function is called somewhere (anywhere) in the code, and it returns the value set by exit().
Now, let's move on to the next three ...

Table of contents