Image Resizing using OpenCV

Sometimes, we need to change the size of the given image to a desired size. In openCV we can easily change size of an image using resize() function. Resizing means we can change width and height of the image. The signature of the resize() method is as given below:

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])

The parameters of the above function are described as below:

Parameter Description

src

Input image.

dst

output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.

dsize

output image size; if it equals zero (None in Python), it is computed as:
dsize = Size(round(fx*src.cols), round(fy*src.rows))
Either dsize or both fx and fy must be non-zero.

fx

scale factor along the horizontal axis; when it equals 0, it is computed as
(double)dsize.width/src.cols

fy

scale factor along the vertical axis; when it equals 0, it is computed as
(double)dsize.height/src.rows

Let us look at an example for image resizing. The following Code-1 shows how to resize the input image to a fixe size of (200, 300). The size of destination image can be anything.

Code 1: Image Resizing using OpenCV
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import cv2
import numpy as np
try:
    path = r'F:\img\lena.jpg'
    img = cv2.imread(path, cv2.IMREAD_COLOR)
    out = cv2.resize(img,(300,200),interpolation=cv2.INTER_CUBIC)
    print(img.shape, out.shape)
    cv2.imshow('Input',img)
    cv2.imshow('output',out)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
except Exception as e:
    print(str(e))
    print('Unable to read image.')

Line 1 and 2: We start by importing OpenCV and numpy libraries.
Line 3: This lines defines a try block. The exception block (Line 13 and 14) is associated with this block. If any exception occurs, it throws it and code in exception block is executed.
Line 4: Define path of the image to read.
Line 5: Here we read a color image (see second parameter).
Line 6: Here we use resize() function to change size of the input image. We used a fixed size to change the input image size. The third parameter is cv2.INTER_CUBIC which is used to used to interpolate the pixel values. The function returns a new image and stored in out object.
Line 8-11: These lines help display each channel image.

The output obtained when code-1 is executed is shown in Figure 1.

No image
Figure 1. Input image with size 225 x 225 (left). Output image with size 300 x 200 (right).

We can also create a function to increase or decrease the size of the image using a scaling factor. For instance, in Code-2, we created a function named Rescale() to change the size of the function. It accepts two parameters:
Img: the input image
Scale: The scaling factor which is a float value. If the scale factor is greater than 1.0 it increases the size of the image and if its value is less than 1.0 then it downscales the image. For scale=1.0, the size of the image will remain unchanged.

The following Code-2 shows Rescale() function and its usage.

Code 2: Image Resizing with a custom function using OpenCV

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import cv2
import numpy as np
def Rescale(img, scale):
    dim=(int(img.shape[0]*scale),int(img.shape[1]*scale))
    out = cv2.resize(img,dim,interpolation=cv2.INTER_CUBIC)
    return out
try:
    path = r'F:\img\lena.jpg'
    img = cv2.imread(path, cv2.IMREAD_COLOR)
    out = Rescale(img,0.8)
    cv2.imshow('Input',img)
    cv2.imshow('output',out)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
except Exception as e:
    print(str(e))

Line 3-6: We defined a function named Rescale(). It takes two parameters: image and scale. It resizes the image according to the scale and returns the image.
Line 8: Define path of the image to read.
Line 9: Here we read a color image (see second parameter).
Line 10: We call the custom created Rescale() function. We use scale 0.8 which will reduce the size of the output image (80% size of the original image).
Line 11-14: These lines help display the input and output image.

The output from above code is shown in Figure 1. Now this customize Resale() method will upscale or downscale the width and height of the input image and return the output image. If you want scaling to be different width and height, then you have to pass two different parameters and use them to find new height and width.

No image
Figure 2: Original image (left), rescaled image (right).

OpenCV Tutorials