🦨

[ML] Interpolation Explained

2024/05/04に公開

I'll explain intepolation methods briefly.

1. What is interpolation?

Sometimes, we are facing scene that have to resize the image size, interpolation is used such time.
It reduces or expands the values of data by various method to fit the shape of the data.

This time, I'll explain method details.

2. cv2.INTER_NEAREST

This is the nearest-neighbor interpolation method. It's the simplest and fastest approach. It selects the nearest pixel value without considering surrounding pixels, which may result in a blocky image when enlarging.

This "nearest pixel" is typically determined based on geometric distance on the grid of pixels of 'origin image' and 'interpolated image'.

3. cv2.INTER_LINEAR

Takes a weighted average of the four closest pixel values to estimate a new pixel value. The weights are proportional to the inverse distance from the points of a original image, so closer data has greater impact.

Same as INTER_NEAREST, closest pixel values is determined based on geometric distance, The contribution of each of the four surrounding pixels is weighted based on their proximity to the point in the new image. The closer a pixel is, the more it contributes to the new pixel’s value. This weighting is typically linear, blending the colors based on the distances.

4. cv2.INTER_AREA

This interpolation is used for shrinking an image. It calculates the pixel value by averaging the pixels in the source image that contribute to the area in the resized image. It provides better results for image decimation.

Computes the output pixel value by averaging the values of all the pixels that fall within the area of the source image corresponding to the target pixel. It’s especially effective when reducing the size of an image.

5. cv2.INTER_CUBIC

This method uses bicubic interpolation over 4x4 pixel neighborhoods. It's slower than bilinear interpolation but produces smoother and sharper images, especially beneficial when enlarging images.

Involves sixteen pixels in the input image for estimating each pixel in the output image. It considers the nearest four pixels in each direction and applies a cubic function to model the pixel intensity.

6. cv2.INTER_LANCZOS4

This is a high-quality resampling method using the Lanczos kernel, which considers 8x8 pixel neighborhoods. It's excellent for both downsampling and upsampling and provides very high-quality results, albeit at a higher computational cost.

Uses a sinc-based kernel, which is a well-defined mathematical formula, making it excellent for both downsampling and upsampling.

Summary

As a guide, I think it's best to use INTER_AREA for downsampling, and INTER_CUBIC or INTER_LINER for upsampling.

Discussion