Advertisement
Guest User

Silhouette extraction

a guest
Jul 20th, 2011
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. import hypermedia.video.*;        //  Imports the OpenCV library
  2. import processing.video.*;
  3. import org.openkinect.*;
  4. import org.openkinect.processing.*;
  5.  
  6. MovieMaker mm;
  7. Kinect kinect;
  8. OpenCV opencv;                    //  Creates a new OpenCV Object
  9. PrintWriter pw = null;
  10.  
  11. PImage depth,copia;
  12. PImage img;
  13. int x=-10,y=-35;
  14. void simplify(PVector[] d, float tol,int s, int e, boolean[] out) {
  15.   out[s]=out[e]=true;
  16.   if(abs(s-e)<=1) return;
  17.   float distance,area, m=0.0;
  18.   int  who=-1;
  19.   PVector a =  d[s];
  20.   PVector b = d[e];
  21.   PVector c = null;
  22.   for(int i=s+1;i<e;i++) {
  23.     c=d[i];
  24.     area=abs(a.x*b.y+b.x*c.y+c.x*a.y - b.x*a.y-c.x*b.y-a.x*c.y);
  25.     distance = area/a.dist(b);
  26.     if(distance>m && distance>tol) {
  27.       m=distance;
  28.       who=i;
  29.     }
  30.   }
  31.   if(who==-1) return;
  32.   simplify(d,tol,s,who,out);
  33.   simplify(d,tol,who,e,out);
  34. }
  35.  
  36. void setup() {
  37.   String filename = sketchPath("out.txt");
  38.   try {pw = new PrintWriter(new FileOutputStream(filename));} catch(Exception e) {println("FNFE"); }
  39.   size( 640, 480 );
  40.   depth = createImage(640,480,RGB);
  41.   img = createImage(640,480,RGB);
  42.   copia = createImage(640,480,RGB);
  43.   kinect = new Kinect(this);
  44.   kinect.start();
  45.   kinect.enableDepth(true);
  46.   kinect.enableRGB(true);
  47. //  mm = new MovieMaker(this, width, height, "silueta.mov");
  48.   opencv = new OpenCV( this );  
  49. }
  50.  
  51.  
  52. void draw() {
  53.   background(0);
  54.   int histo[] =  new int[256];
  55.   img = kinect.getRGBImage();
  56.   //img.updatePixels();
  57.   // opencv.loadImage( "depth.png",width, height );     //  Opens a video capture stream
  58.   opencv.allocate(640,480);
  59.   //opencv.read();
  60.   //image( opencv.image(), 0, 0 );  //  Displays the image in the OpenCV buffer to the screen
  61.  
  62.   //depth.pixels = NativeKinect.getDepthMap();
  63.   depth = kinect.getDepthImage();
  64.   copia=depth.get();
  65.   for(int i=0;i<640*480;i++) { histo[int(brightness(depth.pixels[i]))]++;
  66.   if(brightness(copia.pixels[i])>=135 && brightness(copia.pixels[i])<=140) /* copia.pixels[i]=color(200) */ ;   else copia.pixels[i]=color(0);
  67.   }
  68.   //depth.updatePixels();
  69.   opencv.copy(copia);
  70.   image(img,x,y);
  71.   stroke(255);
  72.  
  73.   Blob[] blobs = opencv.blobs( 15000, width*height/2, 100, true, OpenCV.MAX_VERTICES*4 );
  74.  
  75.   if( blobs.length>0) {
  76.     PVector[] d = new PVector[blobs[0].points.length];
  77.     for( int j=0; j<blobs[0].points.length; j++ ) {
  78.       d[j]=new PVector(blobs[0].points[j].x,blobs[0].points[j].y);
  79.     }
  80.  
  81.     boolean out[] = new boolean[blobs[0].points.length];
  82.  
  83.     simplify(d,5,0,blobs[0].points.length-1,out);
  84.  
  85.  
  86.     stroke(250,0,0);
  87.     //background(0);
  88.     int i=0;
  89.     strokeWeight(2);
  90.     noFill();
  91.     beginShape();
  92.     for( int j=0; j<blobs[i].points.length; j++ ) {
  93.       if(out[j]==true) {
  94.         vertex( blobs[i].points[j].x, blobs[i].points[j].y );
  95.         pw.print(blobs[i].points[j].x +" "+ blobs[i].points[j].y + " ");
  96.       }
  97.     }
  98.     endShape(CLOSE);
  99.     pw.println();
  100.   // mm.addFrame();
  101.  
  102.   }    
  103. }
  104. /*
  105. void keyPressed() {
  106.   if (key == ' ') {
  107.     // Finish the movie if space bar is pressed
  108.   //  mm.finish();
  109.     // Quit running the sketch once the file is written
  110.     exit();
  111.   }
  112. }
  113. */
  114.  
  115. void stop() {
  116.   kinect.quit();
  117.   super.stop();
  118. }
  119.  
  120. void keyPressed() {
  121.   if (key == 'd') {
  122.  x--;
  123.  
  124.   }
  125.   else if (key == 'r') {
  126. y--;
  127.   }
  128.   println (x+","+y);
  129.  
  130.   if (key == ' ') {
  131.     // Finish the movie if space bar is pressed
  132.   //  mm.finish();
  133.     // Quit running the sketch once the file is written
  134.     exit();
  135.   }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement