OpenCV thoughts and Flash BitmapData fun

Following on from my first post about my new generative audio project, I’ve been delving deeper into some of the libraries avaliable for Processing. One that sounded really interesting and powerful was OpenCV (Computer Vision). Peter Kirn over at Create Digtal Motion had this to say about it –

It’s a relatively easy thing for computers to “see” video, but “computer vision” goes a step further, applying a wide range of techniques by which computers can begin to understand and process the content of a video input. These techniques tend toward the primitive, but they can also produce aesthetically beautiful results. The best place to start with computer vision has long been the standard library, OpenCV. A free (as in beer and freedom) library developed by Intel and with ongoing use in a variety of applications, OpenCV is a terrific, C/C++-based tool not just for things like motion tracking, but video processing in general. OpenCV gets a lot of support in the C++-based OpenFrameWorks, but that doesn’t mean Java and Processing have to be left out of the fun.

With loads of ideas for how to implement this into my generative audio project floating around, I started following Andy Best’s great tutorial here.

I had a few issues setting up the library – so let me offer a word of warning – Make sure you download version 1.0 if you want to use OpenCV with Processing, and not the feautred download – which is a later build.

Among the fun things I got running, was this terrifying image difference threshold thing –

It reminded me of a great bit of Code Zevan posted over at ActionSnippet – AS3 Frame Differencing. Which then in turn reminded me of a couple of BitmapData experiments I never put up on here. So here they are!

I’ve wrapped them up into 1 block of code, as they are essentially very similar. The code will produce pictures like those below, by drawing sections of the webcam image into a Bitmap – either in random rectangles, or in sequential lines. These produce different, interesting effects.

?View Code ACTIONSCRIPT3
//Boolean var to draw either random rectangles, or consecutive lines
var drawRandom:Boolean = false;
//Differing framerate for each type.
if (drawRandom) {
	stage.frameRate = 120;
} else {
	stage.frameRate = 50;
}
//Set up the Camera
var camera:Camera = Camera.getCamera();
camera.setMode(550,400,60);
var video = new Video(camera.width, camera.height);
video.attachCamera(camera);
addChild(video);
//Determine the maximum dimensions of the random rectangles
var drawWidth:int  = 25;
var drawHeight:int = 25;
//Boolean var for if app is currently drawing
var drawIt:Boolean = false;
//Counter to make lines sequential.
var lineCount:int = 0;
 
var BMD:BitmapData = new BitmapData(550,400);
var B:Bitmap = new Bitmap(BMD);
addChild(B);
B.x=550;
//Start and pause the drawing on any keypress
stage.addEventListener(KeyboardEvent.KEY_UP, toggleSnap);
 
function snap(e:Event):void {
 
	var r1:int;
	var r2:int;
	var r3:int;
	var r4:int;
 
	if (drawRandom) {
 
		//Start X and Y
		r1 = Math.round(Math.random()*(550-drawWidth));
		r2 = Math.round(Math.random()*(400-drawHeight));
		//Width and height of clip
		r3 = Math.round(Math.random()*drawWidth);
		r4 = Math.round(Math.random()*drawHeight);
 
	} else {
		lineCount+=1;
		if (lineCount==stage.stageHeight) {
			toggleSnap(null);
		}
		r1 = 0;
		r2 = lineCount;
		r3 = 550;
		r4 = 1;
	}
	BMD.draw(video, null, null, null, new Rectangle( r1, r2, r3, r4 ), true );
}
 
function toggleSnap(e:KeyboardEvent):void {
	if (drawIt) {
		drawIt = false;
		removeEventListener(Event.ENTER_FRAME, snap);
	} else {
		drawIt = true;
		addEventListener(Event.ENTER_FRAME, snap);
	}
}

Cheers.

3 thoughts on “OpenCV thoughts and Flash BitmapData fun”

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>