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 5: Basics of Image Processing

In the previous chapter, we learned about and demonstrated various ways to capture images and videos for image processing and computer vision applications. We learned how to use Command Prompt and Python 3 programming extensively to read images and to interface with the USB webcam and the Raspberry Pi camera module.

In this chapter, we will look at how to perform basic arithmetic and logical operations on images with NumPy, OpenCV, and matplotlib. We will also learn about different color channels and image properties in detail.

The following is a list of the topics that will be covered in this chapter:

  • Retrieving image properties
  • Basic operations on images
  • Arithmetic operations on images
  • Blending and transitioning images
  • Multiplying images by constants and one another
  • Creating a negative of an image
  • Bitwise logical operations on images

This chapter has a lot of hands-on exercises that use Python 3...

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

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

Retrieving image properties

We can retrieve and use many properties, such as the data type, the dimensions, the shape, and the size of bytes of an image with NumPy. Open the Python 3 interpreter by running the python3 command in the command prompt. Then, run the following statements one by one:

>>> import cv2
>>> img = cv2.imread('/home/pi/book/dataset/4.1.01.tiff', 0)
>>> print(type(img))

The following is the output of these statements:

<class 'numpy.ndarray'>

The preceding output confirms that the OpenCV imread() function read an image and stored it in NumPy's ndarray format. The following statement prints dimensions of the image it read:

>>> print(img.ndim)
2

The image is read in grayscale mode, which is why it is a two-dimensional image. It just has a single channel composed of intensities of grayscale. Now, let's see its shape:

>>> print(img.shape)
(256, 256)

The preceding...

Basic operations on images

Let's perform a few basic operations, such as splitting and combining the channels of a color image and adding a border to an image. We will continue this demonstration in interactive mode. Let's import OpenCV and read a color image, as follows:

>>> import cv2
>>> img = cv2.imread('/home/pi/book/dataset/4.1.01.tiff', 1)

For any image, the origin—the (0, 0) pixel—is the pixel at the upper-left corner. We can retrieve the intensity values for all the channels by running the following statement:

>>> print(img[10, 10])
[34 38 44]

These are the intensity values of the blue, green, and red channels, respectively, for pixel (10, 10). If you only want to access an individual channel for a pixel, then run the following statement:

>>> print(img[10, 10, 0])
34

The preceding output, 34, is the intensity of the blue channel. Similarly, we can access the green and red channels with...

Arithmetic operations on images

We know that images are nothing but NumPy ndarrays and we can perform arithmetic operations on images just as we can perform them on ndarrays. If we know how to apply numerical or arithmetic operations to matrices, then we should not have any trouble doing the same when the operands for those operations are images. Images must be of the same size and must have the same number of channels for us to perform arithmetic operations on them, and these operations are performed on individual pixels. There are many arithmetic operations, such as addition and subtraction. The first is the addition operation. We can add two images by using either the NumPy Addition or the add() function in OpenCV, as follows:

import cv2
img1 = cv2.imread('/home/pi/book/dataset/4.2.03.tiff', 1)
img2 = cv2.imread('/home/pi/book/dataset/4.2.05.tiff', 1)
cv2.imshow('NumPy Addition', img1 + img2 )
cv2.imshow('OpenCV Addition', cv2.add(img1...

Blending and transitioning images

The cv2.addWeighted() function computes the weighted sum of the two images that we pass in as arguments. This causes them to blend. The following is some code that demonstrates this concept of blending:

import cv2
img1 = cv2.imread('/home/pi/book/dataset/4.2.03.tiff', 1)
img2 = cv2.imread('/home/pi/book/dataset/4.2.05.tiff', 1)
cv2.imshow('Blended Image',
           cv2.addWeighted(img1, 0.5, img2, 0.5, 0))
cv2.waitKey(0)
cv2.destroyAllWindows()

In the preceding code, we are passing the following five arguments to the OpenCV cv2.addWeighted() function:

  • img1: The first image
  • alpha: The coefficient for the first image (0.5 in the preceding example)
  • img2: The second image
  • beta: The coefficient for the second image (0.5 in the preceding example)
  • gamma: The scalar value (0 in the preceding example)

OpenCV uses the following formula to compute the output image:

output image = (alpha *...

Multiplying images by a constant and one another

Just like normal matrices or NumPy ndarrays, images can be multiplied by a constant and with one another. We can multiply an image by a constant, as follows:

import cv2
img1 = cv2.imread('/home/pi/book/dataset/4.2.03.tiff', 1)
img2 = cv2.imread('/home/pi/book/dataset/4.2.05.tiff', 1)
cv2.imshow('Image1', img1 * 2)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the preceding code, every element of the ndarray representing the image is multiplied by 2. Run the preceding program and see the output. We can also multiply images with one another, as follows:

cv2.imshow('Image1', img1 * 2)

The result is likely to look like noise.

Creating a negative of an image

In terms of pure mathematics, when we invert the colors of an image, it creates a negative of the image. This inversion operation can be computed by subtracting the color of a pixel from 255. If it is a color image, we invert the color of all the planes. For a grayscale image, we can directly compute the inversion by subtracting it from 255, as follows:

import cv2
img = cv2.imread('/home/pi/book/dataset/4.2.07.tiff', 0)
negative = abs(255 - img)
cv2.imshow('Grayscale', img)
cv2.imshow('Negative', negative)
cv2.waitKey(0)
cv2.destroyAllWindows()

The following is the output of this:

Figure 5.6 – A negative of an image

Figure 5.6 – A negative of an image

Try to find the negative of a color image, we just need to read the image in color mode in the preceding program.

Note:

The negative of a negative will be the original grayscale image. Try this on your own by computing the negative of the negative again for our...

Bitwise logical operations on images

The OpenCV library has many functions for computing bitwise logical operations on images. We can compute bitwise logical AND, OR, XOR (exclusive OR), and NOT (inversion) operations. The best way to demonstrate how these functions work is to use them with binary (black and white) images:

import cv2
import numpy as np
import matplotlib.pyplot as plt
a = [0, 255, 0]
img1 = np.array([a, a, a], dtype=np.uint8)
img2 = np.transpose(img1)
not_out = cv2.bitwise_not(img1 )
and_out = cv2.bitwise_and(img1, img2)
or_out = cv2.bitwise_or(img1, img2)
xor_out = cv2.bitwise_xor(img1, img2)
titles = ['Image 1', 'Image 2', 'Image 1 NOT', 'AND', 'OR', 'XOR']
images = [img1, img2, not_out, and_out, or_out, xor_out]
for i in range(6):
        plt.subplot(2, 3, i+1)
        plt.imshow(images[i], cmap='gray')
        plt.title(titles[i])
        plt.axis('off')
plt.show()

We created our...

Summary

In this chapter, we started by looking at image processing with OpenCV and NumPy. We learned about some important concepts, such as image channels, arithmetic and logical operations, and the negative of an image. Along the way, we also learned to use a bit more functionality in Python 3 and the NumPy library. The bitwise logical operations that we learned today will be very useful when writing programs for the functionality of object tracking by color in the next chapter.

In the next chapter, we will study colorspaces, transformations, and thresholding images.

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