März 5, 2012
an example using the Xbox Kinect to detect motion differences.import SimpleOpenNI.*; SimpleOpenNI context; // Previous Frame PImage prevFrame; // How different must a pixel be to be a "motion" pixel float threshold = 50; void setup() { context = new SimpleOpenNI(this); // mirror is by default enabled context.setMirror(false); // enable depthMap generation if (context.enableDepth() == false) { println("Can't open the depthMap, maybe the camera is not connected!"); exit(); return; } // enable ir generation //context.enableRGB(640,480,30); //context.enableRGB(1280,1024,15); if (context.enableRGB() == false) { println("Can't open the rgbMap, maybe the camera is not connected or there is no rgbSensor!"); exit(); return; } size(context.rgbWidth(), context.rgbHeight()); //context.update(); prevFrame = createImage(context.rgbWidth(), context.rgbHeight(),RGB); //smooth(); } void draw() { prevFrame.copy(context.rgbImage(), 0, 0, context.rgbWidth(),context.rgbHeight() , 0, 0, context.rgbWidth(), context.rgbHeight()); // Before we read the new frame, we always save the previous frame for comparison! prevFrame.updatePixels(); // update the cam context.update(); prevFrame.loadPixels(); context.rgbImage().loadPixels(); loadPixels(); // Begin loop to walk through every pixel for (int x = 0; x < context.rgbImage().width; x ++ ) { for (int y = 0; y < context.rgbImage().height; y ++ ) { int loc = x + y*context.rgbImage().width; // Step 1, what is the 1D pixel location color current = context.rgbImage().pixels[loc]; // Step 2, what is the current color color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color // 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); float diff = dist(r1, g1, b1, r2, g2, b2); // Step 5, How different are the colors? // If the color at that pixel has changed, then there is motion at that pixel. if (diff > threshold) { // If motion, display black pixels[loc] = color(0); } else { // If not, display white pixels[loc] = color(255); } } } updatePixels(); }