Thursday, July 4, 2019

OpenCV – Image Resizing techniques


In this tutprial you will learn the various Image Resizing techniques.

  1. How to downsize the image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will downsize image to 60%, and print the dimensions of Original size and Resized dimensions.

''' How to downsize the image to 60%?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# input the percentage for resizing the image
scale_percent = 60   # used 60 for downsize the image to 60%,
                      
# calculations for resize image
width = int(img.shape[1]*scale_percent/100)
height = int(img.shape[0]*scale_percent/100)
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (205, 328, 3)

2. How to upsize the image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will upsize image to 150%, and print the dimensions of Original size and Resized dimensions.

''' How to upsize the image to 150%?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# input the percentage for resizing the image
scale_percent = 150   # used 150 for upsize the image to 150%,
                     # if we want to upsize 200% use 200

# calculations for resize image
width = int(img.shape[1]*scale_percent/100)
height = int(img.shape[0]*scale_percent/100)
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (513, 822, 3)

3. How to resize width of image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will resize width of image to 700 pixels, and print the dimensions of Original size and Resized dimensions.
''' How to resize width of image to 700px?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# calculations for resize image
width = 700
height = img.shape[0]   # keep the original height
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (342, 700, 3)

4.How to resize Height of image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will resize height of image to 500 pixels, and print the dimensions of Original size and Resized dimensions.
''' How to resize height of image to 500px?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# calculations for resize image
width = img.shape[1]   # keep the original width
height = 500
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (500, 548, 3)

5.How to resize Width, Height of image using OpenCV ?
To resize the image, we have to use cv2.resize() function, in this example we will resize width of image to 600 pixels and height to 500 pixels, and print the dimensions of Original size and Resized dimensions.

''' How to resize width, height of the image to required value?'''

import cv2

# input image
img = cv2.imread('messi5.jpg')

# to know the dimensions of image
print("Original Dimensions: ", img.shape)

# input values for resize image
width = 600  
height = 500
dim = (width, height)

# resize image
resized = cv2.resize(img, dim)

print('Resized Dimensions: ', resized.shape)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output
Original Dimensions:  (342, 548, 3)
Resized Dimensions:  (500, 600, 3)


=======================================

No comments:

Post a Comment