avatarZahid Parvez

Summary

The website content provides an overview of various image interpolation algorithms used in OpenCV for manipulating digital images, including their characteristics and use cases.

Abstract

The article discusses the concept of image interpolation in OpenCV, which is a crucial aspect of digital image processing. It explains that interpolation is unavoidable during operations such as resizing, lens distortion correction, perspective change, and image rotation. The author outlines different interpolation methods, including nearest-neighbor, bilinear, pixel area relation, bicubic, and Lanczos interpolation. Each method has its own merits and is suitable for specific scenarios: nearest-neighbor is ideal for maintaining precise pixel borders; bilinear is a general-purpose algorithm that generates new pixel values based on surrounding pixels; pixel area relation is similar to nearest-neighbor but preferred for image decimation; bicubic favors closer values and results in sharper images; and Lanczos, while slow, is best for detailed images like those with text. The article also provides code snippets and visual examples to illustrate the effects of each interpolation method.

Opinions

  • The nearest-neighbor interpolation is recommended for scaling images where pixel borders must be preserved, such as pixel art.
  • Bilinear interpolation is considered a good general-purpose algorithm due to its balance between computational efficiency and image quality.
  • Pixel area relation resampling is suggested for image decimation to avoid moiré patterns.
  • Bicubic interpolation is noted for producing sharper images compared to bilinear interpolation, making it suitable for enlarging images with fine details.
  • Lanczos interpolation is highlighted as the best choice for images with intricate details, despite its slower performance and higher resource demands.
  • The article implies that the choice of interpolation algorithm can significantly affect the quality of the manipulated image, and it should be selected based on the specific requirements of the task at hand.

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

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.

By default, OpenCV applies this interpolation algorithm unless oterwise defined. This is a good general purpose interpolation algrothim.

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 results

Pixel area relation resampling

This algorithm yields similar results to the Nearest-neighbor algorithm. According to OpenCV’s documentation, it preforms resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results.

As we can see in the compressions below, when upscaling, it had the same results as the nearest-neighbour algorithm, however, downscaling resulted in output similar to the bilinear algorithm.

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

DisplayImageComparison1x3(original_px_im,
                          ScaleImageByRatio(original_px_im,2,2,cv2.INTER_AREA),
                          ScaleImageByRatio(original_px_im,.5,.5,cv2.INTER_AREA),
                          "2x scale",
                          ".5x scale")
Pixel area relation resampling results

Bicubic interpolation

This method of resizing uses bicubic interpolation. It works by averaging the weights for each pixel adjacent to the current image location, then using that value as a new pixel in the image. This method is similar to the bilinear algorithm however instead of linear interpolation, the weighting favours the values closer to the source pixel (imagine a S curve between 2 points instead of a line). This method would result in a ‘sharper’ output compared to bilinear interpolation

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

DisplayImageComparison1x3(original_px_im,
                          ScaleImageByRatio(original_px_im,2,2,cv2.INTER_CUBIC),
                          ScaleImageByRatio(original_px_im,.5,.5,cv2.INTER_CUBIC),
                          "2x scale",
                          ".5x scale")
Bicubic interpolation results

Lanczos interpolation

Uses Lanczos resampling over 8×8 pixel neighborhood and is quiet slow and resource-intensive. This is best used for images containing lots of intricate details (such as text)

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

DisplayImageComparison1x3(original_px_im,
                          ScaleImageByRatio(original_px_im,2,2,cv2.INTER_LANCZOS4),
                          ScaleImageByRatio(original_px_im,.5,.5,cv2.INTER_LANCZOS4),
                          "2x scale",
                          ".5x scale")
Lanczos interpolation results

If you would like to get a copy of the code used in this article, it can be found here on Github.

Want to make use of your knowledge of image interpolation? check out Geometric Transformations using OpenCV (python)

Image Processing
Opencv
Python
Computer Vision
Recommended from ReadMedium