Browse the notebook

Category: Video

Frame Differencing

[SWF(width = 800, height= 600)]
var sw:Number = 800;
var sh:Number = 600;
var pixelNum:int = sw * sh;
var blurAmount:Number = 10;
var pnt:Point = new Point(0,0);
var rect:Rectangle = new Rectangle(0,0,sw,sh);

var canvas:BitmapData = new BitmapData(sw, sh, false, 0x000000);
var buffer:BitmapData = new BitmapData(sw, sh, false, 0x000000);
var feed  :BitmapData = new BitmapData(sw, sh, false, 0x000000);
var prev  :BitmapData = new BitmapData(sw, sh, false, 0x000000);

var frame:Bitmap = new Bitmap(canvas, "auto", true);
addChild(frame);

var cam:Camera =  Camera.getCamera();
cam.setMode(sw,sh,12);
var video:Video = new Video(sw, sh);
video.attachCamera(cam);

cam.addEventListener(ActivityEvent.ACTIVITY, onActivityStart);

function onActivityStart(evt:ActivityEvent):void {
	addEventListener(Event.ENTER_FRAME, onRun);
	cam.removeEventListener(ActivityEvent.ACTIVITY, onActivityStart);
}

function onRun(evt:Event):void{
	buffer.draw(video);
	feed.copyPixels(buffer, rect, pnt);
	buffer.draw(prev, null, null, BlendMode.DIFFERENCE);
	prev.draw(video);
	canvas.copyPixels(buffer, rect, pnt);
}

This snippet shows a simple method to do frame differencing with a web cam. This is useful for detecting which areas of the screen have change from frame to frame. This post assumes you pretty much know what frame differencing is.


You can view an swf file here:

This is one of those things I do a good deal but never wrapped up into a library… it’s easy (for me at least) to forget exactly how to set it up from scratch.

I added an extra buffer because it’s pretty common that you’ll want to other things aside from just frame differencing alone, so it’s nice to have it all wrapped up in a buffer BitmapData that you can use elsewhere. If speed is a real concern, you can do away with the buffer and just use the canvas instead – depending on what kind of analysis your doing on the frame differenced image it may be trickier without the buffer.

Read snippet: Frame Differencing

Spring PerspectiveProjection

var pointNum:int = 3000;
x = stage.stageWidth / 2;
y = stage.stageHeight / 2;

var verts:Vector.<Number>  = new Vector.<Number>();
var tVerts:Vector.<Number> = new Vector.<Number>();
var pVerts:Vector.<Number> = new Vector.<Number>();
var uv:Vector.<Number> = new Vector.<Number>();
var cmds:Vector.<int> = new Vector.<int>();

var index:int = 0;
var half:Number = pointNum / 20000;
for (var i:int = 0; i<pointNum; i+=3){
	verts[i] = 0.08 * Math.cos(i * Math.PI / 180);
	verts[i+1] = 0.08 * Math.sin(i * Math.PI / 180);
	verts[i+2] =  i / 10000 - half;
	cmds[index++] = 2;
}
cmds[0] = 1;

var proj:PerspectiveProjection = new PerspectiveProjection();
proj.fieldOfView = 45;
var projMat:Matrix3D = proj.toMatrix3D();
var m:Matrix3D = new Matrix3D();
var dx:Number = 0, dy:Number = 0;
addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void {
       m.identity();
	   dx += (mouseX - dx) / 4;
	   dy += (mouseY - dy) / 4;
	   m.appendRotation(dy,Vector3D.X_AXIS);
	   m.appendRotation(dx,Vector3D.Y_AXIS);
	   m.appendTranslation(0,0,0.5);
	   m.transformVectors(verts, tVerts);
	   Utils3D.projectVectors(projMat, tVerts, pVerts, uv);
	   graphics.clear();
	   graphics.lineStyle(3,0x000000);
	   graphics.drawPath(cmds, pVerts);
}

This snippet draws a spring shape in 3D with perspective. This is the first snippet where I’ve made use of the PerspectiveProjection class. So if your wondering how to add perspective to your Utils3D.projectVectors code… this is one way to do it…


Have a look at the swf…

Read snippet: Spring PerspectiveProjection

Web Cam

var cam:Camera =  Camera.getCamera();
var video:Video = new Video(320, 240);
video.attachCamera(cam);
addChild(video);

Adds the feed from your webcam to the stage.

Read snippet: Web Cam