Image Resizing using OpenCV | Python
Image resizing refers to the scaling of images. Scaling comes in handy in many image processing as well as machine learning applications. It helps in reducing the number of pixels from an image and that has several advantages e.g. It can reduce the time of training of a neural network as the more the number of pixels in an image more is the number of input nodes that in turn increases the complexity of the model.
It also helps in zooming in on images. Many times we need to resize the image i.e. either shrink it or scale it up to meet the size requirements. OpenCV provides us several interpolation methods for resizing an image.
Choice of Interpolation Method for Resizing:
- cv2.INTER_AREA: This is used when we need to shrink an image.
- cv2.INTER_CUBIC: This is slow but more efficient.
- cv2.INTER_LINEAR: This is primarily used when zooming is required. This is the default interpolation technique in OpenCV.
Syntax: cv2.resize(source, dsize, dest, fx, fy, interpolation)
- source: Input Image array (Single-channel, 8-bit or floating-point)
- dsize: Size of the output array
- dest: Output array (Similar to the dimensions and type of Input image array) [optional]
- fx: Scale factor along the horizontal axis [optional]
- fy: Scale factor along the vertical axis [optional]
- interpolation: One of the above interpolation methods [optional]
Below is the code for resizing:
numpy as np
matplotlib.pyplot as plt
"D:\sims\eb\sim21\EB-ML-06-10-2022-Test-Output-15\PERFORATION\Overkill\Fail\Blister 1 2022-03-12 12-59-43.859 T0 M0 G0 3 PERFORATION Mono.bmp"
[image, half, bigger, stretch_near]
Note: One thing to keep in mind while using the cv2.resize() function is that the tuple passed for determining the size of the new image ((1050, 1610) in this case) follows the order (width, height) unlike as expected (height, width).