# Image interpolation in OpenCV

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

# 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")

# 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.