使用弗洛伊德-斯坦伯格抖动不起作用

Using Floyd–Steinberg dithering not working

本文关键字:伯格 抖动 不起作用 弗洛伊德      更新时间:2023-10-16

我正在尝试实现弗洛伊德算法,但似乎不起作用。

我使用了维基百科中的伪代码算法没有弗洛伊德 http://img15.hostingpics.net/pics/298623Capturedcran20140226165024.png

与弗洛伊德·http://img15.hostingpics.net/pics/968250Capturedcran20140226165035.png

这是我的代码:

template<>
Image<ubyte>* Image<ubyte>::floydSteinberg() const
{
    Image<ubyte>* tmp = new Image<ubyte>(this->width, this->height);
    for (int i=0; i < width*height; i++)
            tmp->array[i]= this->array[i];

    for (int y = 0; y< this->height; y++){
        for (int x = 1; x<this->width; x++){
            ubyte oldpixel = tmp->pixel(x, y);
            ubyte newpixel  = (oldpixel > 128) ? 255 : 0;
            tmp->pixel(x,y) = newpixel;
            ubyte propagationErreur  = oldpixel - newpixel;
            tmp->pixel(x+1,y) =tmp->pixel(x+1,y) + 7.0/16 * propagationErreur;
            tmp->pixel(x-1,y+1) = tmp->pixel(x-1,y+1)  + 3.0/16 * propagationErreur ;
            tmp->pixel(x,y+1) = tmp->pixel(x,y+1)  + 5.0/16 * propagationErreur ;
            tmp->pixel(x+1,y+1) = tmp->pixel(x+1,y+1)  + 1.0/16 * propagationErreur ;
        }
    }

    return tmp;
}
ubyte newpixel  = (oldpixel > 128) ? 0 : 255;

必须是

ubyte newpixel  = (oldpixel > 128) ? 255 : 0;

另一个可能的问题:我建议传播Erreur应该是有符号的类型