使用最近邻插值调整 1 通道 numpy(图像)数组的大小

koh*_*kob 2 python numpy nearest-neighbor scipy python-imaging-library

我得到了一个形状为 388x388x1 的 1 通道 numpy 数组,其值范围为 1-150,作为 ML 推理的输出。我需要使用最近邻插值将数组大小调整为 5000x4000x1。

目前我正在使用 PIL 调整大小。它可以工作,但为此必须导入 PIL 感觉过于复杂。

output = np.random.randint(150, size=(388, 388))
width, height = 4000, 5000

pilImage = Image.fromarray(pred.astype(np.uint8))
pilImageResized = pilImage.resize((width, height), Image.NEAREST)

resizedOutput = np.array(pilImageResized).astype(np.uint8)
Run Code Online (Sandbox Code Playgroud)

在 numpy 中是否有更简单的方法可以实现我想要的目标?(不使用cv2.resize,scipy.interpolatePIL

Mad*_*ist 6

您可以通过构建将每个输出位置映射到输入中的源的索引数组来非常简单地进行神经网络插值。你必须定义/假设一些事情才能有意义地做到这一点。例如,我假设您希望将每个输出行的左边缘与输入行的左边缘匹配,将像素视为表面元素,而不是点源。在后一种情况下,我会将中心匹配起来,导致边缘区域看起来稍微被截断。

一种简单的方法是引入一个坐标系,其中整数位置指向输入像素的中心。这意味着图像实际上在每个轴上从 -0.5px 到 (N - 0.5)px。这也意味着舍入输出像素的中心会自动将它们映射到最近的输入像素:

在此输入图像描述

这将为每个输入像素在输出中提供近似相等的表示,直至舍入:

in_img = np.random.randint(150, size=(388, 388, 1), dtype=np.uint8) + 1
in_height, in_width, *_ = in_img.shape

out_width, out_height = 4000, 5000

ratio_width = in_width / out_width
ratio_height = in_height / out_height

rows = np.round(np.linspace(0.5 * (ratio_height - 1), in_height - 0.5 * (ratio_height + 1), num=out_height)).astype(int)[:, None]
cols = np.round(np.linspace(0.5 * (ratio_width - 1), in_width - 0.5 * (ratio_width + 1), num=out_width)).astype(int)

out_img = in_img[rows, cols]
Run Code Online (Sandbox Code Playgroud)

就是这样。不需要复杂的函数,并且输出保证保留输入的类型,因为它只是一个奇特的索引操作。

您可以简化代码并将其包装起来以供将来重用:

def nn_resample(img, shape):
    def per_axis(in_sz, out_sz):
        ratio = 0.5 * in_sz / out_sz
        return np.round(np.linspace(ratio - 0.5, in_sz - ratio - 0.5, num=out_sz)).astype(int)

    return img[per_axis(img.shape[0], shape[0])[:, None],
               per_axis(img.shape[1], shape[1])]
Run Code Online (Sandbox Code Playgroud)