Book Image

Computer Vision with OpenCV 3 and Qt5

By : Amin Ahmadi Tazehkandi
4 (1)
Book Image

Computer Vision with OpenCV 3 and Qt5

4 (1)
By: Amin Ahmadi Tazehkandi

Overview of this book

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

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:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. 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 at https://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 at https://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."

Note

Warnings or important notes appear like this.

Note

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.