In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

マーカー認識のための二値化

/**
 * Copyright keno42 ( http://wonderfl.net/user/keno42 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9b3J
 */

package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.ConvolutionFilter;
	import flash.geom.Point;
	import flash.media.Camera;
	import flash.media.Video;
        import net.hires.debug.*;
	public class Test3 extends Sprite
	{
		private var camera:Camera;
		private var video:Video;
		private var bd:BitmapData;
		private var threshold:uint = 0xFF888888;
		private var grayConst:Array = [
			0.3, 0.59, 0.11, 0, 0,
			0.3, 0.59, 0.11, 0, 0,
			0.3, 0.59, 0.11, 0, 0,
			0, 0, 0, 0, 255
		];
		public function Test3() 
		{
			camera = Camera.getCamera();
			if ( camera == null ) {
			} else {
				start();
			}
		}
		private function start():void {
			camera.setMode(465, 232, 30);
			video = new Video(465, 232);
			video.attachCamera(camera);
			bd = new BitmapData(video.width, video.height);
			this.addChild( video );
			this.addChild( new Bitmap( bd ) );
			this.getChildAt(1).y = 233;
                        this.addChild( new Stats() );
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		private function onEnterFrame(e:Event):void {
			bd.lock();
			bd.draw(video);
			
			// グレー化
			bd.applyFilter(bd, bd.rect, new Point(), new ColorMatrixFilter(grayConst));
			bd.applyFilter(bd, bd.rect, new Point(), new ConvolutionFilter(5, 5, [
				0, -1, -1, -1, 0,
				-1, -1, -2, -1, -1,
				-1, -2, 25, -2, -1,
				-1, -1, -2, -1, -1,
				0, -1, -1, -1, 0
			]));
			bd.applyFilter(bd, bd.rect, new Point(), new BlurFilter(3, 3));
			
			// 二値化
			bd.threshold(bd, bd.rect, new Point(), ">", threshold, 0xFFFFFFFF, 0x0000FF00);
			bd.threshold(bd, bd.rect, new Point(), "!=", 0xFFFFFFFF, 0xFF000000);
			bd.unlock();
		}
	}
}