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, Blob detection, convert shape into Pimage
Page Index Toggle Pages: 1
OpenCV, Blob detection, convert shape into Pimage (Read 3337 times)
OpenCV, Blob detection, convert shape into Pimage
Nov 5th, 2009, 7:27am
 
/*

Hi everybody. I'm working on a blob detection system to extract silhouette from a background. So i used openCV, and it helped me well until now. Indeed i would like to know how can i convert the output, the blobs (shapes) into a Pimage. I searched in so many ways, but evertimes i have syntax problems.

Below is the sample code from open cv. If you try it, you will see on the bottom right corner, your image filled with blobs and squares. And this is what i want to get. To convert this area into pixels, as a Pimage. Exactly like if you would take a screenshot of this area. (For instance in action script you can convert a movieclip into a bitmap)

I would be really happy that someone help me on that Smiley

cheers

ps:
If you dont have OpenCV for p55 installed in your library, you can find it right here :
ubaa.net/shared/processing/opencv/

*/


import hypermedia.video.*;



OpenCV opencv;

int w = 320;
int h = 240;
int threshold = 80;

boolean find=true;

PFont font;

void setup() {

 size( w*2+30, h*2+30 );

 opencv = new OpenCV( this );
 opencv.capture(w,h);

 font = loadFont( "AndaleMono.vlw" );
 textFont( font );

 println( "Drag mouse inside sketch window to change threshold" );
 println( "Press space bar to record background image" );

}



void draw() {

 background(0);
 opencv.read();
 //opencv.flip( OpenCV.FLIP_HORIZONTAL );

 image( opencv.image(), 10, 10 );                  // RGB image
 image( opencv.image(OpenCV.GRAY), 20+w, 10 );   // GRAY image
 image( opencv.image(OpenCV.MEMORY), 10, 20+h ); // image in memory

 opencv.absDiff();
 opencv.threshold(threshold);
 image( opencv.image(OpenCV.GRAY), 20+w, 20+h ); // absolute difference image


 // working with blobs
 Blob[] blobs = opencv.blobs( 100, w*h/3, 20, true );

 noFill();

 pushMatrix();
 translate(20+w,20+h);

 for( int i=0; i<blobs.length; i++ ) {

   Rectangle bounding_rect      = blobs[i].rectangle;
   float area = blobs[i].area;
   float circumference = blobs[i].length;
   Point centroid = blobs[i].centroid;
   Point[] points = blobs[i].points;

   // rectangle
   noFill();
   stroke( blobs[i].isHole ? 128 : 64 );
   rect( bounding_rect.x, bounding_rect.y, bounding_rect.width, bounding_rect.height );


   // centroid
   stroke(0,0,255);
   line( centroid.x-5, centroid.y, centroid.x+5, centroid.y );
   line( centroid.x, centroid.y-5, centroid.x, centroid.y+5 );
   noStroke();
   fill(0,0,255);
   text( area,centroid.x+5, centroid.y+5 );

   // ---------------------------- HERE IS THE PART ABOUT BLOBS THAT I WANT TO CONVERT

   fill(255,0,255,64);
   stroke(255,0,255);
   if ( points.length>0 ) {
     beginShape();
     for( int j=0; j<points.length; j++ ) {
       vertex( points[j].x, points[j].y );
     }
     endShape(CLOSE);
   }
   
   // --------------------------------------------------------------------------

   noStroke();
   fill(255,0,255);
   text( circumference, centroid.x+5, centroid.y+15 );

 }
 popMatrix();

}

void keyPressed() {
 if ( key==' ' ) opencv.remember();
}

void mouseDragged() {
 threshold = int( map(mouseX,0,width,0,255) );
}

public void stop() {
 opencv.stop();
 super.stop();
}
Re: OpenCV, Blob detection, convert shape into Pimage
Reply #1 - Nov 8th, 2009, 6:49pm
 
Nobody tried to answer to my message but at least i will give a feedback, cuz since last time i found a solution. So to capture a part of the screen, i used a BUFFER. I created my graphics outside the screen, and then copying them back in the screen by calling this buffer :


// setting up the buffer
buffer = createGraphics(320, 240, JAVA2D);

// creating graphics in the buffer
buffer.beginDraw();
buffer.fill(255,255,255,255);
buffer.stroke(255,255,255);
buffer.rect(0,0,50,50);
buffer.endDraw();

// calling back the buffer. Now this is a Pimage object
PImage blobcopy;
blobcopy = buffer.get(0, 0, buffer.width, buffer.height);
image(blobcopy, 0, 0);


Hope this can help someone later ^^
cheers
Page Index Toggle Pages: 1