Monday 13 November 2023

Resizing the image with OpenCV

Using resize method, we can resize the frame or image. This method takes the original image (or frame) and resizes it to the specified dimensions.

Example

new_frame = cv.resize(frame, new_dimensions, interpolation=cv.INTER_AREA)

 

frame: Input frame or image that we want to resize.

new_dimensions: Tuple represents the width and height of resized image (new_width, new_height)

interpolation=cv.INTER_AREA: Specifies to use INTER_AREA interpolation method to resize the image. INTER_AREA interploation method is most suitable for downscaling the image.

 

Find the below working application.

 

resize_image.py

import cv2 as cv

def resize_frame(frame, scale_width=0.5, scale_height=0.5):
    width = int(frame.shape[1] * scale_width)
    height = int(frame.shape[0] * scale_height)
    new_dimensions = (width, height)
    return cv.resize(frame, new_dimensions, interpolation=cv.INTER_AREA)

# Read the image as matrix of pixels
frame = cv.imread('photos/tiger.png')

print(f'height : {frame.shape[0]}')
print(f'width : {frame.shape[1]}')

print('\nResizing image to half')
frame = resize_frame(frame)

print(f'height : {frame.shape[0]}')
print(f'width : {frame.shape[1]}')

# Display the image in new window
cv.imshow('Tiger', frame)

# Wait for Infinite amount of time for a keyboard key to be pressed
cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

Output

height : 596
width : 1004

Resizing image to half
height : 298
width : 502

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment