How to scale or resize images in OpenCV?

In scaling we can modify the size of images. Either we can increase or decrease the size of image.

In OpenCV for resizing image by cv2.resize() method. We can specify size of the images manually or by the scaling factor. OpenCV provide various interpolation methods.

Resize method accepts the following parameters −

  • src − Source image for the operation.
  • dst − output image of the operation.
  • dsize − Size of the output image.
  • fx − Scale factor along the horizontal axis.
  • fy − Scale factor along the vertical axis.
  • Interpolation − An integer variable of interpolation method.

Following are the interpolation methods:

  • cv2.INTER_AREA: It is used when we need to shrink image.
  • cv2.INTER_CUBIC: It is slow but more accurate.
  • cv2.INTER_LINEAR: It is default in resize method. It is used when zooming is required.
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("redimg.jpg")
img1 = cv2.resize(img, (200, 200), cv2.INTER_AREA)
img2 = cv2.resize(img, (200, 200), cv2.INTER_CUBIC)
img3 = cv2.resize(img, (200, 200), cv2.INTER_LINEAR)

img_arr = [img1, img2, img3]
count = 3

for i in range(count):
plt.subplot(2, 2, i + 1)

Leave a Comment

Your email address will not be published. Required fields are marked *