1

我正在制作一个小应用程序,孩子们可以在其中用颜色填充预设插图。我已经使用洪水填充算法成功地实现了一个 MS-paint 风格的油漆桶。然而,图像元素的边缘附近的像素未被填充,因为线条是抗锯齿的。这是因为当前是否填充的条件是colourAtCurrentPixel == colourToReplace,这对线条处的混合像素不起作用。(颜色是 RGB 单位)

我想在 Photoshop 和其他复杂工具中添加一个平滑/阈值选项,但是确定两种颜色之间相等/距离的算法是什么?

if (match(pixel(x,y), colourToReplace) setpixel(x,y,colourToReplaceWith)

()怎么填match

在这里,一张图片(左边是情况,右边是想要的)

替代文字 http://www.freeimagehosting.net/uploads/6aa7b4ad53.png

这是我当前的完整代码:

            var b:BitmapData = settings.background;
            b.lock();

            var from:uint = b.getPixel(x,y);


            var q:Array = [];


            var xx:int;
            var yy:int;
            var w:int = b.width;
            var h:int = b.height;
            q.push(y*w + x);
            while (q.length != 0) {
                var xy:int = q.shift();
                xx = xy % w;
                yy = (xy - xx) / w;
                if (b.getPixel(xx,yy) == from) { //<- want to replace this line
                    b.setPixel(xx,yy,to);
                    if (xx != 0) q.push(xy-1);
                    if (xx != w-1) q.push(xy+1);
                    if (yy != 0) q.push(xy-w);
                    if (yy != h-1) q.push(xy+w);
                }
            }
            b.unlock(null);
4

2 回答 2

1

好吧,我想最自然的方法是计算颜色之间的差异。为了达到一个合理的值,应该计算每个通道的差异。尚未对其进行测试,但以下应该可以工作:

const perChanThreshold:uint = 5;
const overallThreshold:uint = perChanThreshold * perChanThreshold * 3;
function match(source:uint, target:uint):Boolean {
    var diff:uint = 0, chanDiff:uint;
    for (var i:int = 0; i < 3; i++) {
        chanDiff = (source >> (i * 8)) & 0xFF;
        diff += chanDiff * chanDiff;
    }
    return diff <= overallThreshold;
}
于 2010-03-31T14:15:30.647 回答
1

做了一些有用的东西:

                c = b.getPixel(xx,yy);
                if (c == to) continue;
                if (c != from) d = 
                    Math.pow(f1 - (c & 0xFF), 2) +
                    Math.pow(f2 - (c >> 8 & 0xFF), 2) +
                    Math.pow(f3 - (c >> 16 & 0xFF), 2)
                if (c == from || d < tres) {
于 2010-03-31T15:01:46.800 回答