// forked from makc3d's ASSurf test
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.media.*;
import ru.inspirit.surf.ASSURF;
import ru.inspirit.surf.ReferenceInfo;
import ru.inspirit.surf.image.AutoImageProcessor;
/**
* ASSurf test (based on ASSurf Homography example).
* Show something to camera and click to start tracking.
*/
[SWF(width=465,height=465,frameRate=25)]
public class Homography extends Sprite {
public var video:Video;
public var outline:Sprite;
public function Homography () {
// set up video
addChild (video = new Video);
var cam:Camera = Camera.getCamera ();
cam.setMode (320, 240, 25); video.attachCamera (cam);
// set up canvas
addChild (outline = new Sprite);
outline.scrollRect = new Rectangle (0, 0, 320, 240);
// mark target zone
outline.graphics.lineStyle (2, 0xFF00);
outline.graphics.drawRect (320 / 4, 240 / 4, 320 / 2, 240 / 2);
// wait for click
stage.addEventListener (MouseEvent.CLICK, startTracking);
}
public var ref_bd:BitmapData, ref_id:int;
public var cam_bd:BitmapData;
public var surf:ASSURF;
public function startTracking (e:MouseEvent):void {
stage.removeEventListener (MouseEvent.CLICK, startTracking);
// create reference image
ref_bd = new BitmapData (160, 120);
ref_bd.draw (video, new Matrix (1, 0, 0, 1, -320 / 4, -240 / 4));
// create bitmap for video copy
cam_bd = new BitmapData (320, 240);
// init ASSurf stuff
surf = new ASSURF;
surf.init (ASSURF.DETECT_PRECISION_LOW);
surf.autoDetectROI = true;
ref_id = surf.addRefObject (ref_bd);
surf.buildRefIndex ();
surf.imageProcessor = new AutoImageProcessor (cam_bd.rect);
surf.setup (320, 240);
// hook main loop
addEventListener (Event.ENTER_FRAME, loop);
}
public function loop (e:Event):void {
cam_bd.draw (video);
var ref:ReferenceInfo = surf.detectSingleObject (cam_bd, ref_id, ASSURF.GET_HOMOGRAPHY);
var gfx:Graphics = outline.graphics;
gfx.clear (); gfx.lineStyle (2, 0xFF00);
if (ref.matchedPointsCount > 4) {
var pt0:Point = ref.homography.projectPoint (new Point (0, 0));
var pt1:Point = ref.homography.projectPoint (new Point (ref_bd.width, 0));
var pt2:Point = ref.homography.projectPoint (new Point (ref_bd.width, ref_bd.height));
var pt3:Point = ref.homography.projectPoint (new Point (0, ref_bd.height));
gfx.moveTo(pt0.x, pt0.y);
gfx.lineTo(pt1.x, pt1.y);
gfx.lineTo(pt2.x, pt2.y);
gfx.lineTo(pt3.x, pt3.y);
gfx.lineTo(pt0.x, pt0.y);
}
}
}
}