OpenCV — Basic image processing in Python

Santhosh Kannan
featurepreneur
Published in
3 min readJan 17, 2023

What is OpenCV?

Open Source Computer Vision(OpenCV) is one of the widely used library for computer vision and image processing tasks. It includes various algorithms for image and video analysis, such as object detection, motion analysis, image processing, feature detection, etc.

How to install OpenCV library?

OpenCV can be easily install from the pip package manager as follows:

pip install opencv-python

The installation can be verified by running the following in python

import cv2

print(cv2.__version__)

How to read an image?

An image can be represented as a multidimensional array of pixel information, where each element is a numeric value representing the color of the pixel.

img = cv2.imread("cat.jpg")
print(img)

The output of the above code is an array of numeric values. If you print the variable type of img, it will be a numpy.ndarray. Hence the image can be manipulated by numpy methods too.

How to display an image?

An image which has been read by OpenCV can be displayed using matplotlib library.

import matplotlib.pyplot as plt
fig = plt.imshow(img)
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.show()

This will display the image but the color of the image will be off. This is because OpenCV reads the image in the BGR format. To convert it into RGB format:

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
fig = plt.imshow(img)
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.show()

How to get the dimensions of the image?

The height, width and color channels of an image can be obtained by

height, width, channels = img.shape
print((height, width, channels)) # (2500, 2392, 3)

How to resize an image?

An image can be resized by using the resize function of OpenCV by providing the new dimensions

resized_img = cv2.resize(img,(500,500),interpolation=cv2.INTER_AREA)
print(resized_img.shape) # (500, 500, 3)

Images can also be resized by providing a scaling factor instead of the new dimensions.

resized_img = cv2.resize(img, dsize = None,fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
print(resized_img.shape) # (1250, 1196, 3)

Here, the fx and fy represent the scaling factor by with the image will be resized in the width and height respectively. Therefore, in this example, the image is resized to half it’s original size.

How to crop an image?

Since an image is represented as a numpy array, the image can be easily cropped by using array slicing.

x, y = 600, 100
h, w = 1500, 1300
cropped_img = img[y:y+h, x:x+w]
fig = plt.imshow(cropped_img)
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.show()

Here, (x, y) represent the coordinate of the top-left pixel of the cropped image and h and w are the height and width of the cropped image respectively.

How to convert a color image to grayscale?

A color image has 3 color channels but a grayscale image has only 1 color channel.

img = cv2.imread("cat.jpg")
print("color image dimensions: ",img.shape) # (2500, 2392, 3)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print("grayscale image dimensions: ",gray_img.shape) # (2500, 2392)

In order to display the grayscale image using imshow(), the cmap parameter has to be set to “gray”.

fig = plt.imshow(gray_img, cmap="gray")
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.show()

How to save an image?

Images can be save by using the imwrite() function.

cv2.imwrite("gray_cat.jpg",gray_img)

--

--