Qt 5 and OpenCV 4 Computer Vision Projects
eBook - ePub

Qt 5 and OpenCV 4 Computer Vision Projects

Get up to speed with cross-platform computer vision app development by building seven practical projects

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

Qt 5 and OpenCV 4 Computer Vision Projects

Get up to speed with cross-platform computer vision app development by building seven practical projects

About this book

Create image processing, object detection and face recognition apps by leveraging the power of machine learning and deep learning with OpenCV 4 and Qt 5

Key Features

  • Gain practical insights into code for all projects covered in this book
  • Understand modern computer vision concepts such as character recognition, image processing and modification
  • Learn to use a graphics processing unit (GPU) and its parallel processing power for filtering images quickly

Book Description

OpenCV and Qt have proven to be a winning combination for developing cross-platform computer vision applications. By leveraging their power, you can create robust applications with both an intuitive graphical user interface (GUI) and high-performance capabilities. This book will help you learn through a variety of real-world projects on image processing, face and text recognition, object detection, and high-performance computing. You'll be able to progressively build on your skills by working on projects of increasing complexity.

You'll begin by creating an image viewer application, building a user interface from scratch by adding menus, performing actions based on key-presses, and applying other functions. As you progress, the book will guide you through using OpenCV image processing and modification functions to edit an image with filters and transformation features. In addition to this, you'll explore the complex motion analysis and facial landmark detection algorithms, which you can use to build security and face detection applications. Finally, you'll learn to use pretrained deep learning models in OpenCV and GPUs to filter images quickly.

By the end of this book, you will have learned how to effectively develop full-fledged computer vision applications with OpenCV and Qt.

What you will learn

  • Create an image viewer with all the basic requirements
  • Construct an image editor to filter or transform images
  • Develop a security app to detect movement and secure homes
  • Build an app to detect facial landmarks and apply masks to faces
  • Create an app to extract text from scanned documents and photos
  • Train and use cascade classifiers and DL models for object detection
  • Build an app to measure the distance between detected objects
  • Implement high-speed image filters on GPU with Open Graphics Library (OpenGL)

Who this book is for

This book is for engineers and developers who are familiar with both Qt and OpenCV frameworks and are capable of creating simple projects using them, but want to build their skills to create professional-level projects using them. Familiarity with the C++ language is a must to follow the example source codes in this book.

Trusted by 375,005 students

Access to over 1.5 million titles for a fair monthly price.

Study more efficiently using our study tools.

Information

Year
2019
Print ISBN
9781789532586
Edition
1
eBook ISBN
9781789531831

Editing Images Like a Pro

In Chapter 1, Building an Image Viewer, we built a simple application for image viewing with Qt from scratch. With that application, we can view an image from the local disk, zoom the view in or out, and navigate in the opening directory. In this chapter, we will continue with that application and add some features to allow users to edit the opening image. To achieve this goal, we will get the OpenCV library we mentioned at the beginning of this book involved. To make the application extensible, we will develop most of these editing features as plugins using the plugin mechanism of Qt.
The following topics will be covered in this chapter:
  • Converting images between Qt and OpenCV
  • Extending an application through Qt's plugin mechanism
  • Modifying images using image processing algorithms provided by OpenCV

Technical requirements

Users are required to have the ImageViewer application, which we built in the previous chapter, running correctly. Our development in this chapter will be based on that application.
Also, some basic knowledge of OpenCV is required as a prerequisite. We will be using the latest version of OpenCV, that is, version 4.0, which was released in December 2018, when this book was being written. Since the new version is not yet included in the software repositories of many operator systems, such as Debian, Ubuntu, or Fedora, we will build it from the source. Please don't worry about this—we will cover the installation instructions briefly, later in this chapter.
All of the code for this chapter can be found in this book's GitHub repository at https://github.com/PacktPublishing/Qt-5-and-OpenCV-4-Computer-Vision-Projects/tree/master/Chapter-02.
Check out the following video to see the code in action: http://bit.ly/2FhYLro

The ImageEditor application

In this chapter, we will build an application that can be used to edit images, so we will name it ImageEditor. To edit an image with a GUI application, the first step is opening and viewing the image with that application, which is what we did in the previous chapter. Therefore, I decided to make a copy of the ImageViewer application and rename it ImageEditor, before adding the image editing features to it.
Let's start by copying the sources:
 $ mkdir Chapter-02
$ cp -r Chapter-01/ImageViewer/ Chapter-02/ImageEditor
$ ls Chapter-02
ImageEditor
$ cd Chapter-02/ImageEditor
$ make clean
$ rm -f ImageViewer
With these commands, we copy the ImageViewer directory under the Chapter-01 directory to Chapter-02/ImageEditor. Then, we can enter that directory, run make clean to clean all intermediate files that were generated in the compiling process, and remove the old target executable file using rm -f ImageViewer.
Now that we have a cleaned project, let's rename some of it:
  • The project directory is named with the new project name ImageEditor in the copying process, so we don't need to do anything here.
  • The Qt project file, ImageViewer.pro, should be renamed to ImageEditor.pro. You can do this in your file manager or in a Terminal.
  • In the ImageEditor.pro file, we should rename the TARGET to ImageEditor by changing the TARGET = ImageViewer line to TARGET = ImageEditor.
  • In the source file, main.cpp, we should change the window title by changing the window.setWindowTitle("ImageViewer"); line to window.setWindowTitle("ImageEditor");.
Now that everything has been renamed, let's compile and run the new ImageEditor application, which has been copied from ImageViewer:
 $ qmake -makefile
$ make
g++ -c -pipe ...
# output truncated
# ...
$ ls
ImageEditor ImageEditor.pro main.cpp main.o mainwindow.cpp mainwindow.h
mainwindow.o Makefile moc_mainwindow.cpp moc_mainwindow.o moc_predefs.h
$ export LD_LIBRARY_PATH=/home/kdr2/programs/opencv/lib/
$ ./ImageEditor
You will see that the window is exactly the same as the window of ImageViewer, except that it has a different window title, ImageEditor. Anyway, we have set our editor application up, even though it has no feature for image editing now. We will add a simple editing feature in the next chapter.

Blurring images using OpenCV

In the preceding section, we set up our editor application. In this section, we will add a simple feature of image editing—an action (both on the menu and the toolbar) to blur the image.
We will do this in two steps:
  1. First, we will set up the UI and add the action, and then connect the action to a dummy slot.
  2. Then, we will rewrite the dummy slot to blur the image, which will get the OpenCV library involved.

Adding the blur action

Most of the actions we will add in this chapter will be used to edit an image, so we should categorize them in a new menu and toolbar. First, we will declare three members, that is, the edit menu, the edit toolbar, and the blur action, in the private section of the mainwindow.h header file:
 QMenu *editMenu;
QToolBar *editToolBar;
QAction *blurAction;
Then, we will create them in the MainWindow::initUI and MainWindow::createActions methods, respectively, as follows:
In MainWindow::initUI, this is executed as follows:
 editMenu = menuBar()->addMenu("&Edit");
editToolBar = addToolBar("Edit");
In MainWindow::createActions, this is executed as follows:
 blurAction = new QAction("Blur", this);
editMenu->addAction(blurAction);
editToolBar->addAction(blurAction);
Up until now, we have an edit menu and an edit toolbar with a blur action on both of them. But, if the user clicks either the blur buttons on the toolbar or the blur items under the edit menu, nothing will happen. This is because we haven't connected a slot to that action yet. Let's give the action a slot now. First, we will declare a slot in the private slots section of mainwindow.h, as follows:
 // for editting
void blurImage();
Then, we will give it a dummy implementation in mainwindow.cpp:
 void MainWindow::blurImage()
{
qDebug() << "Blurring the image!";
}
Now that the slot is ready, it's time to connect the triggered signal of the blur action with this slot at the end of the mainwindow::createActions method:
 connect(blurAction, SIGNAL(triggered(bool)), this, SLOT(blurImage()));
When you compile and run the application, you will see the menu, toolbar, and the action. If you trigger the action by clicking it, you will see the message Blurring the image! being printed.
This is what the window and printed message look like:
The UI part is now ready, which means that we can focus on how to blur the image by using OpenCV in the slot in the following sections.

Building and installing OpenCV from the source

In the preceding section, we installed a dummy slot for the blur action that does nothing but prints a simple message. Now, we are going to rewrite the implementation of that slot to do the real blurring work.
As we mentioned in the preceding sections, we will be using the OpenCV library, and, more precisely, the latest version (4.0) of it, to edit the images. So, before we start with our code, we will install the latest version of the OpenCV library and include it in our project.
OpenCV is a set of libraries, tools, and modules that contain classes and functions that are required for building computer vision applications. Its release files can be found on the release page of its official website: https://opencv.org/releases.html. Another thing we need to know is that OpenCV uses a modern build tool called CMake to construct its building system. This means that we must have CMake installed on our operating system to build OpenCV from source, and at least version 3.12 of CMake is required, so please ensure that your version of CMake is set up properly.
How to build a project, especially a large scale project, is a complex topic in the software engineering world. Many tools have been invented to cope with a variety of situations in relation to this topic in the vicissitude during the development of software engineering. From make to Autotools, from SCons to CMake, from Ninja to bazel—there's too many to talk about here. However, up until now, only two of them were introduced in our book: Qmake is the one that was developed by the Qt team and is dedicated to building Qt projects. CMake is another one that is widely adopted nowadays by many projects, including OpenCV.

In our book, we will try our best to keep the use of these tools simple and clear.
The OpenCV release page looks as follows:
We can click the Sources link to download the ZIP package of its source to the local disk and then unzip it. We will build OpenCV by using CMake in a Terminal, so, we will open a Terminal and change its work directory to the directory of the unzipped source. Also, OpenCV doesn't allow you to build directly in the root directory of its source tree, so we should create a separate directory to build it.
Here are the instructions we used to build OpenCV in the Terminal:
 $ cd ~/opencv-4.0.0 # path to the unzipped source
$ mkdir release # create the separate dir
$ cd release
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$HOME/programs/opencv ..
# ... output of cmake ....

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. About Packt
  5. Contributors
  6. Preface
  7. Building an Image Viewer
  8. Editing Images Like a Pro
  9. Home Security Applications
  10. Fun with Faces
  11. Optical Character Recognition
  12. Object Detection in Real Time
  13. Real-Time Car Detection and Distance Measurement
  14. Using OpenGL for the High-Speed Filtering of Images
  15. Assessments
  16. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.5M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
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.5 million books across 990+ topics, we’ve got you covered! Learn about our mission
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 about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Qt 5 and OpenCV 4 Computer Vision Projects by Zhuo Qingliang in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Graphics. We have over 1.5 million books available in our catalogue for you to explore.