Updated April 7, 2023
Introduction to OpenCV scale image
Open CV scale image is a Function present in the open CV library, which enables the images entered by the user to be upscaled in terms of the dimension and size of the original image provided. The function of upscaling images proves to be very handy in machine learning or ML applications where image processing is required. This method helped by reducing the quantum of the pixels contained in the original image provided by the user, which serves to provide various advantages. The function is used in various applications, such as zooming of the images or even shrinking the images to scale down the image dimensions to meet the specific requirements provided by a specific application. The open CV library provides several interpolation methods in order for the scaling of the image that is being sourced by the user to be done more easily. In this topic, we are going to learn about OpenCV scale images.
Ways of Scaling an in image in Open CV
There are several ways in which an image can be scaled using the open CV library. let us discuss in brief what are the various ways in which the function can be performed:
- Preservation of the overall aspect ratio – in such a scenario, the application where the function of scaling is being used needs the height do width overall ratio with respect to the input image needs to be preserved. in such cases, there are two scenarios that can be applied for scaling the image –
- Downscale or decreasing the overall size of the image provided by the user
- Upscale or increasing the overall size of the image that the user provides
- No preservation is observed for the overall aspect ratio for the input image – In this situation, the aspect ratio is not needed to be preserved for the applications which use such output images. in such an application, two output images can be resulted by scaling of the image to be performed
- only the width of the input image is scaled, either increasing or decreasing the overall width of the image but at the same time keeping the height of the original image unchanged
- only the height of the input image is scaled, either increasing or decreasing the overall height of the image but at the same time keeping the width of the original image unchanged
- In certain applications, the output image which is being scaled needs to be of a certain height and width to fit the brief; in such a case, both the height as well as the width of the image can be scaled that is increasing or decreasing both the height and the width of the original image according to the specific output results that are needed.
Syntax and functions used for scaling image
Following is the syntax which is used for implementing the Open CV scale image function:
cv2 *. * resize(* src, * dsize * [*,* dst[*,* fx[*,* fy * [*,* * interpolation *] *] *] *] *)
Example of Open CV Scale image:
Following is an example that illustrates the use of the scaling method using the open CV library in Python 3 programming language:
# command used to import the Open CV library to utilize the scale image function import cv2 # command im read is used in order to read the image which is being sourced by the programmer img_1 = cv2.imread('/ desktop/ img/ python3/ educba.png', cv2.IMREAD_UNCHANGED) # displaying the original image cv2.imshow("Displaying the original image / source image :", img_1 print (Scaled Dimensions of the original image are: ',img _ 1.shape) # defining the scaled imaged percent part of the original size of the input image scale_percent_1 = 60 width _ 1 = int(img_1.shape * scale_percent _ 1 / 100) height _ 1 = int(img_1.shape * scale_percent _ 1 / 100) dim _ 1 = (width_1, height_1) # scaling the original image to defined dimensions scale _ 1 = cv2.resize(img _ 1, dim _ 1, interpolation = cv2.INTER _ AREA) print(Scaled Dimensions of the original image are : ',scale_1.shape) # the resultant image is displayed which is showed together show the difference in the images after scaling has been performed on it cv2.imshow("Scaled output image", scale_1) cv2.waitKey(0) cv2.destroyAllWindows()
The output of the example for Open CV scale image:
Displaying the original image/source image :
Scaled Dimensions of the original image are : (149, 200, 4) Scaled output image Dimensions are : (89, 120, 4) Displaying the output scaled image:
Conclusion – OpenCV scale image
The Open CV scale image function of upscaling images proves to be very handy in machine learning or ML applications where image processing is required. This method helped by reducing the quantum of the pixels that are contained in the original image that the user provides with serves to provide various advantages such as, Reducing the amount of time taken which is required for training the operational neural networks as the more number of pixels that are processed by the system, more is the amount of time required for the system to acquaint itself. Therefore, a greater number of neural networks with greater pixel sizes correspond to A greater number of notes that are input by the user, which increases the complexity involved for the model to function.
We hope that this EDUCBA information on “OpenCV scale image” was beneficial to you. You can view EDUCBA’s recommended articles for more information.