【问题标题】:How can Floyd-Steinberg-Dithering work?Floyd-Steinberg-Dithering 如何工作?
【发布时间】:2014-06-01 15:03:37
【问题描述】:

我目前正在尝试用 Java 实现 Floyd-Steinberg-Dithering 算法。经过几次失败的尝试后,我在阅读了Wikipedia 上列出的伪代码后遇到了一个问题。

for each y from top to bottom
for each x from left to right
  oldpixel  := pixel[x][y]
  newpixel  := find_closest_palette_color(oldpixel)
  pixel[x][y]  := newpixel
  quant_error  := oldpixel - newpixel
  pixel[x+1][y  ] := pixel[x+1][y  ] + 7/16 * quant_error
  pixel[x-1][y+1] := pixel[x-1][y+1] + 3/16 * quant_error
  pixel[x  ][y+1] := pixel[x  ][y+1] + 5/16 * quant_error
  pixel[x+1][y+1] := pixel[x+1][y+1] + 1/16 * quant_error

我想要实现的是将图像压缩到 16 色调色板中。然而,在添加错误像素之后,我不是创建了调色板中甚至不存在的全新颜色吗?

但是最终将整个图像强制返回到调色板中就足以使这项工作有效吗?

提前致谢!

【问题讨论】:

    标签: image dithering


    【解决方案1】:

    请注意,量化误差只添加到尚未映射到调色板(量化)的像素!

    这意味着在添加了错误之后,这些像素也会被映射,并且它们的错误会向前传播到其他未处理的像素上。

    算法结束时,每个像素都会被映射,最后剩下的误差会被丢弃。

    因此,在量化操作结束时,调色板之外不应有任何像素。

    【讨论】:

    • 谢谢!这很有意义,并帮助我完美地解决了问题。
    【解决方案2】:

    我使用了修改后的算法:

     for each y from 0 to ImageHeight-1
        for each x from 1 to ImageWidth-1
            oldpixel  := pixel[x][y]
            newpixel  := find_closest_palette_color(oldpixel)
            pixel[x][y]  := newpixel
            quant_error  := oldpixel - newpixel
            pixel[x+1][y  ] := pixel[x+1][y  ] + 7/16 * quant_error
            pixel[x-1][y+1] := pixel[x-1][y+1] + 3/16 * quant_error
            pixel[x  ][y+1] := pixel[x  ][y+1] + 5/16 * quant_error
            pixel[x+1][y+1] := pixel[x+1][y+1] + 1/16 * quant_error
    
    and i have used this 16 colors palette:
                   **B    G    R**
     black:          0,   0,   0
     blue:         127,   0,   0
     green:          0, 127,   0
     cyan:         127, 127,   0
     red:            0,   0, 127
     magenta:      127,   0,   0
     brown:        127,   0, 127
     gray:         191, 191, 191
     dark gray:     63,  63,  63
     light blue:   255,   0,   0
     light green:    0, 255,   0
     light cyan:   255, 255,   0
     light red:      0,   0, 255
     pink:         255,   0, 255
     yellow:         0, 255, 255
     white:        255, 255, 255
    

    这个调色板的结果更好,没有维基百科推荐的蛇形扫描。

    【讨论】:

    • 抱歉我的错误:洋红色:127、0、127 棕色:0、127、127
    猜你喜欢
    • 2021-02-01
    • 2019-09-16
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2021-11-13
    • 2022-01-22
    相关资源
    最近更新 更多