Advertisement
Guest User

Processing Point Tracking

a guest
Jul 20th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.00 KB | None | 0 0
  1. //The code I've been using in Processing to track points
  2. //Kudos if you can reverse engineer it.
  3. //Mostly by Johnathon "zalo" Selstad
  4.  
  5. import cl.eye.*;
  6. import oscP5.*;
  7. import netP5.*;
  8. import processing.serial.*;
  9. import java.awt.*;
  10.  
  11. OscP5 oscP5;
  12.  
  13. NetAddress myBroadcastLocation;
  14.  
  15. class TPoint {
  16.   public float x, y, z;
  17.   TPoint (float x1, float y1, float z1) {
  18.     x = x1;
  19.     y = y1;
  20.     z = z1;
  21.   }
  22.   PVector Pos() {
  23.     return new PVector((x/z), (y/z));
  24.   }
  25. }
  26.  
  27. float[] track = new float[4];
  28.  
  29. // Camera Variables
  30. int numCams;
  31. CLCamera myCameras[] = new CLCamera[2];
  32. PImage myImages[] = new PImage[2];
  33. int cameraWidth = 320;
  34. int cameraHeight = 240;
  35. int cameraRate = 125;
  36. int thresh = 40;
  37. ArrayList<TPoint> Blobs = new ArrayList<TPoint>();
  38.  
  39. void DetectBlob(int x, int y) {
  40.   Boolean found = true;
  41.   for (TPoint Blub : Blobs) {
  42.     if (dist(Blub.Pos().x, Blub.Pos().y, x, y)<sqrt(Blub.z/PI)*1.4+15) {
  43.       found = false;
  44.       Blub.x += x;
  45.       Blub.y += y;
  46.       Blub.z++;
  47.     }
  48.   }
  49.   if (found) {
  50.     Blobs.add(new TPoint(x, y, 1));
  51.   }
  52. }
  53.  
  54. void Vision(int i) {
  55.   // Loop through every pixel in the image.
  56.   for (int y = 1; y < myImages[i].height-1; y++) { // Skip top and bottom edges
  57.     for (int x = 1; x < myImages[i].width-1; x++) { // Skip left and right edges
  58.       int Pos = y*myImages[i].width + x;
  59.       if (brightness(myImages[i].pixels[Pos])>thresh) {
  60.         DetectBlob(x, y);
  61.       }
  62.     }
  63.   }
  64.   // State that there are changes to edgeImg.pixels[]
  65.   myImages[i].updatePixels();
  66. }
  67.  
  68.  
  69. boolean setupCameras() {
  70.   println("Getting number of cameras");
  71.   // Checks available cameras
  72.   numCams = CLCamera.cameraCount();
  73.   println("Found " + numCams + " cameras");
  74.   if (numCams == 0)  return false;
  75.   // create cameras and start capture
  76.   for (int i = 0; i < numCams; i++)
  77.   {
  78.     // Prints Unique Identifier per camera
  79.     println("Camera " + (i+1) + " UUID " + CLCamera.cameraUUID(i));
  80.     // New camera instance per camera
  81.     myCameras[i] = new CLCamera(this);
  82.     // ----------------------(i, CLEYE_GRAYSCALE/COLOR, CLEYE_QVGA/VGA, Framerate)
  83.     myCameras[i].createCamera(i, CLCamera.CLEYE_GRAYSCALE, CLCamera.CLEYE_QVGA, cameraRate);
  84.     // Starts camera captures
  85.     myCameras[i].startCamera();
  86.     //myCameras[i].setCameraParam(CLCamera.CLEYE_AUTO_GAIN, 1);
  87.     //myCameras[i].setCameraParam(CLCamera.CLEYE_AUTO_EXPOSURE, 1);
  88.     //myCameras[i].setCameraParam(CLCamera.CLEYE_AUTO_WHITEBALANCE, 1);
  89.     myImages[i] = createImage(cameraWidth, cameraHeight, RGB);
  90.   }
  91.   // resize the output window
  92.   size(320, 240);
  93.   println("Complete Initializing Camera");  
  94.   return true;
  95. }
  96.  
  97. void sendTrack() {
  98.   OscMessage myOscMessage = new OscMessage("/trackingData");
  99.   myOscMessage.add(track[0]);
  100.   myOscMessage.add(track[1]);
  101.   myOscMessage.add(track[2]);
  102.   myOscMessage.add(track[3]);
  103.   oscP5.send(myOscMessage, myBroadcastLocation);
  104.   println("Sent Points - X1: "+floor(track[0])+" Y1: "+floor(track[1])+" X2: "+floor(track[2])+" Y2: "+floor(track[3]));
  105. }
  106.  
  107. void oscEvent(OscMessage theOscMessage) {
  108.   /* get and print the address pattern and the typetag of the received OscMessage */
  109.   println("### received an osc message with addrpattern "+theOscMessage.addrPattern()+" and typetag "+theOscMessage.typetag());
  110.   theOscMessage.print();
  111. }
  112.  
  113. void setup() {
  114.   //CLCamera.loadLibrary("C:/CLEyeMulticam.dll");
  115.   frameRate(cameraRate);
  116.   noFill();
  117.   if (!setupCameras()) exit();
  118.  
  119.   oscP5 = new OscP5(this,12000);
  120.   myBroadcastLocation = new NetAddress("127.0.0.1",8000);
  121. }
  122.  
  123. void draw() {
  124.   background(0);
  125.   // Loops through available cameras and updates
  126.   for (int i = 0; i < numCams; i++) {
  127.     myCameras[i].getCameraFrame(myImages[i].pixels, (i==0) ? 1000 : 0);
  128.     Vision(i);
  129.     image(myImages[i], cameraWidth*i, 0);
  130.     for (TPoint Blub : Blobs) {
  131.       text("POINT: "+floor(Blub.Pos().x)+", "+floor(Blub.Pos().y), (Blub.Pos().x)-30, (Blub.Pos().y)-(Blub.z/30));
  132.       stroke(200, 0, 0);
  133.       ellipse(Blub.Pos().x+(cameraWidth*i), Blub.Pos().y, (sqrt(Blub.z/PI)*1)*2, (sqrt(Blub.z/PI))*2);
  134.     }
  135.     if(Blobs.size()==2){
  136.       line(Blobs.get(0).Pos().x,Blobs.get(0).Pos().y,Blobs.get(1).Pos().x,Blobs.get(1).Pos().y);
  137.       line(((Blobs.get(0).Pos().x+Blobs.get(1).Pos().x)/2),((Blobs.get(0).Pos().y+Blobs.get(1).Pos().y)/2),((Blobs.get(0).Pos().x+Blobs.get(1).Pos().x)/2)-((Blobs.get(0).Pos().y-Blobs.get(1).Pos().y)/2),((Blobs.get(0).Pos().y+Blobs.get(1).Pos().y)/2)+((Blobs.get(0).Pos().x-Blobs.get(1).Pos().x)/2));
  138.       line(((Blobs.get(0).Pos().x+Blobs.get(1).Pos().x)/2),((Blobs.get(0).Pos().y+Blobs.get(1).Pos().y)/2),((Blobs.get(0).Pos().x+Blobs.get(1).Pos().x)/2)+((Blobs.get(0).Pos().y-Blobs.get(1).Pos().y)/2),((Blobs.get(0).Pos().y+Blobs.get(1).Pos().y)/2)-((Blobs.get(0).Pos().x-Blobs.get(1).Pos().x)/2  ));
  139.       track[0] = Blobs.get(0).Pos().x;
  140.       track[1] = Blobs.get(0).Pos().y;
  141.       track[2] = Blobs.get(1).Pos().x;
  142.       track[3] = Blobs.get(1).Pos().y;
  143.       sendTrack();
  144.     }
  145.     Blobs = new ArrayList<TPoint>();
  146.   }
  147.   stroke(255);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement