Image Interpolation in OpenCV

In affine transformations, such as resize , interpolation has actually been used. When a picture is enlarged from 100 × 100 to 200 × 200, new pixels (red dots in the figure below) will be generated, and what is the new pixel value, this is what interpolation does.

There are many interpolation algorithms supported by OpenCV 4.x version, as shown below

Here are just a few of the most commonly used

  • cv2.INTER_LINEAR: bilinear interpolation, default value
  • cv2.INTER_NEAREST: The nearest neighbor interpolation method, find the nearest neighbor (the original pixel), and assign the same value to it
  • cv2.INTER_AREA: Area interpolation, based on local pixel resampling. It is often used for image extraction. If the image is enlarged, the effect is similar to the nearest neighbor interpolation method.
  • cv2.INTER_CUBIC: 3 interpolation based on 4*4 pixel neighborhood
  • cv2.INTER_LANCZOS4: Lanzos interpolation method, calculated from adjacent 8*8 pixels, the formula is similar to bilinear interpolation

Example

 import cv2 image = cv2.imread('lenna.png') cv2.imshow("original image", image) h, w = image.shape[:2] dst = cv2.resize(image, (w*2, h*2), fx=0.75, fy=0.75, interpolation=cv2.INTER_NEAREST) cv2.imshow("INTER_NEAREST", dst) dst = cv2.resize(image, (w*2, h*2), interpolation=cv2.INTER_LINEAR) cv2.imshow("INTER_LINEAR", dst) dst = cv2.resize(image, (w*2, h*2), interpolation=cv2.INTER_AREA) cv2.imshow("INTER_AREA", dst) dst = cv2.resize(image, (w*2, h*2), interpolation=cv2.INTER_CUBIC) cv2.imshow("INTER_CUBIC", dst) dst = cv2.resize(image, (w*2, h*2), interpolation=cv2.INTER_LANCZOS4) cv2.imshow("INTER_LANCZOS4", dst) cv2.waitKey(0) cv2.destroyAllWindows()

This article is reprinted from https://xugaoxiang.com/2022/07/16/opencv-interpolation/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment