【问题标题】:Python distort image with repeating edges using numpy.roll()Python 使用 numpy.roll() 扭曲具有重复边缘的图像
【发布时间】:2021-05-17 14:52:26
【问题描述】:

我有一个带有 (w,h,3) 的 numpy 数组的图像,我想使用一些使用叠加正弦波生成的值沿 y 轴扭曲它。

以下是该方法的代码和 3 张图像(输入、当前输出、所需输出)。所需的输出是使用 Photoshop Wave Distort 过滤器完成的,该过滤器具有重复边缘像素的选项。 基本上我想实现该选项以重复边缘像素而不是环绕像素,以便图像看起来更像一条路径。

另一件事是我不认为 np.roll() 是这项工作的正确方法,但我对任何与 numpy 相关的事情都是个菜鸟。

def distortH(data, distortions, w, h):
    imgdata = np.zeros((h, w, 3), dtype=np.uint8)
    for y in range(h):
        imgdata[y] = np.roll(data[y], distortions[y], axis=0)
    return imgdata
  • 数据:带有 w,h,3 的 numpy 数组

  • distortions: 长度为 h 且带有失真的数组 -500 和 500 之间的值(在这种情况下;失真值将 根据图像大小有不同的缩放比例)

  • w: 图片的宽度

  • h: 图片的高度

提前致谢!

输入:

电流输出

所需的输出

【问题讨论】:

    标签: python arrays python-3.x image numpy


    【解决方案1】:

    解决了!

    numpy.roll() 确实不是正确的方法。

    使用 numpy.pad() 解决了它

    def distortH(data, distortions, w, h):
        imgdata = np.zeros((h, w, 3), dtype=np.uint8)
        for y in range(h):
            if distortions[y] == 0:
                imgdata[y] = data[y]
            elif distortions[y] > 0:
                ndata = np.pad(data[y], ((distortions[y], 0),(0,0)), mode='edge')
                imgdata[y] = ndata[:len(ndata)-distortions[y], :]
            else:
                ndata = np.pad(data[y], ((0, -distortions[y]),(0,0)), mode='edge')
                imgdata[y] = ndata[-distortions[y]:, :]
        return imgdata
    

    可能不是解决问题的最佳方法。但这比手动遍历所有行并逐个交换像素要快得多。

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 2013-11-08
      相关资源
      最近更新 更多