Monday, August 31, 2009

First run of Processing, project "Cube-cam"


Processing is a programming framework/language for whom like to program images, videos and human interactions. Many artists used the Processing to make their interactive arts. It is designed to be easy to use and powerful.

The best way to learn programming is by writing programs. I started the project "cube-cam". It captures images from the web camera and converts each pixel to a rotating cube. The cube is in a 3D space which rotating itself.

Some highlights of my program:

1) Capture is done by using Opencv library, you can do it with the Processing bundled video library. I prefer to use opencv because it can do more image processing stuffs. To install opencv for Processing, please check out the opencv port link.

2) Grabbing image pixels and process one by one.

3) Construct 3D cubes.

4) Used Moviemaker library to record the demonstration video, it is very handy to show your works to your friend.


import processing.opengl.*;
import hypermedia.video.*; // Imports the OpenCV library
import processing.video.*;
MovieMaker mm; // Declare MovieMaker object
float a;

OpenCV opencv;

PImage myimage;
boolean gray=false;


void setup() {
size(800, 600, P3D);

lights();

opencv = new OpenCV( this );

opencv.capture( 80, 60 );
frameRate(30);
mm = new MovieMaker(this, width, height, "demo.mov",
30, MovieMaker.ANIMATION, MovieMaker.LOW);

}

void draw() {

opencv.read();

background(0);

a+=0.5;
if(a > TWO_PI) {
a = 0.0;
}

if (gray)
opencv.convert(OpenCV.GRAY); // Converts to greyscale

myimage = opencv.image();
myimage.loadPixels();
for (int j = 0; j < 60; j++) {
for (int i = 0; i < 80; i++) { // For each pixel in the video frame...
color c = myimage.pixels[i+j*80];


pushMatrix();
translate(i*10,j*10);
fill(c);
rotateX(a);
rotateY(a);
box(5);
popMatrix();

}
}
mm.addFrame(); // Add window's pixels to movie
}

void mouseClicked() {
gray =!gray;
}

void keyPressed() {
if (key == ' ') {
mm.finish(); // Finish the movie if space bar is pressed!
}
}

No comments: