Java Dithering无法正常工作

时间:2015-05-12 14:35:57

标签: java math dithering

我试图在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

请注意&#34;抖动&#34;图像过度抖动,几乎是黑色。

2 个答案:

答案 0 :(得分:0)

您正在进行整数除法:7/163/165/161/16 ......

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

请确保使用浮点数,例如:7.0 / 163.0 / 16等。

答案 1 :(得分:0)

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

在您的每个代码if阻止中,您只能呼叫getRed()而永远不会getGreen90getBlue()。请参见下面的块:(重新格式化以使问题更容易发现。

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光谱的红色部分。