SoFunction
Updated on 2024-10-30

python implementation of image nearest neighbor interpolation

Introduction:

nearest neighbor interpolationNearest Neighbour Interpolatealgorithm is a commonly used image size scaling algorithm in image processing, which is popular among engineers due to its simple implementation and fast computational speed.

Image interpolation technique is one of the important research methods in the field of image super-resolution, which aims at obtaining a high-resolution image (High Resolution, HR) based on an existing low-resolution image (Low Resolution, LR).

This paper analyzes the process of nearest neighbor interpolation algorithm on one hand and implements the basic nearest neighbor interpolation algorithm with the help of python on the other hand.

Notes:Some online sources translate it as "near neighbor" and others as "near".

1, nearest neighbor interpolation algorithm idea

The purpose of interpolation is to obtain the pixel values of an unknown target image based on the known pixel values of the image.The interpolation transformation process is shown in the following figure (the background of the PPT drawing is not removed):

included among thesesrcdenotes the original image, tar denotes the target image obtained by interpolation, and H and W denote the height and width of the image respectively. The core of interpolation is to find the mapping relationship between (tar_x, tar_y) and (src_x, src_y) so as to realize the assignment of value to each pixel point of the target image. Suppose the aim is to expand the length and width of the original image by a factor of (3, 4), i.e.:

ratio_H = tar_H/src_H = tar_x/src_x = 3
ratio_W = tar_W/src_W = tar_y/src_y = 4

By morphing the above equation, the pixel points of the target image and the pixel points of the original image are obtained to be mapped as follows:

tar_x = src_x/ratio_H
tar_y = src_y/ratio_W

Knowing the mapping relationship between pixel points, it is easy to implement the algorithm thatThe algorithm flow is as follows:

  • (1) Create target image based on tar_H and tar_W
  • (2) Calculate the scaling factor ratio
  • (3) Iterate through each pixel of the target image and calculate the mapping relationship
  • (4) Iterate through each pixel of the target image and assign a value to it using the corresponding pixel of the corresponding original image

2、python implement nearest neighbor interpolation

With the previous theoretical analysis is very easy to realize, their own implementation of the more difficult to understand the place is the "coordinate transformation relationship"! If the original image is enlarged by an integer multiple, it is easy to understand, for example, if an original 10x10 image is enlarged to a target 20x20 image, then the value of any pixel (tar_x,tar_y) in the 20x20 image comes from the value of the original 10x10 image, which is the same as that of the original 10x10 image.(src_x,src_y)=int(tar_x/2, tar_y/2), which is exactly where it divides by 2; however it is often necessary to zoom in by fractional multiples, such as zooming from 10x10 to 15x15, so that the value of (tar_x,tar_y) comes from the 10x10 image in (src_x, src_y) = int(tar_x/1.5, tar_y/1.5).

The code is as follows:

def nearest(image, target_size):
    """
    Nearest Neighbour interpolate for RGB  image
    
    :param image: rgb image
    :param target_size: tuple = (height, width)
    :return: None
    """
    if target_size[0] < [0] or target_size[1] < [1]:
        raise ValueError("target image must bigger than input image")
    # 1: Creating a target image by size
    target_image = (shape=(*target_size, 3))
    # 2: Calculate the scaling factors for height and width.
    alpha_h = target_size[0]/[0]
    alpha_w = target_size[1]/[1]

    for tar_x in range(target_image.shape[0]-1):
        for tar_y in range(target_image.shape[1]-1):
            # 3: Calculate any pixel point of the target image person
            # target_image[tar_x,tar_y] needs to be taken from the original image
            # which identified pixel point image[src_x, xrc_y] takes the value of
            # That is, calculate the mapping of the coordinates #
            src_x = round(tar_x/alpha_h)
            src_y = round(tar_y/alpha_w)

            # 4: Assign a value to any pixel of the target image
            target_image[tar_x, tar_y] = image[src_x, src_y]

    return target_image

The interpolation results of the obtained interpolation results are as follows:

It can be seen that after the interpolation of the image after the obvious existence of jagged effect, many places appear "square".

to this article on python image nearest neighbor interpolation is introduced to this article, more related python image neighbor interpolation content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!