/*
mit USE_WEBCAM kann man auswählen ob man nun
das Bild oder die Webcam nutzen möchte. Achtung falls
du mehrere Webcams installiert hast musst du get
getCamera() deine zu nutzende Webcam angeben ;)
*/
package
{
import at.geoathome.cv.blob.ColorBlobDetector;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.Video;
import flash.utils.Timer;
/**
* @author Georg Kamptner
*
* License: Do as you wish, but don`t be evil ;)
*/
public class Main extends Sprite
{
// image
[Embed(source="image.png", mimeType="image/png")]
protected var Img:Class;
protected var _img:Bitmap;
protected var _imgContainer:Sprite;
protected var _graphicsContainer:Sprite;
// video / webcam
protected var _webcam:Camera;
protected var _video:Video;
protected var _bitmapDataFromVideo:BitmapData;
protected var _trackTimer:Timer;
protected var _colorFromVideoStorage:Object;
public static var USE_WEBCAM:Boolean = false;
// color blob detector
protected var _colorBlobDetector:ColorBlobDetector;
private var _roi:Rectangle;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
protected function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
if (!USE_WEBCAM)
{ // image //
// create image
_img = new Img() as Bitmap;
// Since Bitmap is not interactive we need a Sprite
// to handle mouse events.
_imgContainer = new Sprite();
_imgContainer.addChild(_img);
addChild(_imgContainer);
// add mouse events
_imgContainer.addEventListener(MouseEvent.CLICK, detectColorFromImageUnderMouse);
}
else
{ // webcam //
_webcam = Camera.getCamera();
_webcam.setMode(320, 240, 20, false);
_video = new Video();
_video.attachCamera(_webcam);
// Since Video is not interactive we need a Sprite
// to handle mouse events.
_imgContainer = new Sprite();
_imgContainer.addChild(_video);
addChild(_imgContainer);
// add mouse events to select a color
_colorFromVideoStorage = { r:255, g:255, b:255 };
_imgContainer.addEventListener(MouseEvent.CLICK, getColorFromVideo);
_bitmapDataFromVideo = new BitmapData(_video.width, _video.height);
_trackTimer = new Timer(1000 / 20);
_trackTimer.addEventListener(TimerEvent.TIMER, detectColorInVideo);
_trackTimer.start();
}
// draw the found regions into this sprite
_graphicsContainer = new Sprite();
addChild(_graphicsContainer);
// detector (this is it) - debug version needs a sprite to draw into
_colorBlobDetector = new ColorBlobDetector( 2, 2000, 2, 2000);
_colorBlobDetector.setConfig(2.0, 6.0, 9.0);
_colorBlobDetector.setThresholds(8.0, 0.20, 0.0, 0.0);
if (!USE_WEBCAM)
{
_roi = new Rectangle(0, 0, _img.width, _img.height);
}
else
{
_roi = new Rectangle(0, 0, _bitmapDataFromVideo.width, _bitmapDataFromVideo.height);
}
}
private function getColorFromVideo(e:MouseEvent):void
{
// get color unter mousecursor
var rgb:uint = _bitmapDataFromVideo.getPixel(_video.mouseX, _video.mouseY);
_colorFromVideoStorage['r'] = rgb >> 16 & 0xff;
_colorFromVideoStorage['g'] = rgb >> 8 & 0xff;
_colorFromVideoStorage['b'] = rgb & 0xff;
}
private function detectColorInVideo(e:TimerEvent):void
{
// draw video into bitmapdata
_bitmapDataFromVideo.draw(_video);
// detect blobs
_colorBlobDetector.detectRGB(_bitmapDataFromVideo, _colorFromVideoStorage['r'],
_colorFromVideoStorage['g'],
_colorFromVideoStorage['b'], _roi);
// draw regions
_graphicsContainer.graphics.clear();
for (var i:uint = 0; i < _colorBlobDetector.numOfBlobs; ++i)
{
_graphicsContainer.graphics.lineStyle(2, 0xff0000);
_graphicsContainer.graphics.drawRect(_colorBlobDetector.blobs[i].x,
_colorBlobDetector.blobs[i].y,
_colorBlobDetector.blobs[i].width,
_colorBlobDetector.blobs[i].height);
}
// debug trace
trace("Blobs found: " + _colorBlobDetector.numOfBlobs +
", Duration in MS: " + _colorBlobDetector.detectionDuration +
" in FPS: " + int(1000 / _colorBlobDetector.detectionDuration));
}
protected function detectColorFromImageUnderMouse(e:MouseEvent):void
{
// get color under mousecursor
var rgb:uint = _img.bitmapData.getPixel(_img.mouseX, _img.mouseY);
var r:uint = rgb >> 16;
var g:uint = rgb >> 8 & 0xff;
var b:uint = rgb & 0xff;
// detect blobs
_colorBlobDetector.detectRGB(_img.bitmapData, r, g, b, _roi);
// draw regions
_graphicsContainer.graphics.clear();
for (var i:uint = 0; i < _colorBlobDetector.numOfBlobs; ++i)
{
_graphicsContainer.graphics.lineStyle(2, 0xff0000);
_graphicsContainer.graphics.drawRect(_colorBlobDetector.blobs[i].x,
_colorBlobDetector.blobs[i].y,
_colorBlobDetector.blobs[i].width,
_colorBlobDetector.blobs[i].height);
}
// debug trace
trace("Blobs found: " + _colorBlobDetector.numOfBlobs +
", Duration in MS: " + _colorBlobDetector.detectionDuration +
" in FPS: " + int(1000 / _colorBlobDetector.detectionDuration));
}
}
}