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 7: Let's Make Some Noise

In the previous chapter, we learned and demonstrated the concepts of colorspaces and converting them, mathematical transformations, and thresholding operations.

In this chapter, we will learn and demonstrate the concepts related to noise and filtering. This entire chapter is dedicated to understanding the concept of noise in detail. First, we will learn how to simulate various types of noise pattern in depth. Then, we will learn and demonstrate how to use image kernels and the convolution operation. We will also learn how to use the convolution operation to apply various types of filters. Finally, we will learn the basics of low pass filters and demonstrate how to use them to perform blurring and noise removal operations.

We will also use GPIO for demonstrations. In this chapter, we will cover the following topics:

  • Noise
  • Working with kernels
  • 2D convolution with the Signal Processing module in SciPy
  • Filtering and blurring...

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/Chapter07/programs.

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

Noise

Let's understand the concept of noise in detail. In the field of signal processing, noise is simply just any unwanted signal mixed in with the expected signal. When we talk in terms of noise in images or videos, we can define noise as the undesired variation of intensity and color of pixels. This noise can come from multiple sources.

A few examples include dust on a camera lens, grains in the photo film (this one is desired in analog photography and filmmaking), errors in the CCD sensor and its storage, errors during transmission and reception, and errors while scanning the photograph. A very high amount of noise is not desired. This is because high noise reduces the useful and expected signal, affecting the quality of images.

We can mathematically represent the signal-to-noise ratio with the following formula:

Note:

A higher signal-to-noise ratio means a better quality regarding the signal and the image.

Introducing noise to an image...

Working with kernels

Now, let's learn about kernels. We will learn how to use kernels for signal and image processing operations. Kernels are square numerical matrices. Depending on the size and components of the kernel, if we convolve the kernel with the image, we get blurred or sharpened output. Kernels are used for a variety of image processing operations.

Let's look at an example of a simple kernel used for averaging. It can be represented with the following formula:

By using the preceding formula, an averaging kernel that's 3x3 in size can be expressed as follows:

The value of the number of rows and the number of columns is always odd and always the same. They are all square matrices.

We can use the following NumPy code to create the preceding kernel:

K = np.ones((3, 3), np.uint8)/9

Now, we'll learn how to use the preceding kernel and other kernels to process the sample images from the dataset...

2D convolution with the signal processing module in SciPy

Now, let's take a look at the mathematical background of convolution. Convolution is understanding how the shape of a function is affected by another function. The process of computing it and the resultant function is known as a convolution. We can perform convolutions on 1D, 2D, and multidimensional data. Signals are multidimensional entities. Images are a type of signal. So, we can apply convolution to an image.

Note

You can read more about convolution at http://www.songho.ca/dsp/convolution/convolution2d_example.html.

We can perform convolution operations on images with various kernels to process images. For that, we will learn how to use the signal module from SciPy. Let's install the SciPy library with the following command:

pip3 install scipy

We can perform convolution operations on images with various kernels to process images. The function that performs convolution on 2D data is signal.convolve2d...

Filtering and blurring with OpenCV

OpenCV also has many filtering and convolution functions. These filtering functions are cv2.filter2D(), cv2.boxFilter(), cv2.blur(), cv2.GaussianBlur(), cv2.medianBlur(), cv2.sepFilter2D(), and cv2.BilateralFilter(). In this section, we will explore all these functions in detail.

2D convolution filtering

The cv2.filter2D() function, just like the scipy.signal.convolve2d() function, convolves a kernel with an image, thus applying a linear filter to the image. The advantage of the cv2.filter2D() function is that we can apply it to data that has more than two dimensions. We can apply this to color images, too.

This function accepts the input image, the depth of the output image (-1 means the input and the output have the same depth), and a kernel for the convolution operation as arguments. The following code demonstrates the usage of this function:

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread(&apos...

Summary

In this chapter, we learned about noise and low-pass filtering techniques and how they are used to smooth images. The techniques we learned about in this chapter are very useful if we wish to remove various types of noise from images. You will use these techniques for removing, smoothing, and blurring noise while writing programs for real-life applications such as detecting movement in real time with a USB webcam.

In the next chapter, we will study high-pass filtering techniques and how to detect edges using various functions offered by OpenCV that implement various mathematical morphological operators.

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