Scaling simply refers to resizing of images. 

cv2.resize(image, dsize, fx, fy, interpolation) function can be used to resize the image down to or up to the desired size. It has the following arguments:

  1. image: It specifies the image whose size has to be changed.
  2. dsize: It is the desired size of the image.
  3. fx: Horizontal scaling factor.
  4. fy: Vertical scaling factor.
  5. interpolation: flag used for different scaling technique. It has five variations:
  • INTER_NEAREST: a nearest-neighbor interpolation.
  • INTER_CUBIC: it is slow but enlarge image look best with this.
  • INTER_LINEAR: faster than INTER_CUBIC but result is not as good as in INTER_CUBIC.
  • INTER_AREA:  best for image shrinking.
  • INTER_LANCZOS4: a lanczos interpolation over 8×8 pixel neighborhood.

Resizing of image can be done both by preserving and not preserving the aspect ratio of the image.

Resizing with preserving the aspect ratio

Implemented code in Python:

import cv2
 
img = cv2.imread('flower.jpg',1)

 
scaling_factor = 60 # as it is <100, hence it will scale down the image
width = int(img.shape[1] * scaling_factor / 100);
height = int(img.shape[0] * scaling_factor / 100);
dim = (width, height);

# resize the image
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA);

print('Original Dimensions : ',img.shape);
print('Resized Dimensions : ',resized.shape);
#Original Dimensions :  (177, 285, 3)
#Resized Dimensions :  (106, 171, 3)

cv2.imshow("Original image", img);
cv2.imshow("Resized image", resized);

cv2.waitKey(0);
cv2.destroyAllWindows();

Output:

Resizing without preserving the aspect ratio

Implemented code in Python:

import cv2
 
img = cv2.imread('flower.jpg',1)

 
fx = 60 # horizontal scaling factor
fy = 120 # vertical scaling factor
width = int(img.shape[1] * fx / 100);
height = int(img.shape[0] * fy / 100);
dim = (width, height);

# resize the image
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA);

print('Original Dimensions : ',img.shape);
print('Resized Dimensions : ',resized.shape);
#Original Dimensions :  (177, 285, 3)
#Resized Dimensions :  (212, 171, 3)

cv2.imshow("Original image", img);
cv2.imshow("Resized image", resized);

cv2.waitKey(0);
cv2.destroyAllWindows();

Output:

We can also specify the width and height of the image by explicitly passing the desired size.

Implemented code in Python:

import cv2
 
img = cv2.imread('flower.jpg',1)

 
fx = 477 # desired width
fy = 200 # desired height
dim = (fx, fy);

# resize the image
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA);

print('Original Dimensions : ',img.shape);
print('Resized Dimensions : ',resized.shape);
#Original Dimensions :  (177, 285, 3)
#Resized Dimensions :  (212, 171, 3)

cv2.imshow("Original image", img);
cv2.imshow("Resized image", resized);

cv2.waitKey(0);
cv2.destroyAllWindows();

Output:

Thank you Rishika Gupta for this article.


0 Comments

Leave a Reply

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

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert