OpenCV thoughts and Flash BitmapData fun

In: Actionscript|Experiments|Flash|Processing|Source Code

11 Apr 2010

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.

//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();
var video = new Video(camera.width, camera.height);
//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);
//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 {
		if (lineCount==stage.stageHeight) {
		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);


3 Responses to OpenCV thoughts and Flash BitmapData fun



April 12th, 2010 at 2:50 am

This is great stuff. Nice work.



April 12th, 2010 at 11:33 am

Thanks Zevan. ActionSnippet has been a constant inspiration for me.



May 23rd, 2011 at 3:32 am


Comment Form

About this blog

This is the blog of Lawrie Cape, an interactive developer from Leeds, England.


  • Flash indie game interweb-mash-up : May 19, 2009 « [...] blog is new to this mash-up, but has a lot to offer - lots of free code and nice unique tutori [...]
  • Joe: But the problem of your flash midi server is: Don't receive other than your sendNote and sendCC or [...]
  • kristin collins: I'm addicted to Blockade!!! Great Game thanks! [...]
  • Vincent: It's been a long time since this got posted, but I just stumbled upon it and couldn't help but notic [...]
  • Lawrie: Para eliminar elementos en blanco, tendrá que utilizar la función superior (para asegurarse de que [...]