Advertisement
itblanco

Untitled

Nov 24th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import kinect4WinSDK.Kinect;
  2. import processing.net.*;
  3. import kinect4WinSDK.SkeletonData;
  4.  
  5. Kinect kinect;
  6. color trackColor;
  7. float threshold = 80;
  8. PImage img;
  9.  
  10. void setup()
  11. {
  12.   size(1280, 960);
  13.   background(0);
  14.   kinect = new Kinect(this);
  15.    trackColor = color(255, 0, 0);
  16.    img = createImage(100, 100, RGB);
  17. }
  18.  
  19.  
  20. void draw()
  21. {
  22.   background(0);
  23.   img = kinect.GetImage();
  24.   image(img, 0, 0, 1000, 745);  
  25.  
  26.   float avgX = 0;
  27.   float avgY = 0;
  28.  
  29.   int count = 0;
  30.  
  31.   img.loadPixels();
  32.   // Begin loop to walk through every pixel
  33.   for (int x = 0; x < img.width; x++ ) {
  34.     for (int y = 0; y < img.height; y++ ) {
  35.       int loc = x + y * img.width;
  36.       // What is current color
  37.       color currentColor = img.pixels[loc];
  38.       float r1 = red(currentColor);
  39.       float g1 = green(currentColor);
  40.       float b1 = blue(currentColor);
  41.       float r2 = red(trackColor);
  42.       float g2 = green(trackColor);
  43.       float b2 = blue(trackColor);
  44.  
  45.       float d = distSq(r1, g1, b1, r2, g2, b2);
  46.  
  47.       if (d < threshold*threshold) {
  48.         stroke(255);
  49.         strokeWeight(1);
  50.         point(x, y);
  51.         avgX += x;
  52.         avgY += y;
  53.         count++;
  54.       }
  55.     }
  56.   }
  57.  
  58.   // We only consider the color found if its color distance is less than 10.
  59.   // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
  60.   if (count > 0) {
  61.     avgX = avgX / count;
  62.     avgY = avgY / count;
  63.     // Draw a circle at the tracked pixel
  64.     fill(255);
  65.     strokeWeight(4.0);
  66.     stroke(0);
  67.     ellipse(avgX, avgY, 24, 24);
  68.   }
  69. }
  70.  
  71. float distSq(float x1, float y1, float z1, float x2, float y2, float z2) {
  72.   float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
  73.   return d;
  74. }
  75.  
  76. void mousePressed() {
  77.   // Save color where the mouse is clicked in trackColor variable
  78.   int loc = mouseX + mouseY * img.width;
  79.   trackColor = img.pixels[loc];
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement