Resizing Images with OpenCV and Imutils

Samuel Ozechi
4 min readJan 26, 2024

Image resizing is one of the most frequently used image processing
techniques in computer vision. There is little chance you would
complete a computer vision project without having to resize an image at
one point or the other. Resizing images using OpenCV is quite simple and easy.
In this article, I will be using a Google doodle of my favourite high-life musician (Oliver de Coque) to demonstrate image resizing with OpenCV.

Resizing Images with OpenCV

1. Load the desired image using the cv2.imread function.

Result

2. Next we get the dimensions of the original image. Asides the fact that
we would need it in resizing the image, it is also a good practice to note
the image dimension before processing.

Result

3. We then define and set new dimensions for the new resized image

Let’s define and set a 500 * 500 square dimension for the image.

4. Next is to resize the image using the cv2.resize function.
I also use the cv2.rectangle to draw a border around the resized image.

The image is resized using the cv2.resize function which takes the parameters of the image, dimensions for the resized image and interpolation method. The Interpolation parameter accepts a defined method for calculating the pixel values for the new image from the original one. Possible parameters that can be passed are INTER_NEAREST, INTER_LINEAR, INTER_AREA, INTER_CUBIC AND INTER_LANCZ0S4. They use methods as their name suggests (area, linear, nearest neighbour and cubic interpolations) to calculate the pixel values. You can use the INTER_AREA method when not sure what method to use, even though there are methods that perform relatively better for certain resizing operations (like INTER _LINEAR or INTER_CUBIC for enlarging and INTER_AREA for shrinking the image).

Result

The resized image appears distorted compared to the original image
(which has a longer width). This is expected as we did not maintain the
image’s aspect ratio but set arbitrary values (500 pixels for width and 500 for height) as our new dimension. We could resize the image in such a way that we maintain the aspect ratio and avoid distortion.

Resizing Images while maintaining its aspect ratio

To retain the image’s aspect ratio when resizing, we need to get a new
dimension that is equivalent to the aspect ratio of the original
dimensions. This is achieved by calculating the ratio of a new dimension to the original dimension and then using that ratio to determine the new
dimensions( by multiplication) that maintain the original aspect ratio. This concept is best understood in practice.

1. Get the ratio of the new width to the original width

Result

We get a new set of dimensions that maintains the original image’s
aspect ratio. Notice the dimensions must be integer values.

3. Now we can resize the image with the new dimensions which
maintains its aspect ratio.

Result

The resized image is smaller than the original image but still maintains
the same aspect ratio. It could be cumbersome to calculate aspect ratios to resize images, especially when we are working with numerous images.
An easier way to do this will be to use a convenience function from the
imutils package that can resize images while automatically maintaining the aspect ratio of the previous image, and better still, all in one function call.

Resizing images with Imutils

The imutils.resize function resizes the image and maintains the aspect ratio by taking the image object and one dimension( height in our example) as arguments, without having to manually compute the ratio.

The image looks quite similar to the original image despite having much
smaller dimensions.

Summary

I describe the steps required to resize images with OpenCV and Imutils. I also showed how we resize the image in such a way that we maintain its aspect ratio by using a calculated ratio to get the new dimensions. Finally, I showed an easier way to do this using imutils.resize convenience function.

--

--