We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
OpenCV face tracking as input (Read 654 times)
OpenCV face tracking as input
Jan 29th, 2010, 3:56am
 
Hi there,

fairly new to Processing and programming, newbie question ahead:

I am trying to make a little sketch using openCV that does the following:
loads an image to memory
looks through all the pixels
based on the position of each face detected by openCV,
it draws the image from the picture, in a circle with a radius of brightness depending on the distance between the pixel and the center of the rectangle of the face.

My problem is: the code I use feels very slow and non-optimal. I am using algorithms learnt from Shiffman's book, as well as from the openCV examples. I guess a more experienced programmer can help me either optimizing or telling me that this is impossible, and I'd rather do something else :)

Anyhow, here's the code, and thanks in advance!

Code:
import hypermedia.video.*;
OpenCV opencv;
PImage img;
int centerX, centerY, lenX, lenY;

void setup() {
 img = loadImage( "night.jpeg" );
 size(img.width, img.height);
 opencv = new OpenCV(this);
 opencv.capture(width, height);
 opencv.cascade(OpenCV.CASCADE_FRONTALFACE_ALT);
}

void draw()
{
 background(0);

 opencv.read();
 Rectangle [] faces = opencv.detect();

 loadPixels();
 img.loadPixels();


 for(int i = 0; i < faces.length; i++)
 {
   lenX = faces[i].width/2;
   lenY = faces[i].height/2;
   centerX = faces[i].x + lenX;
   centerY = faces[i].y + lenY;
   for (int x = 0; x < img.width; x++ ) {
     for (int y = 0; y < img.height; y++ ) {

       //pixel location
       int loc = x + y*img.width;

       // getting RGB
       float r = red (img.pixels[loc]);
       float g = green (img.pixels[loc]);
       float b = blue (img.pixels[loc]);

       // lintern effect
       float distance = dist(x,y, centerX,centerY);

       float adjustBrightness = (100-distance)/100;
       r *= adjustBrightness;
       g *= adjustBrightness;
       b *= adjustBrightness;

       // Constrain RGB
       r = constrain(r,0,255);
       g = constrain(g,0,255);
       b = constrain(b,0,255);

       // new color, pixel displayed
       color c = color(r,g,b);
       pixels[loc] = c;

     }

   }
   updatePixels();  
 }
}
Page Index Toggle Pages: 1