Overall motion with openkinect

Hi,

I'm trying to display a circle that changes color and size based on the average amount of motion using Kinect V2 and openkinect. My code is based on this example on Learning Processing: http://learningprocessing.com/examples/chp16/example-16-14-MotionSensor

The example is working fine for me, but when I try the same using kinect, there's nothing happening. I can't get my head around what's wrong in this code, would really appreciate your help!

import org.openkinect.processing.*;

// Kinect Library object
Kinect2 kinect2;


PImage img;
PImage prevFrame;
float threshold=50;

void setup() {
  size(512, 424);

  kinect2 = new Kinect2(this);
   kinect2.initVideo();
  kinect2.initDevice();
  img = createImage(kinect2.depthWidth, kinect2.depthHeight, RGB);
  prevFrame = createImage(kinect2.depthWidth, kinect2.depthHeight, RGB);

}


void draw() {
  background(255);

  img.loadPixels();
   prevFrame.loadPixels();

   //Save previous frame for motion detection
    prevFrame.copy(img, 0, 0, 512, 424, 0, 0, 512, 424);

    float totalMotion = 0;

  for (int x = 0; x < kinect2.depthWidth; x++) {
    for (int y = 0; y < kinect2.depthHeight; y++) {
      int offset = x + y * kinect2.depthWidth;
      color current = img.pixels[offset];
      color previous= prevFrame.pixels[offset];

// Step 4, compare colors (previous vs. current)
    float r1 = red(current); 
    float g1 = green(current);
    float b1 = blue(current);
    float r2 = red(previous); 
    float g2 = green(previous);
    float b2 = blue(previous);

    // Motion for an individual pixel is the difference between the previous color and current color.
    float diff = dist(r1, g1, b1, r2, g2, b2);
    // totalMotion is the sum of all color differences. 
    totalMotion += diff;


    }
  }


 prevFrame.updatePixels();
  img.updatePixels();


 // image(kinect2.getVideoImage(), 0, 0);

 // averageMotion is total motion divided by the number of pixels analyzed.
  float avgMotion = totalMotion / img.pixels.length; 

  // Draw a circle based on average motion
  noStroke();
  fill(0);
  float r = avgMotion * 2;
  ellipse(width/2, height/2, r, r);
}
Sign In or Register to comment.