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
Finger detection with OpenCV (Read 2731 times)
Finger detection with OpenCV
Apr 19th, 2010, 9:00am
 
Hey kids,
  I am working with the OpenCV libraries and I would like to do some finger detection. I looked a bit into Haar features and that might have some potential, but then it occurred to me to wrap a colored paper around my finger then filter out everything except that color and then do some blob detection.

However, my hang up is on the image manipulation. I had a friend do some photoshop magic and he got pretty close with a darken red and green channels (I had a yellow piece of paper on my finger).
So, my questions:
1) How can I "darken" red and green? I have been playing with the PImage.set( x, y, color ) method, with mixed results. Is there a better way to do this?
2) What other image libraries are out there that would give me some similar control that is available in photoshop or gimp?

Here is what I have so far.
Code:
import hypermedia.video.*;

OpenCV opencv;
PImage img;
PImage redImage;
PImage greImage;
PImage bluImage;

PImage test, test2;

void setup() {
 size( 640, 480 );
 opencv = new OpenCV(this);
 opencv.capture(width,height);

 redImage = createMask( width, height, 255, 0, 0 );
 greImage = createMask( width, height, 0, 255, 0 );
 bluImage = createMask( width, height, 0, 0, 255 );

 test = createMask( width, height, 255, 255, 0);
 test2 = createMask( width, height, 0, 0, 255 );

}

void draw() {
 opencv.read();
 //   opencv.threshold( 50 );

 img = opencv.image();

 if( key == 'r' )
   img = myMask( img, 0, -1, -1 );
 else if( key == 'g' )
   img = myMask( img, -1, 0, -1 );
 else if( key == 'b' )
   img = myMask( img, -1, -1, 0 );

 image( img , 0, 0 );
}


PImage myMask( PImage i, int r, int g, int b )
{

 for( int x=0; x<i.width; x++ )
   for( int y=0; y<i.height; y++ ){
     if( r != -1 )
       i.set( x, y, color( r, green( i.get( x, y ) ), blue( i.get( x, y ) ) ) );

     if( g != -1 )
       i.set( x, y, color( red(i.get(x,y)) , g, blue( i.get( x, y ) ) ) );
     if( b != -1 )
       i.set( x, y, color( red(i.get(x,y)) , green( i.get( x, y ) ), b ) );
   }
 return i;  
}

PImage createMask( int w, int h, int r, int g, int b )
{

 PImage img = createImage( w, h, RGB );
 img.loadPixels();
 for (int i = 0; i < img.pixels.length; i++) {
   img.pixels[i] = color(r, g, b );
 }
 img.updatePixels();

 return img;

}

Page Index Toggle Pages: 1