【问题标题】:Resize 1-channel numpy (image) array with nearest neighbour interpolation使用最近邻插值调整 1 通道 numpy(图像)数组的大小
【发布时间】:2021-12-12 03:18:30
【问题描述】:

我得到了一个形状为 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)

有没有更简单的方法可以在 numpy 中实现我想要的? (不使用cv2.resizescipy.interpolatePIL

【问题讨论】:

  • 请问“感觉不对”是什么意思?调整它的大小似乎是一种非常明智且简单的方法。
  • 感觉过于复杂了,必须导入 PIL 并创建一个新的 Image 对象来调整大小。

标签: python numpy scipy python-imaging-library nearest-neighbor


【解决方案1】:

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

一种简单的方法是引入一个坐标系,其中整数位置指向输入像素的中心。这意味着图像实际上在每个轴上从 -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]

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

您可以简化代码并将其打包以备将来重用:

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])]

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2020-05-20
    • 2014-11-15
    • 2018-12-04
    • 2017-02-01
    • 1970-01-01
    • 2021-04-21
    • 2018-10-12
    相关资源
    最近更新 更多