Reader small image

You're reading from  Qt 5 and OpenCV 4 Computer Vision Projects

Product typeBook
Published inJun 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789532586
Edition1st Edition
Languages
Right arrow
Author (1)
Zhuo Qingliang
Zhuo Qingliang
author image
Zhuo Qingliang

Zhuo Qingliang (a.k.a. KDr2 online) is presently working at Beijing Paoding Technology Co. LTD., a start-up Fintech company in China that is dedicated to improving the financial industry by using artificial intelligence technologies. He has over 10 years experience in Linux, C, C++, Python, Perl, and Java development. He is interested in programming, doing consulting work, participating in and contributing to the open source community (of course, includes the Julia community).
Read more about Zhuo Qingliang

Right arrow

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

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

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

Adding features using Qt's plugin mechanism

In the previous section, we added a new menu and toolbar named Edit to our application and added an action to both of them to blur the opening image. Let's recall the progress of adding this feature.

First, we added the menu and toolbar, and then the action. After the action was added, we connected a new slot to the action. In the slot, we got the opening image as an instance of QPixmap and converted it into a QImage object, and then a Mat object. The crucial editing work began here—we used OpenCV to modify the Mat instance to get the editing work done. Then, we converted Mat back into QImage and QPixmap accordingly to show the edited image.

Now, if we want to add another editing feature to our app, what should we do? Of course, just repeating the preceding process of adding the blurring action is fine, but it's...

Editing images like a pro

In the preceding section, we looked at how we can add image editing features as plugins for our application. By doing this, we don't need to take care of the user interface, opening and closing the images, and the hotkeys. Instead, we have to add a new editing feature, which we do by writing a subclass of the EditorPluginInterface interface and implementing its pure virtual functions, and then compile it into a plugin file (a shared library file) and copy it into the plugin directory of our application. In this section, we will talk about image editing using OpenCV.

First, let's start with sharpening images.

Sharpening images

Image sharpening is a common feature that is implemented by many...

Summary

In this chapter, we remade the desktop application for image viewing that we built in Chapter 1, Building an Image Viewer, to make an image editor application. Then, we added a simple editing feature to blur images. At the same time, we learned about how to install and set up OpenCV for Qt applications, the data structures related to image processing both in Qt and OpenCV, and how to process images using OpenCV.

Afterward, we learned about the plugin mechanism of the Qt library and abstracted out a way to add editing features to our applications in a more flexible and convenient way as plugins. As an example of this, we wrote our first plugin to erode images.

Then, we moved our attention to the OpenCV library to discuss how to edit images like a prowe made numerous plugins to edit images, to sharpen images, to make cartoon effects, to rotate, to execute affine...

Questions

Try out these questions to test your knowledge of this chapter:

  1. How do we know if an OpenCV function supports in-place operation or not?
  2. How can we add a hotkey for each action we added as a plugin?
  3. How can we add a new action to discard all the changes to the current image in our application?
  4. How can we resize images using OpenCV?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Qt 5 and OpenCV 4 Computer Vision Projects
Published in: Jun 2019Publisher: PacktISBN-13: 9781789532586
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Zhuo Qingliang

Zhuo Qingliang (a.k.a. KDr2 online) is presently working at Beijing Paoding Technology Co. LTD., a start-up Fintech company in China that is dedicated to improving the financial industry by using artificial intelligence technologies. He has over 10 years experience in Linux, C, C++, Python, Perl, and Java development. He is interested in programming, doing consulting work, participating in and contributing to the open source community (of course, includes the Julia community).
Read more about Zhuo Qingliang