閾值(單色)效果

需要:

  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)
}