我正在尝试使用动作脚本在我的Flex 3应用程序中编写一些内容,该动作脚本会拍摄图像,当用户点击按钮时,它会剥离所有白色(ish)像素并将它们转换为透明,我说白色( ish)因为我试过了白色,但是边缘周围有很多文物。我使用以下代码有点接近:

targetBitmapData.threshold(sourceBitmapData, sourceBitmapData.rect, new Point(0,0), ">=", 0xFFf7f0f2, 0x00FFFFFF, 0xFFFFFFFF, true);

然而,它也会使红色或黄色消失。它为什么这样做?我不确定如何使这项工作。还有其他功能更适合我的需求吗?

有帮助吗?

解决方案

一位朋友和我试图在一段时间内为一个项目做这件事,并发现编写一个在ActionScript中执行此操作的内联方法非常慢。您必须扫描每个像素并对其进行计算,但使用PixelBender进行计算证明是快速的(如果您可以使用Flash 10,否则您会遇到慢速AS)。

像素弯曲代码如下所示:

input image4 src;
output float4 dst;

// How close of a match you want
parameter float threshold
<
  minValue:     0.0;
  maxValue:     1.0;
  defaultValue: 0.4;
>;

// Color you are matching against.
parameter float3 color
<
  defaultValue: float3(1.0, 1.0, 1.0);
>;

void evaluatePixel()
{
  float4 current = sampleNearest(src, outCoord());
  dst = float4((distance(current.rgb, color) < threshold) ? 0.0 : current);
}

如果您需要在AS中执行此操作,可以使用以下内容:

function threshold(source:BitmapData, dest:BitmapData, color:uint, threshold:Number) {
  dest.lock();

  var x:uint, y:uint;
  for (y = 0; y < source.height; y++) {
    for (x = 0; x < source.width; x++) {
      var c1:uint = source.getPixel(x, y);
      var c2:uint = color;
      var rx:uint = Math.abs(((c1 & 0xff0000) >> 16) - ((c2 & 0xff0000) >> 16));
      var gx:uint = Math.abs(((c1 & 0xff00) >> 8) - ((c2 & 0xff00) >> 8));
      var bx:uint = Math.abs((c1 & 0xff) - (c2 & 0xff));

      var dist = Math.sqrt(rx*rx + gx*gx + bx*bx);

      if (dist <= threshold)
        dest.setPixel(x, y, 0x00ffffff);
      else
        dest.setPixel(x, y, c1);
    }
  }
  dest.unlock();
}

其他提示

你可以实际做到没有 pixelbender和实时,这要归功于内置的阈值功能

// Creates a new transparent BitmapData (in case the source is opaque)
var dest:BitmapData = new BitmapData(source.width,source.height,true,0x00000000);

// Copies the source pixels onto it
dest.draw(source);

// Replaces all the pixels greater than 0xf1f1f1 by transparent pixels
dest.threshold(source, source.rect, new Point(), ">", 0xfff1f1f1,0x00000000);

// And here you go ...  
addChild(new Bitmap(dest));     

看起来上面的代码会使一系列颜色透明。

伪代码:结果     <!>对于targetBitmapData中的每个像素,<!> <!> <!> nbsp; <!>         <!> <!> nbsp; <!> <!> <!> <!> <!> <!> <!> <!> <!>如果像素的颜色为<!> gt; = #FFF7F0F2结果             NBSP <!>; NBSP <!>; NBSP <!>; NBSP <!>; NBSP <!>; NBSP <!>; NBSP <!>; NBSP <!>; NBSP <!>; NBSP <!>; <!> nbsp; <!>将颜色更改为#00FFFFFF

这样的事情永远不会是完美的,因为你会失去任何浅色 我会找到一个在线颜色选择器,你可以用它来确切地看到将改变哪些颜色

像素弯曲代码中的答案:

dst = float4((distance(current.rgb,color)<!> lt; threshold)?0.0:current);

应该是:

dst =(距离(current.rgb,颜色)<!> lt; threshold)? float4(0.0):current;

if(distance(current.rgb,color)<!> lt; threshold)     dst = float4(0.0); 其他     dst = float4(当前);

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top