OpenCV Resize the image
Sometimes, it is necessary to transform the loaded image. In the image processing, we need to resize the image to perform the particular operation. Images are generally stored in Numpy ndarray(array). The ndarray.shape is used to obtain the dimension of the image. We can get the width, height, and numbers of the channels for each pixel by using the index of the dimension variable.
Resized Dimensions : (199, 300, 3)
The resizing of image means changing the dimension of the image, its width or height as well as both. Also the aspect ratio of the original image could be retained by resizing an image. OpenCV provides cv2.resize() function to resize the image. The syntax is given as:
Example of resizing the images
There are several ways to resize the image. Below are some examples to perform resize operation:
Retain the aspect ratio
Original Dimensions : (332, 500, 3) Resized Dimensions : (199, 300, 3)
In the above example, the scale_per variable holds the percentage of the image which needs to be scaled. The value<100 is used to downscale the provided image. We will use this scale_per value along with the original image's dimension to calculate the width and height of the output image.
Upscale with resize()
Original Dimensions : (332, 500, 3) Resized Dimensions : (398, 600, 3)
Not retaining the aspect ratio
In the below example, we have provided a specific value in pixel for width and the height will remain unaffected.
Original Dimensions : (332, 500, 3) Resized Dimensions : (440, 500, 3)
In the below example, the scale_per value holds the percentage by which height has to be scaled or we can provide the specific value in pixels.
Original Dimensions : (332, 500, 3) Resized Dimensions : (200, 500, 3)
Resize the specific width and height