阈值(单色)效果

需要:

  1. 理解位图和位图数据

什么是门槛

此调整采用图像中的所有像素并将其推送到纯白色或纯黑色

我们要做什么

这是此示例的 实时演示 ,其中包含一些其他更改,例如使用 UI 在运行时更改阈值级别。

StackOverflow 文档

**** 来自 as3 官方文档的 操作脚本 3 中的阈值

根据指定的阈值测试图像中的像素值,并将通过测试的像素设置为新的颜色值。使用 threshold() 方法,你可以隔离和替换图像中的颜色范围,并对图像像素执行其他逻辑操作。

threshold() 方法的测试逻辑如下:

  1. 如果((pixelValue&mask)操作(阈值和掩码)),则将像素设置为颜色;
  2. 否则,如果 copySource == true,则将像素设置为 sourceBitmap 中对应的像素值。

我刚刚评论了以下代码,其名称与引用说明完全相同。

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.geom.Point;

var bmd:BitmapData = new wildcat(); // instantied a bitmapdata from library a wildcat
var bmp:Bitmap = new Bitmap(bmd); // our display object to previewing bitmapdata on stage
addChild(bmp);
monochrome(bmd); // invoking threshold function

/**
    @param bmd, input bitmapData that should be monochromed
*/
function monochrome(bmd:BitmapData):void {
    var bmd_copy:BitmapData = bmd.clone(); // holding a pure copy of bitmapdata for comparation steps
    // this is our "threshold" in description above, source pixels will be compared with this value
    var level:uint = 0xFFAAAAAA; // #AARRGGBB. in this case i used RGB(170,170,170) with an alpha of 1. its not median but standard
    // A rectangle that defines the area of the source image to use as input.
    var rect:Rectangle = new Rectangle(0,0,bmd.width,bmd.height);
    // The point within the destination image (the current BitmapData instance) that corresponds to the upper-left corner of the source rectangle.
    var dest:Point = new Point();
    // thresholding will be done in two section
    // the last argument is "mask", which exists in both sides of comparation
    // first, modifying pixels which passed comparation and setting them all with "color" white (0xFFFFFFFF)
    bmd.bitmapData.threshold(bmd_copy, rect, dest, ">", level, 0xFFFFFFFF, 0xFFFFFFFF);
    // then, remaining pixels and make them all with "color" black (0xFF000000)
    bmd.bitmapData.threshold(bmd_copy, rect, dest, "<=", level, 0xFF000000, 0xFFFFFFFF);
    // Note: as we have no alpha channel in our default BitmapData (pixelValue), we left it to its full value, a white mask (0xffffffff)
}