Reader small image

You're reading from  Raspberry Pi Computer Vision Programming. - Second Edition

Product typeBook
Published inJun 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781800207219
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Ashwin Pajankar
Ashwin Pajankar
author image
Ashwin Pajankar

Ashwin Pajankar is an author, a YouTuber, and an instructor. He graduated from the International Institute of Information Technology, Hyderabad, with an MTech in Computer Science and Engineering. He has been writing programs for over two and a half decades. He is proficient in Linux, Unix shell scripting, C, C++, Java, JavaScript, Python, PowerShell, Golang, HTML, and assembly language. He has worked on single-board computers such as Raspberry Pi and Banana Pro. He is also proficient with microcontroller boards such as Arduino and the BBC Micro:bit. He is currently self-employed and teaches on Udemy and YouTube. He also organizes programming boot camps for working professionals and software companies.
Read more about Ashwin Pajankar

Right arrow

Chapter 10: Histograms, Contours, and Morphological Transformations

In the previous chapter, we learned about and demonstrated the basic- and intermediate-level concepts surrounding the areas of image processing and computer vision.

From this chapter onward, we will learn about and demonstrate advanced concepts that will prepare us for writing programs for applications in real life. First, we will look at the theoretical foundations of computing histograms with an ndarray. Then, we will learn how to compute it for grayscale and color image channels. We will also learn how to compute and visualize contours. Finally, we will learn about various mathematical morphological operations in detail and demonstrate how to use them with various structuring elements. We will learn about and demonstrate the following topics:

  • Computing and visualizing histograms
  • Visualizing image contours
  • Applying morphological transformations to images

After completing this chapter, you...

Technical requirements

The code files of this chapter can be found on GitHub at https://github.com/PacktPublishing/raspberry-pi-computer-vision-programming/tree/master/Chapter10/programs.

Check out the following video to see the Code in Action at https://bit.ly/2NsLkZv.

Computing and visualizing histograms

A histogram is a graphical representation of a distribution of data. Basically, it is the graphical depiction of a frequency distribution table. Let me explain this through an example. Suppose we have a dataset such as (1, 2, 1, 3, 4, 1, 2, 3, 4, 4, 2, 3, 4). Here, a frequency distribution looks as follows:

Figure 10.1 – Frequency distribution

If we are to plot a bar graph so that the x axis represents elements and the y axis represents the frequency in which they occur, then this is known as a histogram. We can use np.histogram() to compute a histogram. plt.hist() can compute and directly plot it. Let's write some code that will use both functions to interpret the data in the preceding table:

import numpy as np
import matplotlib.pyplot as plt
a = np.array([1, 2, 1, 3, 4, 1, 2, 3, 4, 4, 2, 3, 4])
hist, bins = np.histogram(a)
print(hist)
print(bins)
plt.hist(a)
plt.show()

The output looks as follows...

Visualizing image contours

A curve that joins all the points that lie continuously along the boundary that have the same value as the color of the pixels is known as a contour. Contours are used for detecting the boundaries in an image. Contours are also used for image segmentation. Contours are usually computed using edges in an image. However, contours are closed curves and that is their main distinction from the edges in an image. It is always a good idea to apply the thresholding operation to an image before we extract contours from an image. It will increase the accuracy of the computation of the contour operation.

The cv2.findContours() function is used to compute the contours in an image. This function accepts an image array, the mode of the retrieval of the contours, and the method for the approximation of contours as arguments. It then returns a list of computer contours in the image. The contour retrieval mode can be any of the following:

  • CV_RETR_CCOMP
  • CV_RETR_TREE...

Applying morphological transformations to images

Morphological operations are mathematical in nature and they change the shape of an image. These operations can best be demonstrated visually with binary images. We can apply morphological operations to eliminate a lot of unnecessary information, such as noise, in an image. A morphological operation accepts an image and a kernel as inputs. We will create a custom binary image as a binary image since this is the most suitable way to visually demonstrate morphological operations.

The mathematical morphological operation of erosion contracts the boundaries in an image. In a binary image, the white part is considered the foreground and the black part is considered the background. The erosion operation sets all the pixels on the boundary of the background part that is white to black, thus effectively shrinking the white region. Morphological dilation is the exact opposite of the erosion operation. It adds the white pixels near the boundary...

Summary

In this chapter, we learned and demonstrated the concepts of histograms in general and saw how to create a simple histogram from a simple single-dimensional array. Then, we saw how to visualize a histogram for grayscale and color images. We also demonstrated how to use image contours. Finally, we visually demonstrated the operations that are performed in the area of mathematical morphology. These morphological operations will be extremely useful for real-life applications, some of which we will demonstrate in Chapter 11, Real-Life Applications of Computer Vision.

In the next chapter, we will demonstrate many of the concepts we learned in this and the earlier chapters by building real-life applications such as movement detectors, chroma keys with a green screen, and barcode detection in still images. It will be an exciting and interesting chapter that culminates all the knowledge we have gained so far.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Computer Vision Programming. - Second Edition
Published in: Jun 2020Publisher: PacktISBN-13: 9781800207219
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
Ashwin Pajankar

Ashwin Pajankar is an author, a YouTuber, and an instructor. He graduated from the International Institute of Information Technology, Hyderabad, with an MTech in Computer Science and Engineering. He has been writing programs for over two and a half decades. He is proficient in Linux, Unix shell scripting, C, C++, Java, JavaScript, Python, PowerShell, Golang, HTML, and assembly language. He has worked on single-board computers such as Raspberry Pi and Banana Pro. He is also proficient with microcontroller boards such as Arduino and the BBC Micro:bit. He is currently self-employed and teaches on Udemy and YouTube. He also organizes programming boot camps for working professionals and software companies.
Read more about Ashwin Pajankar