Floyd-Steinberg 抖动算法出界

问题描述 投票:0回答:1

我对 Python 中的 Floyd-Steinberg 抖动算法有疑问。它的宽度不断超出范围。我检查了很多来源,但我不知道错误在哪里......这是我的代码:

def floydSteinberg(src, colorPalette):
src = src.copy()
h,w = src.shape[:2]
for y in range(h):
    for x in range(w):
        oldpixel = src[x][y]
        newpixel = colorFit(oldpixel, colorPalette)
        src[x][y] = newpixel
        quant_error = oldpixel - newpixel
        if (x<w-1):
            src[x+1][y] = src[x+1][y] + quant_error * 7/16
        if (x>0) and (y<h-1):
            src[x-1][y+1] = src[x-1][y+1] + quant_error * 3/16
        if (y<h-1):
            src[x][y+1] = src[x][y+1] + quant_error * 5/16
        if (x<w-1) and (y<h-1):
            src[x+1][y+1] = src[x+1][y+1] + quant_error * 1/16     
return src

错误:

第 33 行,弗洛伊德斯坦伯格
src[x][y+1] = src[x][y+1] + Quant_error * 5/16 IndexError:索引 180 超出尺寸 180 的轴 0 的范围

测试图像宽180px。 我还尝试在 for 循环中从 w 或 h 中减去 1,但它根本没有帮助(只是为了看看这是否会起作用)

python numpy image-processing dithering
1个回答
0
投票

你的高度和宽度似乎互换了。尝试改变:

h,w = src.shape[:2]

至:

w, h = src.shape[:2]

它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.