0

我正在尝试在 java 中实现 Floyd-Steinberg 算法,但我仍然有一些错误?!在我无法解决的代码中。也许你们中的一些人建议我如何解决这个问题。

这是方法

 public DitheringGUI dith(String fileName) {
DitheringGUI g = new DitheringGUI(fileName);

for(int y = 0 ; y<g.img.getHeight();y++){
    for(int x = 0 ; x < g.img.getWidth();x++){

    Color oldColor = g.img.getColor(x, y);
    Color newColor = palette(oldColor);

    g.img.set(x, y, newColor);



    int quant_Error = oldColor.getRed() - newColor.getRed();


    if(x+1 < g.img.getWidth()) {
        g.img.set(x+1,y,new Color(  g.img.getColor(x+1, y).getRed()+quant_Error*(7/16),
            g.img.getColor(x+1, y).getRed()+quant_Error*(7/16),
            g.img.getColor(x+1, y).getRed()+quant_Error*(7/16)));
    }


    if(x-1 >=0 && y+1 <g.img.getHeight()){
        g.img.set(x-1,y+1,new Color(    g.img.getColor(x-1, y+1).getRed()+quant_Error*(3/16),
            g.img.getColor(x-1, y+1).getRed()+quant_Error*(3/16),
            g.img.getColor(x-1, y+1).getRed()+quant_Error*(3/16)));
    }

    if(y+1 < g.img.getHeight()){
        g.img.set(x,y+1,new Color(  g.img.getColor(x, y+1).getRed()+quant_Error*(5/16),
            g.img.getColor(x, y+1).getRed()+quant_Error*(5/16),
            g.img.getColor(x, y+1).getRed()+quant_Error*(5/16)));

    }



    if(x+1 < g.img.getWidth() && y+1 < g.img.getHeight()){
        g.img.set(x+1,y+1,new Color(    g.img.getColor(x+1, y+1).getRed()+quant_Error*(1/16),
            g.img.getColor(x+1, y+1).getRed()+quant_Error*(1/16),
            g.img.getColor(x+1, y+1).getRed()+quant_Error*(1/16)));
    }

    }
}
return g;
}

实际问题是它只是黑白而不抖动,这意味着没有灰度。

我的输入是这样的:http ://www.directupload.net/file/d/3985/spd2k9wq.png

我的输出是:http ://www.directupload.net/file/d/3985/s24rq7qo.png

请注意,“抖动”图像​​过度抖动并且几乎是黑色的。

4

2 回答 2

0

你正在做整数除法:7/16, 3/16, 5/16, 1/16...

这些划分的结果是0而不是一些分数。

确保您使用浮点数,例如:7.0 / 163.0 / 16

于 2015-05-12T14:46:54.887 回答
0

我想我看到了错误,它可能是剪切和粘贴的种类:

在每个代码if块中,您只调用过getRed()并且从不调用getGreen90or getBlue()。请在下面的块中查看此内容:(重新格式化以使问题更容易发现。

if(x+1 < g.img.getWidth()) {
    g.img.set(x+1,y,new Color(  
        g.img.getColor(x+1, y).getRed()+quant_Error*(7/16),
        g.img.getColor(x+1, y).getRed()+quant_Error*(7/16),
        g.img.getColor(x+1, y).getRed()+quant_Error*(7/16)));
}                              /\/\/\/\
                                 here   

这个和你的其他块不应该是这样的:

if(x+1 < g.img.getWidth()) {
    g.img.set(x+1,y,new Color(  
        g.img.getColor(x+1, y).getRed()+quant_Error*(7/16),
        g.img.getColor(x+1, y).getGreen()+quant_Error*(7/16),
        g.img.getColor(x+1, y).getBlue()+quant_Error*(7/16)));
} 

我猜你已经非常接近了,你得到的图像看起来像它的原因是因为只有 RGB 光谱的红色部分正在被评估。

于 2015-05-12T15:50:11.927 回答