Reader small image

You're reading from  Mastering OpenCV 4 with Python

Product typeBook
Published inMar 2019
Reading LevelExpert
PublisherPackt
ISBN-139781789344912
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Alberto Fernández Villán
Alberto Fernández Villán
author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán

Right arrow

Image Processing Techniques

Image processing techniques are the core of your computer vision projects. They can be seen as useful key tools, which you can use to complete various tasks. In other words, image processing techniques are like building blocks that should be kept in mind when processing your images. Therefore, a basic understanding of image processing is required if you are to work with computer vision projects.

In this chapter, you will learn most of the common image processing techniques you need. These will be complemented by the other image processing techniques covered in the next three chapters of this book (histograms, thresholding techniques, contour detection, and filtering).

In this chapter, the following topics will be covered:

  • Splitting and merging channels
  • Geometric transformations of images—translation, rotation, scaling, affine transformation...

Technical requirements

The technical requirements for this chapter are listed as follows:

  • Python and OpenCV
  • A Python-specific IDE
  • The NumPy and Matplotlib packages
  • A Git client

For further details on how to install these requirements, see Chapter 1, Setting Up OpenCV. The GitHub repository, Mastering OpenCV 4 with Python, containing all the supporting project files necessary to work through this book from the first chapter to the last one, can be accessed at: https://github.com/PacktPublishing/Mastering-OpenCV-4-with-Python.

Splitting and merging channels in OpenCV

Sometimes, you have to work with specific channels on multichannel images. To do this, you have to split the multichannel image into several single-channel images. Additionally, once the processing has been done, you may want to create one multichannel image from different single-channel images. In order to both split and merge channels, you can use the cv2.split() and cv2.merge() functions, respectively. The cv2.split() function splits the source multichannel image into several single-channel images. The cv2.merge() function merges several single-channel images into a multichannel image.

In the next example, splitting_and_merging.py, you will learn how to work with these two aforementioned functions. Using the cv2.split() function, if you want to get the three channels from a loaded BGR image, then you should use the following code:

 ...

Geometric transformations of images

In this section the first, an introduction to the main geometric transformations of images will be covered. We will look at some examples the of scaling, translation, rotation, affine transformation, perspective transform, and cropping of images. The two key functions to perform these geometric transformations are cv2.warpAffine() and cv2.warpPerspective(). The cv2.warpAffine() function transforms the source image by using the following 2 x 3 M transformation matrix:

The cv2.warpPerspective() function transforms the source image using the following 3 x 3 transformation matrix:

In the next subsections, we will learn about the most common geometric transformation techniques, which we will learn more about when we look at the geometric_image_transformations.py script.

...

Image filtering

In this section, we are going to tackle how to blur and sharpen images, applying both several filters and custom-made kernels. Additionally, we will look at some common kernels that we can use to perform other image-processing functionalities.

Applying arbitrary kernels

OpenCV provides the cv2.filter2D() function in order to apply an arbitrary kernel to an image, convolving the image with the provided kernel. In order to see how this function works, we should first build the kernel that we will use later. In this case, a 5 x 5 kernel will be used, as shown in the following code:

kernel_averaging_5_5 = np.array([[0.04, 0.04, 0.04, 0.04, 0.04], [0.04, 0.04, 0.04, 0.04, 0.04], [0.04, 0.04, 0.04, 0.04, 0.04],...

Arithmetic with images

In this section, we will learn about some common arithmetic operations that can be performed on images, such as bitwise operations, addition, and subtraction, among others. In connection with these operations, one key point to take into account is the concept of saturation arithmetic, which is explained in the following subsection.

Saturation arithmetic

Saturation arithmetic is a type of arithmetic operation where the operations are limited to a fixed range by restricting the maximum and minimum values that the operation can take. For example, certain operations on images (for example, color space conversions, interpolation techniques, and so on) can produce values out of the available range. Saturation...

Morphological transformations

Morphological transformations are operations that are normally performed on binary images and based on the image shape. The exact operation is determined by a kernel-structuring element, which decides the nature of the operation. Dilation and erosion are the two basic operators in the area of morphological transformations. Additionally, opening and closing are two important operations, which are derived from the two aforementioned operations (dilation and erosion). Finally, there are three other operations that based on the difference between some of these previous operations.

All of these morphological transformations are described in the following subsections, and the morphological_operations.py script shows the output when applying these transformations to some test images. The key points will also be commented on.

...

Color spaces

In this section, the basics of popular color spaces will be covered. These color spaces are—RGB, CIE L*a*b*, HSL and HSV, and YCbCr.

OpenCV provides more than 150 color-space conversion methods to perform the user's required conversions. In the following example, the conversions are performed from an image loaded in the RGB (BGR in OpenCV) to the other color spaces (for example, HSV, HLS, or YCbCr).

Showing color spaces

The RGB color space is an additive color space, where a specific color is represented by red, green, and blue values. Human vision works in a similar way, so this color space is an appropriate way to display computer graphics.

The CIELAB color space (also known as CIE L*a*b* or simply...

Color maps

In many computer vision applications, the output of your algorithm is a grayscale image. However, human eyes are not good at observing changes in grayscale images. They are more sensitive when appreciating changes in color images, therefore a common approach is to transform (recolor) the grayscale images into a pseudocolor equivalent image.

Color maps in OpenCV

In order to perform this transformation, OpenCV has several color maps to enhance visualization. The cv2.applyColorMap() function applies a color map on the given image. The color_map_example.py script loads a grayscale image and applies the cv2.COLORMAP_HSV color map, as shown in the following code:

img_COLORMAP_HSV = cv2.applyColorMap(gray_img, cv2.COLORMAP_HSV...

Summary

In this chapter, we reviewed most of the common image processing techniques you need in your computer vision projects. In the next three chapters (Chapter 6, Constructing and Building Histograms, Chapter 7, Thresholding Techniques, and Chapter 8, Contour Detection, Filtering, and Drawing), the most common image processing techniques will be reviewed.

In Chapter 6, Constructing and Building Histograms, you will learn how to both create and understand histograms, which are a powerful technique that is used to better understand image content.

Questions

  1. Which function splits a multichannel into several single-channel images?
  2. Which function merges several single-channel images into a multichannel image?
  3. Translate an image 150 pixels in the x direction and 300 pixels in the y direction.
  4. Rotate an image named img by 30 degrees with respect to the center of the image with a scale factor of 1.
  5. Build a 5 x 5 averaging kernel and apply it to an image using cv2.filter2D().
  6. Add 40 to all the pixels in a grayscale image.
  7. Apply the COLORMAP_JET color map to a grayscale image.

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering OpenCV 4 with Python
Published in: Mar 2019Publisher: PacktISBN-13: 9781789344912
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 €14.99/month. Cancel anytime

Author (1)

author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán