One of the most used libraries for handling image processing and manipulation is openCV for Python users. For image classification, object detection, or optical character recognition, any work related to images in the AI world need some kind of image processing and manipulation most of the time. I decided to write a few tutorials on OpenCV.
In this tutorial, we will focus on a few basic functions of OpenCV. These are basic and can also be very useful at times. We will learn them with examples. So, let’s get ready to get your hands dirty.
Before getting started, these are the libraries that we will use today.
import cv2
import matplotlib.pyplot as plt
We will import other libraries as necessary.
A simple smiley image will be used for this tutorial. Please feel free to download the image from this link.
In this line of code, cv2.imread function is used to read the image and store it as an array.
image = cv2.imread('smiley.jpg')
If you are totally new to OpenCV, here is what the ‘image’ variable looks like:
array([[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
…,
The image can be shown using the .imshow() function. Both Matplotlib and OpenCV libraries have the .imshow functions.
plt.imshow(image)

We will start with how to move or shift an image. A simple smiley image will be used for this tutorial.
Define how to move the image and save it in a move variable. Let’s move the image 100 pixels to the right and 30 pixels down. For that, the move variable will look like this:
move = np.float32([[1, 0, 100], [0, 1, 30]])
Here, 1s and 0s are fixed. 100 in the [0, 2] position will move the image to the right and 30 in the [1, 2] position will move the image down.
shift = cv2.warpAffine(image, move, (image.shape[1], image.shape[0]))
plt.imshow(shift)

If you want to move the image to the left and up, you just need to use negative signs:
move = np.float32([[1, 0, -100], [0, 1, -30]])
shift = cv2.warpAffine(image, move, (image.shape[1], image.shape[0]))
plt.imshow(shift)

The same thing can be achieved using the imutils library as well. Here is the syntax:
import imutils
moved = imutils.translate(image, -100, -30)
It will generate exactly the same image as the last one. As you can see, using the imutils is a bit simpler.
Resizing
Resizing operation is very common in image preprocessing. We increase or decrease the height and width of the images to match our needs. Here is the syntax for resizing in OpenCV:
resized = cv2.resize(image, image_shape, interpolation = cv2.INTER_AREA)
I used ‘interpolation’ as cv2.INTER_AREA. There are other options for that as well. We will talk about them later. Let’s begin with image_shape. It is important to take care of the aspect ratio so that the image does not look distorted. To do that, it is necessary to keep the aspect ratio of the output image as the aspect ratio of the input image.
If we want the height of the output image to be 500, the image shape of the output image needs to be:
rsz = 500.0 / image.shape[1]
image_shape = (500, int(image.shape[0] * rsz))
In the same way, if we need the width of the output image to be 300, we calculate the height of the output image.
rsz = 300.0 / image.shape[0]
image_shape = (int(image.shape[1] * rsz), 300)
resized = cv2.resize(image, image_shape, interpolation = cv2.INTER_AREA)
That was all about the image shape.
The third parameter ‘interpolation’ has several different options. Here are the names:
cv2.INTER_LINEAR
cv2.INTER_NEAREST
cv2.INTER_CUBIC
cv2.INTER_LANCZOS4
If you do not provide any ‘interpolation’ method, the default method OpenCV uses is cv2.INTER_LINEAR method that provides bilinear interpolation.
Please feel free to explore more if you are interested.
The imutils library makes it much easier for us. It calculates the aspect ratio for us. It just requires the height or the width. It calculates the width or height itself. Here is the syntax:
im = imutils.resize(image, width=300)
Here, we provided the width and height will be calculated automatically to keep the aspect ratio intact.
Cropping
If you ever worked with images, you definitely performed cropping in some way. One common way of cropping an image is simply using the slicing of the array. As we talked about in the beginning, an image is nothing but a NumPy array.
Here I am cropping the smiley here:
part = image[0:600, 0:600]
plt.imshow(part)

So, it took the top 600 pixels and 600 pixels from the left. Here is another example:
part = image[100:600, 100:600]
plt.imshow(part)

This time we start at the 100th pixel from the top and the left both. So, here we took from the 100th pixel to the 599th pixel from the top and also the 100th pixel to the 599th pixel from the left. Please feel free to try some other range as well.
Conclusion
There are so many functions available in OpenCV. I just wanted to start with these three basic operations. There will be much more advanced techniques I will write about in my upcoming tutorials. Hope this was helpful.