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.
IndexProgramming Questions & HelpVideo Capture,  Movie Playback,  Vision Libraries › openCV - display an image while analyzing a copy
Page Index Toggle Pages: 1
openCV - display an image while analyzing a copy (Read 3500 times)
openCV - display an image while analyzing a copy
May 23rd, 2009, 6:22am
 
Hello everybody!  I'm stuck with something I thought it had to be very simple.

I wanted to use openCV to detect my face and display the image at the same time. Since face detection gets very slow at 640*480 my idea was to:

-> grab a full res frame from the webcam
-> display it
-> resize it to half the size
-> do the face detection

My last attempt was to grab the video with the standard library and than pass it to openCV. It seems to works, cause i get a smaller image on which the opecv.contrast and opencv.brightness functions work. It's just the detect one that does not, damn it!

Any help is greatly appreciated!

import processing.video.*;
import hypermedia.video.*;

Capture cam;
OpenCV opencv;

// contrast/brightness values
int contrast_value    = 0;
int brightness_value  = 0;

void setup() {
 size( 800, 600 );
cam = new Capture(this, 640, 480);

 opencv = new OpenCV( this );

opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );  // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"


 // print usage
 println( "Drag mouse on X-axis inside this sketch window to change contrast" );
 println( "Drag mouse on Y-axis inside this sketch window to change brightness" );

}

public void stop() {
 opencv.stop();
 super.stop();
}

void draw() {
 if (cam.available() == true) {
   cam.read();
   image(cam, 0, 0);
 }

// //////////////////  HERE IS THE IMPORTANT PART

 opencv.allocate(320,240);                        // create the buffer
 opencv.copy(cam,0,0,640,480,0,0,320,240);
 opencv.convert( GRAY );
 opencv.flip( OpenCV.FLIP_HORIZONTAL ); // flip horizontally

 opencv.contrast( contrast_value );
 opencv.brightness( brightness_value );

 // proceed detection
 Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );

 image( opencv.image(), 0, 0 );

 // draw face area(s)
 noFill();
 stroke(255,0,0);
 // rect(30,18,88,333);
 for( int i=0; i<faces.length; i++ ) {
   rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
   println("DDD");
 }

}


void mouseDragged() {
 contrast_value   = (int) map( mouseX, 0, width, -128, 128 );
 brightness_value = (int) map( mouseY, 0, width, -128, 128 );
}




Thanks again in advance folks!
Re: openCV - display an image while analyzing a copy
Reply #1 - May 23rd, 2009, 6:03pm
 
The two best ways to speed up a cv process while still displaying the captured image is to get the Intel Performance Primitives and to thread the analysis.  The resize idea seems like it could be effective as well, but you can define the size of the capture with the parameters of
opencv.capture( width , height ) and couple it with a second OpenCV object which you copy it to and resize.

summary:

1.capture
2.allocate
3.copy
4.resize
5.analyze
6.display (doing this last ensures that you are using the information analyzed from the current frame and not a past frame which could give you wrong information )

hope this helps!
Re: openCV - display an image while analyzing a copy
Reply #2 - Jul 7th, 2009, 12:34am
 
I hit the same problem with OpenCV, and stumbled upon the solution.  The code below will run at about 50fps on my macbook pro core 2 due 2ghz...

import hypermedia.video.*;
import processing.video.*;

Capture cam;
OpenCV opencv;
int w = 320;
int h = 240;

void setup() {
 size( w, h );
 cam = new Capture(this, w, h);

 opencv = new OpenCV( this );
 opencv.allocate(w,h);
 opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT);

}


void draw(){
 background(0);

 cam.read();
 image(cam, 0, 0);
 opencv.copy(cam);
 opencv.convert( GRAY ); // convert to grey to do detection
 Rectangle[] faces = opencv.detect(1.2, 0,OpenCV.HAAR_DO_ROUGH_SEARCH  + OpenCV.HAAR_FIND_BIGGEST_OBJECT , 20, 20);

 // draw detected face area(s)
 noFill();
 stroke(255,0,0);
 for( int i=0; i<faces.length; i++ ) {
   rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
 }
}










Page Index Toggle Pages: 1