2010.03.27
22:24
author: taikiken
0件のコメント
Flash Builder 4の発売まであと少し。
CS5も4月12日にイベントがあるので発売時期のアナウンスがあるんでしょうねー。
楽しみです。
Googlecode : quasimondolibs
http://code.google.com/p/quasimondolibs/
com.quasimondo.bitmapdata
には ThresholdBitmap
ていう便利そうなクラスがあるけど依存ファイルのひとつが無いのでエラーがでて使えない。
com.quasimondo.filters.AdaptiveThresholdFilter
がそのファイル。
PixelBender ファイルの adaptiveThreshold.ppj を Embed しています。
ハードディスクを探したら見つかったので@quasimondoさんのサイトにでもあったのかもしれない。
フォルダごとパッケージ領域へコピーして使用することにしました。
ThresholdBitmap
は画像を1bit化できます。
1bit化にはColorMatrixFilter, ConvolutionFilter, ColorTransform
や複雑な処理が必要だけど手間いらずなのが助かります。
Radiobutton と Checkbox は @bit101さんのminimalcompsを使っています。
Googlecode : minimalcomps
AdaptiveThresholdFilter, minimalcomps
ともFlex SDKライブラリを必要としています。
minimalcomps
は標準ではFlex SDK 4仕様なのでFlex SDK 3を使っている時は com.bit101.components.Component
のソースコードを修正します。
minimalcomps
は背景色に#ccc選択色に#fffを使っているのでコンポーネントを置いた背景色が黒ぽいと選択したかどうかが分かりにくいのがちょっと残念。
自由に色は変えられなさそう・・・
Threshold Demo

*Webcamが必要です。Flash Player 10が必要です。
import com.quasimondo.bitmapdata.CameraBitmap;
import com.quasimondo.bitmapdata.ThresholdBitmap;
var camw:uint = 320;
var camh:uint = 240;
var camfps:uint = 24;
var vieww:uint = 320;
var viewh:uint = 240;
var camerabd:CameraBitmap;
function intiWidthView ():void {
setUpRadio();
camerabd = new CameraBitmap(vieww, viewh, camfps, camw, camh);
camerabd.addEventListener(Event.RENDER, onRenderCamera);
}
var bitmap:Bitmap = new Bitmap();
bitmap.transform.matrix = new Matrix(-1,0,0,1,vieww,0);
addChild(bitmap);
var invertMatrix:Array = [
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0
];
var filterInvert:ColorMatrixFilter = new ColorMatrixFilter(invertMatrix);
var useAlpha:Boolean = false;
var useInvert:Boolean = false;
var thresholdType:String = "NONE";
var bmdcopy:BitmapData = new BitmapData(vieww, viewh, true, 0x00000000);
function onRenderCamera(e:Event):void {
var cbd:BitmapData = camerabd.bitmapData;
bmdcopy.draw(cbd, new Matrix());
var threshold:BitmapData;
if ( thresholdType != "NONE" ) {
threshold = new ThresholdBitmap(cbd, ThresholdBitmap[thresholdType]) as BitmapData;
}
if (useInvert && threshold) {
threshold.applyFilter(threshold, new Rectangle(0, 0, vieww, viewh), new Point(0, 0), filterInvert);
}
var viewbd:BitmapData;
if (useAlpha) {
if (threshold)
bmdcopy.copyChannel(threshold, threshold.rect, new Point(), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
viewbd = bmdcopy;
} else {
viewbd = threshold ? threshold : bmdcopy;
}
bitmap.bitmapData = viewbd;
}
// =====================================================
// Radiobutton and Checkbox
import com.bit101.components.RadioButton;
import com.bit101.components.CheckBox;
var radioContainer:Sprite = new Sprite();
function setUpRadio ():void {
radioContainer.x = vieww;
addChild(radioContainer);
var labels:Array = [
"none"
,"fixed"
,"entropy"
,"moment"
,"discriminant"
,"otsu"
,"adaptive"
];
var i:int = -1;
var limit:uint = labels.length - 1;
var radio:RadioButton;
i++
radio = new RadioButton( radioContainer, 30, 30 + (i * 20), labels[i], true, onRadioChange );
for ( ;i++ < limit; ) {
radio = new RadioButton( radioContainer, 30, 30 + (i * 20), labels[i], false, onRadioChange );
}
// MASK
new CheckBox(radioContainer, 180, 30, "MASK", onCheckboxMaskChange);
// INVERT
new CheckBox(radioContainer, 180, 60, "INVERT", onCheckboxInvertChange);
}
function onRadioChange (e:MouseEvent):void {
thresholdType = e.currentTarget.label.toUpperCase();
}
function onCheckboxMaskChange (e:MouseEvent):void {
useAlpha = e.currentTarget.selected;
}
function onCheckboxInvertChange (e:MouseEvent):void {
useInvert = e.currentTarget.selected;
}
intiWidthView(); |
import com.quasimondo.bitmapdata.CameraBitmap;
import com.quasimondo.bitmapdata.ThresholdBitmap;
var camw:uint = 320;
var camh:uint = 240;
var camfps:uint = 24;
var vieww:uint = 320;
var viewh:uint = 240;
var camerabd:CameraBitmap;
function intiWidthView ():void {
setUpRadio();
camerabd = new CameraBitmap(vieww, viewh, camfps, camw, camh);
camerabd.addEventListener(Event.RENDER, onRenderCamera);
}
var bitmap:Bitmap = new Bitmap();
bitmap.transform.matrix = new Matrix(-1,0,0,1,vieww,0);
addChild(bitmap);
var invertMatrix:Array = [
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0
];
var filterInvert:ColorMatrixFilter = new ColorMatrixFilter(invertMatrix);
var useAlpha:Boolean = false;
var useInvert:Boolean = false;
var thresholdType:String = "NONE";
var bmdcopy:BitmapData = new BitmapData(vieww, viewh, true, 0x00000000);
function onRenderCamera(e:Event):void {
var cbd:BitmapData = camerabd.bitmapData;
bmdcopy.draw(cbd, new Matrix());
var threshold:BitmapData;
if ( thresholdType != "NONE" ) {
threshold = new ThresholdBitmap(cbd, ThresholdBitmap[thresholdType]) as BitmapData;
}
if (useInvert && threshold) {
threshold.applyFilter(threshold, new Rectangle(0, 0, vieww, viewh), new Point(0, 0), filterInvert);
}
var viewbd:BitmapData;
if (useAlpha) {
if (threshold)
bmdcopy.copyChannel(threshold, threshold.rect, new Point(), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
viewbd = bmdcopy;
} else {
viewbd = threshold ? threshold : bmdcopy;
}
bitmap.bitmapData = viewbd;
}
// =====================================================
// Radiobutton and Checkbox
import com.bit101.components.RadioButton;
import com.bit101.components.CheckBox;
var radioContainer:Sprite = new Sprite();
function setUpRadio ():void {
radioContainer.x = vieww;
addChild(radioContainer);
var labels:Array = [
"none"
,"fixed"
,"entropy"
,"moment"
,"discriminant"
,"otsu"
,"adaptive"
];
var i:int = -1;
var limit:uint = labels.length - 1;
var radio:RadioButton;
i++
radio = new RadioButton( radioContainer, 30, 30 + (i * 20), labels[i], true, onRadioChange );
for ( ;i++ < limit; ) {
radio = new RadioButton( radioContainer, 30, 30 + (i * 20), labels[i], false, onRadioChange );
}
// MASK
new CheckBox(radioContainer, 180, 30, "MASK", onCheckboxMaskChange);
// INVERT
new CheckBox(radioContainer, 180, 60, "INVERT", onCheckboxInvertChange);
}
function onRadioChange (e:MouseEvent):void {
thresholdType = e.currentTarget.label.toUpperCase();
}
function onCheckboxMaskChange (e:MouseEvent):void {
useAlpha = e.currentTarget.selected;
}
function onCheckboxInvertChange (e:MouseEvent):void {
useInvert = e.currentTarget.selected;
}
intiWidthView();
画像の反転(Photoshopの諧調の反転)は ColorMarixFilter
を使います。
var invertMatrix:Array = [
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0
];
var filterInvert:ColorMatrixFilter = new ColorMatrixFilter(invertMatrix); |
var invertMatrix:Array = [
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0
];
var filterInvert:ColorMatrixFilter = new ColorMatrixFilter(invertMatrix);
R, G, B それぞれを-1にするんだね。