OpenCV INTER_AREA with its example

Python’s OpenCV module offers a lot of flexibility. For scaling an image, OpenCV offers a range of interpolations. We are here to examine INTER_AREA, which provides pixel area relations, one of the methods that OpenCV provides. This method is slower than other methods used for resizing an image but INTER_AREA produces better results when reducing the size of the image by dimensions. It also prevents the details of the image by preserving the details of the original image.

INTER_AREA uses an averaging method to compute the value of pixels of output as well as the input image. It is also known as resampling using pixel area relations. INTER_AREA is used in image compression and object detection where the size of the image is reduced.

Allow me to explain it with an example:

import cv2
# Read the image
image_given = cv2.imread("image.jpg")

# Define the new size
width = int(image_given.shape[1] * 0.7)
height = int(image_given.shape[0] * 0.7)
dimenssion = (width, height)

# Resize the image using INTER_AREA interpolation
resized = cv2.resize(image_given, dimenssion, interpolation = cv2.INTER_AREA)

# Show the original and resized images
cv2.imshow("Original Image", image_given)
cv2.imshow("Resized Image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Explanation:

Use cv2.imread() to read the picture in the provided example. The new image’s width and height are then used to specify its size. The image can be resized using the cv2.resize() method. Including the new dimensions The resulting image is shown when the cv2.imshow() method is used.

By selecting the nearby pixels, INTER_AREA employs pixel relations to calculate the value of the hidden pixel. Because it eliminates noise that could otherwise emerge in the image when doing it manually, this method for shrinking an image is effective.

It is important to note that the aspect ratio, or the ratio of width to height, also changes when we use OpenCV to resize the size of an image. We should calculate the image’s height and width appropriately in order to retain it.

Output:

OpenCV INTER_AREA with its example

With this, we have come to the end of this tutorial. Learn, the concept of image sharpening and contrasting in Python using OpenCV

Leave a Reply

Your email address will not be published. Required fields are marked *