Category Archives: Video

Frame Differencing

Actionscript:
  1. [SWF(width = 800, height= 600)]
  2. var sw:Number = 800;
  3. var sh:Number = 600;
  4. var pixelNum:int = sw * sh;
  5. var blurAmount:Number = 10;
  6. var pnt:Point = new Point(0,0);
  7. var rect:Rectangle = new Rectangle(0,0,sw,sh);
  8.  
  9. var canvas:BitmapData = new BitmapData(sw, sh, false, 0x000000);
  10. var buffer:BitmapData = new BitmapData(sw, sh, false, 0x000000);
  11. var feed  :BitmapData = new BitmapData(sw, sh, false, 0x000000);
  12. var prev  :BitmapData = new BitmapData(sw, sh, false, 0x000000);
  13.  
  14. var frame:Bitmap = new Bitmap(canvas, "auto", true);
  15. addChild(frame);
  16.  
  17. var cam:Camera =  Camera.getCamera();
  18. cam.setMode(sw,sh,12);
  19. var video:Video = new Video(sw, sh);
  20. video.attachCamera(cam);
  21.  
  22. cam.addEventListener(ActivityEvent.ACTIVITY, onActivityStart);
  23.  
  24. function onActivityStart(evt:ActivityEvent):void {
  25.     addEventListener(Event.ENTER_FRAME, onRun);
  26.     cam.removeEventListener(ActivityEvent.ACTIVITY, onActivityStart);
  27. }
  28.  
  29. function onRun(evt:Event):void{
  30.     buffer.draw(video);
  31.     feed.copyPixels(buffer, rect, pnt);
  32.     buffer.draw(prev, null, null, BlendMode.DIFFERENCE);
  33.     prev.draw(video);
  34.     canvas.copyPixels(buffer, rect, pnt);
  35. }

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.

Also posted in BitmapData, graphics algorithms, pixel manipulation | Tagged , , | 5 Comments

Spring PerspectiveProjection

Actionscript:
  1. var pointNum:int = 3000;
  2. x = stage.stageWidth / 2;
  3. y = stage.stageHeight / 2;
  4.  
  5. var verts:Vector.<Number>  = new Vector.<Number>();
  6. var tVerts:Vector.<Number> = new Vector.<Number>();
  7. var pVerts:Vector.<Number> = new Vector.<Number>();
  8. var uv:Vector.<Number> = new Vector.<Number>();
  9. var cmds:Vector.<int> = new Vector.<int>();
  10.  
  11. var index:int = 0;
  12. var half:Number = pointNum / 20000;
  13. for (var i:int = 0; i<pointNum; i+=3){
  14.     verts[i] = 0.08 * Math.cos(i * Math.PI / 180);
  15.     verts[i+1] = 0.08 * Math.sin(i * Math.PI / 180);
  16.     verts[i+2] =  i / 10000 - half;
  17.     cmds[index++] = 2;
  18. }
  19. cmds[0] = 1;
  20.  
  21. var proj:PerspectiveProjection = new PerspectiveProjection();
  22. proj.fieldOfView = 45;
  23. var projMat:Matrix3D = proj.toMatrix3D();
  24. var m:Matrix3D = new Matrix3D();
  25. var dx:Number = 0, dy:Number = 0;
  26. addEventListener(Event.ENTER_FRAME, onLoop);
  27. function onLoop(evt:Event):void {
  28.        m.identity();
  29.        dx += (mouseX - dx) / 4;
  30.        dy += (mouseY - dy) / 4;
  31.        m.appendRotation(dy,Vector3D.X_AXIS);
  32.        m.appendRotation(dx,Vector3D.Y_AXIS);
  33.        m.appendTranslation(0,0,0.5);
  34.        m.transformVectors(verts, tVerts);
  35.        Utils3D.projectVectors(projMat, tVerts, pVerts, uv);
  36.        graphics.clear();
  37.        graphics.lineStyle(3,0x000000);
  38.        graphics.drawPath(cmds, pVerts);
  39. }

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...

Also posted in 3D, Graphics, matrix | Tagged , , | 1 Comment

Web Cam

Actionscript:
  1. var cam:Camera =  Camera.getCamera();
  2. var video:Video = new Video(320, 240);
  3. video.attachCamera(cam);
  4. addChild(video);

Adds the feed from your webcam to the stage.

Posted in Video | Tagged , | 1 Comment