Advertisement
Guest User

AveragePointTracking

a guest
Apr 14th, 2020
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. // Daniel Shiffman
  2. // Tracking the average location beyond a given depth threshold
  3. // Thanks to Dan O'Sullivan
  4.  
  5. // https://github.com/shiffman/OpenKinect-for-Processing
  6. // http://shiffman.net/p5/kinect/
  7.  
  8. import org.openkinect.freenect.*;
  9. import org.openkinect.processing.*;
  10.  
  11. // The kinect stuff is happening in another class
  12. KinectTracker tracker;
  13. Kinect kinect;
  14.  
  15.  
  16. void setup() {
  17.   size(640, 520);
  18.   kinect = new Kinect(this);
  19.   tracker = new KinectTracker();
  20. }
  21.  
  22. void draw() {
  23.   background(255);
  24.  
  25.   // Run the tracking analysis
  26.   tracker.track();
  27.   // Show the image
  28.   tracker.display();
  29.  
  30.   // Let's draw the raw location
  31.   PVector v1 = tracker.getPos();
  32.   fill(50, 100, 250, 200);
  33.   noStroke();
  34.   ellipse(v1.x, v1.y, 20, 20);
  35.  
  36.   // Let's draw the "lerped" location
  37.   PVector v2 = tracker.getLerpedPos();
  38.   fill(100, 250, 50, 200);
  39.   noStroke();
  40.   ellipse(v2.x, v2.y, 20, 20);
  41.  
  42.   // Display some info
  43.   int t = tracker.getThreshold();
  44.   fill(0);
  45.   text("threshold: " + t + "    " +  "framerate: " + int(frameRate) + "    " +
  46.     "UP increase threshold, DOWN decrease threshold", 10, 500);
  47. }
  48.  
  49. // Adjust the threshold with key presses
  50. void keyPressed() {
  51.   int t = tracker.getThreshold();
  52.   if (key == CODED) {
  53.     if (keyCode == UP) {
  54.       t+=5;
  55.       tracker.setThreshold(t);
  56.     } else if (keyCode == DOWN) {
  57.       t-=5;
  58.       tracker.setThreshold(t);
  59.     }
  60.   }
  61. }
  62.  
  63. / Daniel Shiffman
  64. // Tracking the average location beyond a given depth threshold
  65. // Thanks to Dan O'Sullivan
  66.  
  67. // https://github.com/shiffman/OpenKinect-for-Processing
  68. // http://shiffman.net/p5/kinect/
  69.  
  70. class KinectTracker {
  71.  
  72.   // Depth threshold
  73.   int threshold = 505;
  74.  
  75.   // Raw location
  76.   PVector loc;
  77.  
  78.   // Interpolated location
  79.   PVector lerpedLoc;
  80.  
  81.   // Depth data
  82.   int[] depth;
  83.  
  84.   // What we'll show the user
  85.   PImage display;
  86.    
  87.   KinectTracker() {
  88.     // This is an awkard use of a global variable here
  89.     // But doing it this way for simplicity
  90.     kinect.initDepth();
  91.     kinect.enableMirror(true);
  92.     // Make a blank image
  93.     display = createImage(kinect.width, kinect.height, RGB);
  94.     // Set up the vectors
  95.     loc = new PVector(0, 0);
  96.     lerpedLoc = new PVector(0, 0);
  97.   }
  98.  
  99.   void track() {
  100.     // Get the raw depth as array of integers
  101.     depth = kinect.getRawDepth();
  102.  
  103.     // Being overly cautious here
  104.     if (depth == null) return;
  105.  
  106.     float sumX = 0;
  107.     float sumY = 0;
  108.     float count = 0;
  109.  
  110.     for (int x = 0; x < kinect.width; x++) {
  111.       for (int y = 0; y < kinect.height; y++) {
  112.        
  113.         int offset =  x + y*kinect.width;
  114.         // Grabbing the raw depth
  115.         int rawDepth = depth[offset];
  116.  
  117.         // Testing against threshold
  118.         if (rawDepth < threshold) {
  119.           sumX += x;
  120.           sumY += y;
  121.           count++;
  122.         }
  123.       }
  124.     }
  125.     // As long as we found something
  126.     if (count != 0) {
  127.       loc = new PVector(sumX/count, sumY/count);
  128.     }
  129.  
  130.     // Interpolating the location, doing it arbitrarily for now
  131.     lerpedLoc.x = PApplet.lerp(lerpedLoc.x, loc.x, 0.3f);
  132.     lerpedLoc.y = PApplet.lerp(lerpedLoc.y, loc.y, 0.3f);
  133.   }
  134.  
  135.   PVector getLerpedPos() {
  136.     return lerpedLoc;
  137.   }
  138.  
  139.   PVector getPos() {
  140.     return loc;
  141.   }
  142.  
  143.   void display() {
  144.     PImage img = kinect.getDepthImage();
  145.  
  146.     // Being overly cautious here
  147.     if (depth == null || img == null) return;
  148.  
  149.     // Going to rewrite the depth image to show which pixels are in threshold
  150.     // A lot of this is redundant, but this is just for demonstration purposes
  151.     display.loadPixels();
  152.     for (int x = 0; x < kinect.width; x++) {
  153.       for (int y = 0; y < kinect.height; y++) {
  154.  
  155.         int offset = x + y * kinect.width;
  156.         // Raw depth
  157.         int rawDepth = depth[offset];
  158.         int pix = x + y * display.width;
  159.         if (rawDepth < threshold) {
  160.           // A red color instead
  161.           display.pixels[pix] = color(150, 50, 50);
  162.         } else {
  163.           display.pixels[pix] = img.pixels[offset];
  164.         }
  165.       }
  166.     }
  167.     display.updatePixels();
  168.  
  169.     // Draw the image
  170.     image(display, 0, 0);
  171.   }
  172.  
  173.   int getThreshold() {
  174.     return threshold;
  175.   }
  176.  
  177.   void setThreshold(int t) {
  178.     threshold =  t;
  179.   }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement