Updated April 11, 2023
Introduction to Open CV resize()
Open CV resize() is a prebuilt function available in python language utilized for scaling the images that are input by users to be analyzed. Scaling is a quintessential function for processing images and is of great use for applications associated with machine learning. It essentially aids in substantially reducing the pixel numbers that are present in an image, thereby reducing the time required for training the system in terms of neural networks, as more are the numbers of pixels present in an image, more is the quantum of input notes that system has to interpret which in lay man’s terms expands the complexity of the image processing model for machine learning systems. Apart from its function in reducing complexity, it also aids in the process of zooming in and out of an image. In several instances, a user is required to resize the image (either by shrinking the image size or scaling the image size to meet the given requirement). There are several interpolation methods that can be utilized from Open CV for resizing the images, with resize function being one of them.
Syntax:
The following is the syntax used for the application of the resize function in the python coding language:
cv2.resize(* src, ** dsize * [,* dst * [,* fx * [,* fy[,* interpolation]]]]) * * * * * * * * * * * *
There are various choices of Interpolation Method available when the coder using the Open CV resize option for resizing the image. They have listed below:
- INTER_AREA: This interpolation method is usually utilized by the coder when there is a need for the provided image to be shrunk.
- INTER_CUBIC: This interpolation method is usually utilized by the coder when there is a need for a slower but more efficient resultant image to be produced at the output and speed of the program is not of utmost priority.
- INTER_LINEAR: This interpolation method is usually utilized by the coder when there is need for zooming in and out of the images being processed. It is the default method when no other method is specified for the interpolation.
Parameters:
The following are the parameters that are present in the Open CV resize function that has specific usage to enable the function to amplify or minimize the image provided:
Parameter | Description |
src | It is the source image that has been provided by the user. This parameter is essential to be entered. |
size | The required size that the output image should have. This parameter is essential to be entered. |
fx | This is an optional parameter. It represents the scaling factor alongside the horizontal axis |
fy | This is an optional parameter. It represents the scaling factor alongside the vertical axis |
interpolation | [This is an optional parameter] The flag was generally taken of the below-mentioned methods for interpolation.
NEAREST – it is the nearest neighbour of interpolation I NTER_LINEAR – it is a bilinear interpolation. (it is used as the default method unless another interpolation method is specified) INTER_AREA – This method is used for the resampling of the image by the usage of the pixel area to be relaxed. This turns out to be a method that is preferred in codes focusing upon the decimation of images. This is because it provides outputs that more noise-free. But in case the image is zoomed it provides results similar to the INTER_NEAREST method. INTER_CUBIC – it is a bi-cubic interpolation method that ranges over 4×4 pixel- neighbour of interpolation I INTER_LANCZOS4 – it is a Lanczos interpolation method that ranges over an 8×8 pixel-neighbor |
Example of Open CV resize()
Following is an example to demonstrate the use of the Open CV resize() function:
Code:
import cv2
import numpy as np1
import matplotlib.pyplot as plt1
% matplotlib qt
# The matplotlib qt is a special command that is used in order for displaying an externally functioning window
image1 = cv2.imread("C:\Users\PriyankaBanerjee\OneDrive \documents\Important folders\educba logo.png", 1)
# System is loading the provided image that needs to be resized
mid1 = cv2.resize(image1, (0, 0), fx = 0.1, fy = 0.1)
large1 = cv2.resize(image1, (1050, 1610))
stretch_n1 = cv2.resize(image1, (780, 540),
interpolation = cv2.INTER_NEAREST)
titles1 =["Original", "Half", "Bigger", "Interpolation Nearest"]
images1 =[image, half, bigger, stretch_near]
count1 = 4
for a1 in range(count):
plt1.subplot(2, 2, a1 + 1)
plt1.title(titles1[a1])
plt1.imshow(images1[a1])
plt.show()
Output:
The demonstrated examples show four images, one being the original image using the EduCBA logo as the input image which has to be a further process to see the visual difference upon application of the function. The resize function is applied on it and the resultant images have been turned into the resized version with interpolation set at nearest, half, and bigger to display the difference post-application of the Open CV resize() function.
Note One important factor that must be accounted for and borne in mind while using the Open CV resize() function is the tuple that is passed in order to determine the size of the new image that is being processed has the order of (with and height) instead of the much-expected order that the user might assume (being height, width). Hence the order of the image is reversed and the coordination must be used keeping the Height: width dimensions in mind.
Conclusion
Open CV resize() function is an important inbuilt function that enables to instantaneously resize by magnifying or minimizing images at a single go. Resize is a quintessential function for processing images and is of great use fir applications associated with machine learning. It essentially aids in substantially reducing the pixel numbers that are present in an image, thereby reducing the time required for training the system in terms of neural networks, as more are the numbers of pixels present in an image, more is the quantum of input notes that system has to interpret which in layman’s terms expands the complexity of the image processing model for machine learning systems. Apart from its function in reducing complexity, it also aids in the process of zooming in and out of an image.
Recommended Articles
We hope that this EDUCBA information on “Open CV resize()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.