2010-03-31 51 views
1

我正在制作一个小应用程序,其中的孩子可以用颜色填充预设插图。我已经成功地使用洪水填充算法实现了MS-paint风格的油漆桶。但是,在图像元素的边缘附近,像素没有填充,因为这些线是反锯齿的。这是因为当前是否填充的条件是colourAtCurrentPixel == colourToReplace,这对线上的混合像素不起作用。 (颜色是RGB提示)柔软的油漆桶填充:颜色平等

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

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

如何match填写()?

这里,图像(左为的情况下,权所需)

alt text 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); 

回答

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; 
} 
+1

谢谢,我现在做这样的事情。看到我的回答 – 2010-03-31 15:01:59

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) { 
+1

是的,应该完美地工作。 但是请注意,如果您经常打电话(这可能是填充的情况),这不会很好。在浮点数和整数之间来回移动并不是很快,静态函数调用也不是。 – back2dos 2010-03-31 15:27:06