Basic Image Processing

In this article, Ashwin Pajankar, the author of the book, Raspberry PI Computer Vision Programming, takes us through basic image processing in OpenCV. We will do this with the help of the following topics:

  • Image arithmetic operations—adding, subtracting, and blending images
  • Splitting color channels in an image
  • Negating an image
  • Performing logical operations on an image

This article is very short and easy to code with plenty of hands-on activities.

(For more resources related to this topic, see here.)

Arithmetic operations on images

In this section, we will have a look at the various arithmetic operations that can be performed on images. Images are represented as matrices in OpenCV. So, arithmetic operations on images are similar to the arithmetic operations on matrices. Images must be of the same size for you to perform arithmetic operations on the images, and these operations are performed on individual pixels.

  • cv2.add(): This function is used to add two images, where the images are passed as parameters.
  • cv2.subtract(): This function is used to subtract an image from another.

We know that the subtraction operation is not commutative. So, cv2.subtract(img1,img2) and cv2.(img2,img1) will yield different results, whereas cv2.add(img1,img2) and cv2.add(img2,img1) will yield the same result as the addition operation is commutative. Both the images have to be of same size and type, as explained before.

Check out the following code:

import cv2
img1 = cv2.imread('/home/pi/book/test_set/4.2.03.tiff',1)
img2 = cv2.imread('/home/pi/book/test_set/4.2.04.tiff',1)
cv2.imshow('Image1',img1)
cv2.waitKey(0)
cv2.imshow('Image2',img2)
cv2.waitKey(0)
cv2.imshow('Addition',cv2.add(img1,img2))
cv2.waitKey(0)
cv2.imshow('Image1-Image2',cv2.subtract(img1,img2))
cv2.waitKey(0)
cv2.imshow('Image2-Image1',cv2.subtract(img2,img1))
cv2.waitKey(0)
cv2.destroyAllWindows()

The preceding code demonstrates the usage of arithmetic functions on images. Here's the output window of Image1:

Raspberry Pi Computer Vision Programming

Here is the output window of Addition:

Raspberry Pi Computer Vision Programming

The output window of Image1-Image2 looks like this:

Raspberry Pi Computer Vision Programming

Here is the output window of Image2-Image1:

Raspberry Pi Computer Vision Programming

Blending and transitioning images

The cv2.addWeighted() function calculates the weighted sum of two images. Because of the weight factor, it provides a blending effect to the images. Add the following lines of code before destroyAllWindows() in the previous code listing to see this function in action:

cv2.addWeighted(img1,0.5,img2,0.5,0)
cv2.waitKey(0)

In the preceding code, we passed the following five arguments to the addWeighted() function:

  • Img1: This is the first image.
  • Alpha: This is the weight factor for the first image (0.5 in the example).
  • Img2: This is the second image.
  • Beta: This is the weight factor for the second image (0.5 in the example).
  • Gamma: This is the scalar value (0 in the example).

The output image value is calculated with the following formula:

Raspberry Pi Computer Vision Programming

This operation is performed on every individual pixel.

Here is the output of the preceding code:

Raspberry Pi Computer Vision Programming

We can create a film-style transition effect on the two images by using the same function. Check out the output of the following code that creates a smooth image transition from an image to another image:

import cv2
import numpy as np
import time
 
img1 = cv2.imread('/home/pi/book/test_set/4.2.03.tiff',1)
img2 = cv2.imread('/home/pi/book/test_set/4.2.04.tiff',1)
 
for i in np.linspace(0,1,40):
alpha=i
beta=1-alpha
print 'ALPHA ='+ str(alpha)+' BETA ='+str (beta)
cv2.imshow('Image Transition',
   cv2.addWeighted(img1,alpha,img2,beta,0)) time.sleep(0.05) if cv2.waitKey(1) == 27 :    break   cv2.destroyAllWindows()

Splitting and merging image colour channels

On several occasions, we may be interested in working separately with the red, green, and blue channels. For example, we might want to build a histogram for
every channel of an image.

Here, cv2.split() is used to split an image into three different intensity arrays for each color channel, whereas cv2.merge() is used to merge different arrays into a single multi-channel array, that is, a color image.

The following example demonstrates this:

import cv2
img = cv2.imread('/home/pi/book/test_set/4.2.03.tiff',1)
b,g,r = cv2.split (img)
cv2.imshow('Blue Channel',b)
cv2.imshow('Green Channel',g)
cv2.imshow('Red Channel',r)
img=cv2.merge((b,g,r))
cv2.imshow('Merged Output',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The preceding program first splits the image into three channels (blue, green, and red) and then displays each one of them. The separate channels will only hold the intensity values of the particular color and the images will essentially be displayed as grayscale intensity images. Then, the program merges all the channels back into an image and displays it.

Creating a negative of an image

In mathematical terms, the negative of an image is the inversion of colors. For a grayscale image, it is even simpler! The negative of a grayscale image is just the intensity inversion, which can be achieved by finding the complement of the intensity from 255. A pixel value ranges from 0 to 255, and therefore, negation involves the subtracting of the pixel value from the maximum value, that is, 255. The code for the same is as follows:

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

Here is the output window of Greyscale:

Raspberry Pi Computer Vision Programming

Here's the output window of Negative:

Raspberry Pi Computer Vision Programming

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

Logical operations on images

OpenCV provides bitwise logical operation functions for images. We will have a look at the functions that provide the bitwise logical AND, OR, XOR (exclusive OR), and NOT (inversion) functionality. These functions can be better demonstrated visually with grayscale images. I am going to use barcode images in horizontal and vertical orientation for demonstration. Let's have a look at the following code:

import cv2
import matplotlib.pyplot as plt
 
img1 = cv2.imread('/home/pi/book/test_set/Barcode_Hor.png',0)
img2 = cv2.imread('/home/pi/book/test_set/Barcode_Ver.png',0)
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 xrange(6):
   plt.subplot(2,3,i+1)
   plt.imshow(images[i],cmap='gray')
   plt.title(titles[i])
   plt.xticks([]),plt.yticks([])
plt.show()

We first read the images in grayscale mode and calculated the NOT, AND, OR, and XOR, functionalities and then with matplotlib, we displayed those in a neat way. We leveraged the plt.subplot() function to display multiple images. Here in the preceding example, we created a grid with two rows and three columns for our images and displayed each image in every part of the grid. You can modify this line and change it to plt.subplot(3,2,i+1) to create a grid with three rows and two columns.

Also, we can use the technique without a loop in the following way. For each image, you have to write the following statements. I will write the code for the first image only. Go ahead and write it for the rest of the five images:

plt.subplot(2,3,1) , plt.imshow(img1,cmap='gray') , 
plt.title('Image 1') , plt.xticks([]),plt.yticks([])

Finally, use plt.show() to display. This technique is to avoid the loop when a very small number of images, usually 2 or 3 in number, have to be displayed. The output of this is as follows:

Make a note of the fact that the logical NOT operation is the negative of the image.

Exercise

You may want to have a look at the functionality of cv2.copyMakeBorder(). This function is used to create the borders and paddings for images, and many of you will find it useful for your projects. The exploring of this function is left as an exercise for the readers.

You can check the python OpenCV API documentation at the following location:

http://docs.opencv.org/modules/refman.html

Summary

In this article, we learned how to perform arithmetic and logical operations on images and split images by their channels. We also learned how to display multiple images in a grid by using matplotlib.

Resources for Article:


Further resources on this subject:


You've been reading an excerpt of:

Raspberry Pi Computer Vision Programming

Explore Title