python - 最近邻插值背后的逻辑

标签 python numpy interpolation nearest-neighbor linear-interpolation

我有一个任务,我需要在 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)

我不知道如何查找原始图像的像素来重新创建具有新缩放位置的新图像。如果有不清楚的地方,请询问:)

最佳答案

如果您在计算中所做的一切都是正确的,那么缺失的部分就很简单:

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

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

关于python - 最近邻插值背后的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59805810/

相关文章:

python - 将值分配给 Pandas 数据框中的重复行

python - Python 文件保存错误无效

python - 从 IronPython 使用 NumPy 和 SciPy 的 final方法

python - scipy.interpolate.Rbf() 的插值不准确

python - 在 Python 中模拟自定义异常

python - 导入错误 : No module named clr when using CPython of python. 组织

python - 使用像素数组设置 opencv 图像/numpy 数组值

python - numpy.random.seed(0) 做什么?

excel - 在 Excel 中插入数据点

javascript - 如何在 VBA 中编写与 "Application.Match"等效的 JavaScript 代码? - - 用于数值插值函数