【问题标题】:Logic behind nearest neighbor interpolation最近邻插值背后的逻辑
【发布时间】:2020-05-05 10:20:39
【问题描述】:

我有一个任务,我需要在 python 中从头开始重新创建最近邻插值函数。 几天前我刚开始学习这门语言,所以我正在尝试编写每一个小步骤来实现这一目标。

这是我第一次尝试解决它:) 其背后的原因是(对于给定的图像和0.5的比例)将原始图像的位置X和Y缩放到X'和Y',如下所示:

给定图像的形状:10x10。 我想将其缩放到 5x5 (这是缩小比例)

缩放前的 X 和 Y 位置

X=[0,1,2,3,4,5,6,7,8,9] Y=[0,1,2,3,4,5,6,7,8,9]

缩放后的 X 和 Y 位置

X'=[0,2.25,4.5,6.75,9] Y'=[0,2.25,4.5,6.75,9]

圆角

X'=[0,2,5,7,9] Y'=[0,2,5,7,9]

然后我使用这些位置从原始图像中查找像素

我不知道这是否有意义或者我错过了什么

我的代码 (我命名变量的方式不是很好)

def interpolation_nn(image, scale):

    # saving the type of the image
    dtype = image.dtype

    #Adding padding to the image
    img_p = np.pad(image.astype(np.float32), 1)

    # Calculation of the size of the original image and of the interpolated image
    #Original img
    height,width = image.shape 

    #interpolated image
    Scaled_width = (width * scale)
    Scaled_height = (height * scale)

    # Calculation of pixel coordinates in the interpolated image
    Scaled_X_coordinates=np.linspace(0.0, width, num=Scaled_width)
    Scaled_Y_coordinates=np.linspace(0.0, height, num=Scaled_height)

    #rounding my positions
    Scaled_X_coordinates=np.around(Scaled_X_coordinates)
    Scaled_Y_coordinates=np.around(Scaled_Y_coordinates)

    #edited
    finalMatrix= np.zeros(shape=(np.around(Scaled_height).astype(int) ,np.around(Scaled_width).astype(int)))
    pixels=[]

    #Here, i store every pixels from the original image using the scaled coordinates
    #into an array of pixels
    for Line in Scaled_Y_coordinates.astype(int)  :
        for Column in Scaled_X_coordinates.astype(int):
            pixel = img_p[Line,Column]
            pixels.append(pixel)

    #Here i reconstruct the scaled image using the array of pixels from above
    Pixel_counter=0
    for i in range(np.around(Scaled_height).astype(int)):
        for j in range(np.around(Scaled_width).astype(int)):
            finalMatrix[i][j]=pixels[Pixel_counter]
            Pixel_counter=Pixel_counter+1

    #returning a new matrix with the same type as the given img
    return finalMatrix.astype(dtype)

我不知道如何查看原始图像的像素以重新创建具有新缩放位置的新图像。如果有什么不清楚的地方请追问:)

【问题讨论】:

    标签: python numpy interpolation nearest-neighbor linear-interpolation


    【解决方案1】:

    如果您在计算中做对了所有事情,那么缺少的部分就像这样简单:

    for a in X_corresponding :
        column = [image[a,b] for b in Y_corresponding]
        finalMatrix.append( column )
    

    这里有现成的解决方案:

    def nn_interpolate(A, new_size):
        """Vectorized Nearest Neighbor Interpolation"""
    
        old_size = A.shape
        row_ratio, col_ratio = np.array(new_size)/np.array(old_size)
    
        # row wise interpolation 
        row_idx = (np.ceil(range(1, 1 + int(old_size[0]*row_ratio))/row_ratio) - 1).astype(int)
    
        # column wise interpolation
        col_idx = (np.ceil(range(1, 1 + int(old_size[1]*col_ratio))/col_ratio) - 1).astype(int)
    
        final_matrix = A[:, row_idx][col_idx, :]
    
    return final_matrix
    

    或者,如果您想了解更多详细信息,请访问以下网址: https://gist.github.com/KeremTurgutlu/68feb119c9dd148285be2e247267a203

    【讨论】:

    • 你好 :) np.around() 的输出是浮点类型。我不能使用浮点数来索引数组:/ IndexError: arrays used as indices must be of integer (or boolean) type
    • 我尝试添加.astype(int),但出现越界错误
    • 我无法返回 finalMatrix 因为AttributeError: 'list' object has no attribute 'astype'
    • @Hazel 似乎您对指数的计算存在某种缺陷。请自行调试或使用我在答案中发布的即用型解决方案。
    • 如果我尝试X=nn_interpolate(img,0.5) plt.imshow(X) plt.show() 的功能它不起作用
    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 2011-11-10
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 2010-12-05
    相关资源
    最近更新 更多