Resizing and rescaling in OpenCV:


Resizing and Rescaling in OpenCV


Resizing and rescaling are most important step in image processing .

Displaying large images need a lot of processing.We do rescaling to reduce computational costs.

Rescaling is a process of modifying height and width of image or video to particular height and width.

Generally rescaling is done for larger images to convert them to smaller images without losing information in it.


Program
import cv2
def rescale(frame,scale=0.5):
    width=int(frame.shape[1]*scale)
    height=int(frame.shape[0]*scale)
    dimensions=(width,height)
    return cv2.resize(frame,dimensions,interpolation=cv2.INTER_AREA)
img=cv2.imread("bumble.png")
frame_resized=rescale(img,0.75)
cv2.imshow("Resized",frame_resized)
cv2.waitKey(0)


frame.shape[1]  denotes  width of frame

frame.shape[0]  denotes height of frame

scale is float value which can be used to shrink or

enlarge image

cv2.resize() converts a frame into particular dimensions.


Different interpolation methods are used to resize the image.

Preferable interpolation methods are cv.INTER_AREA for shrinking and cv.INTER_CUBIC(slow) & cv.INTER_LINEAR for zooming.

By default, interpolation method used is cv.INTER_LINEAR.

We can also perform rescaling on videos as given below


Program

import cv2
def rescale(frame,scale=0.5):
    width=int(frame.shape[1]*scale)
    height=int(frame.shape[0]*scale)
    dimensions=(width,height)
    return cv2.resize(frame,dimensions,interpolation=cv2.INTER_AREA)
capture=cv2.VideoCapture(0)
while True:
    isTrue,frame=capture.read()
    frame_resized=rescale(frame,scale=0.75)
    cv2.imshow("Original",frame)
    cv2.imshow("Resized",frame_resized)
    if cv2.waitKey(20) & 0xFF==ord('f'):
        break
capture.release()
cv2.destroyAllWindows()