0

我正在尝试在 python 中实现 floyd-steinberg 抖动算法,但我的最终输出有问题,我无法找出问题所在,因为它基于维基百科上的伪代码。

输入

输出

查找最接近值的函数:

def closest(val): 
    legit = np.array([0, 255])
    vals = np.array([val for i in range(len(legit))])
    diffs = np.abs(vals - legit)
    return legit[np.argmin(diffs)]

这是弗洛伊德斯坦伯格的实现:

def floyd(img): 
    img = img.copy()
    height, width = img.shape
    for i in range(height):
        for j in range(width): 
            oldpixel = img[i, j]
            newpixel = closest(oldpixel)
            img[i, j] = newpixel
            quant_error = oldpixel - newpixel

            neighbors_and_weights = [
                (i, j+1, 7.0/16), # right
                (i+1, j-1, 3.0/16), # bottom left
                (i+1, j, 5.0/16), # bottom
                (i+1, j+1, 1.0/16) # bottom right
            ]

            for nw in neighbors_and_weights: 
                x, y, w = nw
                if (0 <= x < height) and (0 <= y < width):
                    img[x][y] += quant_error * w
    return img
4

0 回答 0