Advertisement
Guest User

Simple Blob Detection

a guest
Aug 23rd, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. import cl.eye.*;
  2.  
  3. class TPoint {
  4.   public float x, y, z, h, hh;
  5.   TPoint (float x1, float y1, float z1, float h1, float hh1) {
  6.     x = x1;
  7.     y = y1;
  8.     z = z1;
  9.     h = h1;
  10.     hh = hh1;
  11.   }
  12.   PVector Pos() {
  13.     return new PVector((x/z), (y/z));
  14.   }
  15.   String Color() {
  16.     if ((h/z)>(hh/z)) {
  17.       return "Red";
  18.     }else{
  19.       return "Blue";
  20.     }
  21.   }
  22. }
  23.  
  24.  
  25. // Camera Variables
  26. int numCams;
  27. CLCamera myCameras[] = new CLCamera[2];
  28. PImage myImages[] = new PImage[2];
  29. int cameraWidth = 320;
  30. int cameraHeight = 240;
  31. int cameraRate = 125;
  32. int thresh = 190;
  33. ArrayList<TPoint> Blobs = new ArrayList<TPoint>();
  34.  
  35. void DetectBlob(int x, int y, float h, float hh) {
  36.   Boolean found = true;
  37.   for (TPoint Blub : Blobs) {
  38.     if (dist(Blub.Pos().x, Blub.Pos().y, x, y)<sqrt(Blub.z/PI)*1.4+15) {
  39.       found = false;
  40.       Blub.x += x;
  41.       Blub.y += y;
  42.       Blub.h += h;
  43.       Blub.hh += hh;
  44.       Blub.z++;
  45.     }
  46.   }
  47.   if (found) {
  48.     Blobs.add(new TPoint(x, y, 1,h, hh));
  49.   }
  50. }
  51.  
  52. void Vision(int i) {
  53.   // Loop through every pixel in the image.
  54.   for (int y = 1; y < myImages[i].height-1; y++) { // Skip top and bottom edges
  55.     for (int x = 1; x < myImages[i].width-1; x++) { // Skip left and right edges
  56.       int Pos = y*myImages[i].width + x;
  57.       if (brightness(myImages[i].pixels[Pos])>200) {
  58.         DetectBlob(x, y, red(myImages[i].pixels[Pos]), blue(myImages[i].pixels[Pos]));
  59.       }else{
  60.         myImages[i].pixels[Pos] = color(0);
  61.       }
  62.     }
  63.   }
  64.   // State that there are changes to edgeImg.pixels[]
  65.   myImages[i].updatePixels();
  66. }
  67. void setup() {
  68.   //CLCamera.loadLibrary("C:/CLEyeMulticam.dll");
  69.   frameRate(125);
  70.   noFill();
  71.   if (!setupCameras()) exit();
  72. }
  73.  
  74. void draw() {
  75.   background(0);
  76.   // Loops through available cameras and updates
  77.   for (int i = 0; i < numCams; i++) {
  78.     myCameras[i].getCameraFrame(myImages[i].pixels, (i==0) ? 1000 : 0);
  79.     Vision(i);
  80.     image(myImages[i], cameraWidth*i, 0);
  81.     for (TPoint Blub : Blobs) {
  82.       text("POINT: "+floor(Blub.Pos().x)+", "+floor(Blub.Pos().y)+": "+Blub.Color(), Blub.Pos().x+(cameraWidth*i)-30, (Blub.Pos().y)-5);
  83.       stroke(200, 0, 0);
  84.       ellipse((Blub.x/Blub.z)+(cameraWidth*i), Blub.y/Blub.z, (sqrt(Blub.z/PI)*1)*2, (sqrt(Blub.z/PI))*2);
  85.     }
  86.     /*
  87.     if(Blobs.size()>2){
  88.       line(Blobs.get(0).Pos().x*4,Blobs.get(0).Pos().y*4,Blobs.get(1).Pos().x*4,Blobs.get(1).Pos().y*4);
  89.       line(Blobs.get(1).Pos().x*4,Blobs.get(1).Pos().y*4,Blobs.get(2).Pos().x*4,Blobs.get(2).Pos().y*4);
  90.       line(Blobs.get(2).Pos().x*4,Blobs.get(2).Pos().y*4,Blobs.get(0).Pos().x*4,Blobs.get(0).Pos().y*4);
  91.     }
  92.     */
  93.     if(Blobs.size()==3){
  94.       line(Blobs.get(0).Pos().x,Blobs.get(0).Pos().y,Blobs.get(1).Pos().x,Blobs.get(1).Pos().y);
  95.       line(Blobs.get(1).Pos().x,Blobs.get(1).Pos().y,Blobs.get(2).Pos().x,Blobs.get(2).Pos().y);
  96.       line(Blobs.get(2).Pos().x,Blobs.get(2).Pos().y,Blobs.get(0).Pos().x,Blobs.get(0).Pos().y);
  97.     }
  98.     /*
  99.     for(int j = 0;j<Blobs.size()-1;j++){
  100.      line((Blobs.get(j).x/Blobs.get(j).z)+(cameraWidth*i),Blobs.get(j).y/Blobs.get(j).z,(Blobs.get(j+1).x/Blobs.get(j+1).z)+(cameraWidth*i),Blobs.get(j+1).y/Blobs.get(j+1).z);
  101.     }
  102.      
  103.      if(Blobs.size()==2){
  104.      line((Blobs.get(0).x/Blobs.get(0).z)+(cameraWidth*i),Blobs.get(0).y/Blobs.get(0).z,(Blobs.get(1).x/Blobs.get(1).z)+(cameraWidth*i),Blobs.get(1).y/Blobs.get(1).z);
  105.      text("D: "+dist((Blobs.get(0).x/Blobs.get(0).z)+(cameraWidth*i),Blobs.get(0).y/Blobs.get(0).z,(Blobs.get(1).x/Blobs.get(1).z)+(cameraWidth*i),Blobs.get(1).y/Blobs.get(1).z),(((Blobs.get(0).x/Blobs.get(0).z)+(cameraWidth*i))+((Blobs.get(1).x/Blobs.get(1).z)+(cameraWidth*i)))/2,(((Blobs.get(0).y/Blobs.get(0).z))+((Blobs.get(1).y/Blobs.get(1).z)))/2);
  106.      }
  107.      */
  108.     Blobs = new ArrayList<TPoint>();
  109.   }
  110.   stroke(255);
  111. }
  112.  
  113. boolean setupCameras() {
  114.   println("Getting number of cameras");
  115.   // Checks available cameras
  116.   numCams = CLCamera.cameraCount();
  117.   println("Found " + numCams + " cameras");
  118.   if (numCams == 0)  return false;
  119.   // create cameras and start capture
  120.   for (int i = 0; i < numCams; i++)
  121.   {
  122.     // Prints Unique Identifier per camera
  123.     println("Camera " + (i+1) + " UUID " + CLCamera.cameraUUID(i));
  124.     // New camera instance per camera
  125.     myCameras[i] = new CLCamera(this);
  126.     // ----------------------(i, CLEYE_GRAYSCALE/COLOR, CLEYE_QVGA/VGA, Framerate)
  127.     myCameras[i].createCamera(i, CLCamera.CLEYE_COLOR_PROCESSED, CLCamera.CLEYE_QVGA, cameraRate);
  128.     // Starts camera captures
  129.     myCameras[i].startCamera();
  130.     //myCameras[i].setCameraParam(CLCamera.CLEYE_AUTO_GAIN, 1);
  131.     //myCameras[i].setCameraParam(CLCamera.CLEYE_AUTO_EXPOSURE, 1);
  132.     //myCameras[i].setCameraParam(CLCamera.CLEYE_AUTO_WHITEBALANCE, 1);
  133.     myImages[i] = createImage(cameraWidth, cameraHeight, RGB);
  134.   }
  135.   // resize the output window
  136.   size(cameraWidth*numCams, cameraHeight);
  137.   println("Complete Initializing Cameras");  
  138.   return true;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement