Image interpolation in OpenCV

Zahid Parvez
3 min readJan 8, 2023

Some form of image interpolation is always taking place when manipulating digital images — whether it’s resizing them to increase or decrease the total number of pixels, correcting for lens distortion, changing perspective, or rotating an image.

Rephrase Interpolation is an approximation that may result in image degradation each time it’s performed. The image may vary significantly depending on the interpolation algorithm used.

In this post, i will provide an overview of the diffrent interpolation algorithm that OpenCV uses

Source: https://commons.wikimedia.org/wiki/File:Comparison_of_1D_and_2D_interpolation.svg

Nearest-neighbor interpolation

The nearest neighbour algorithm selects the value of the nearest pixel and does not interpolate between values from other neighbouring pixels. This algorithm does not create any pixel values that doesn’t exist on the original image. This type of interpolation is ideal for scaling images where precise pixel borders must be maintained (such when working with pixel art).

DisplayImageComparison1x3(original_im,
ScaleImageByRatio(original_im,2,2,cv2.INTER_LINEAR),
ScaleImageByRatio(original_im,.5,.5,cv2.INTER_LINEAR),
"2x scale",
".5x scale")

DisplayImageComparison1x3(original_px_im,
ScaleImageByRatio(original_px_im,2,2,cv2.INTER_LINEAR),
ScaleImageByRatio(original_px_im,.5,.5,cv2.INTER_LINEAR),
"2x scale",
".5x scale")
Nearest-neighbor interpolation results

Bilinear interpolation

Bilinear interpolation is an algorithm that applies linear interpolation between pixel values in the x and y directions. As images are represented as a matrix, this is implemented as a two-step process; firstly pixels are interpolated in the x direction, then the y direction. This algorithm will generate new pixel values (that doesnt exist on the original) as the pixel values are calculated based on weighted averages of the surrounding pixels.

--

--

Zahid Parvez

I am an analyst with a passion for data, software, and integration. In my free time, I also like to dabble in design, photography, and philosophy.