This code can not do motion detection, why?
in
Contributed Library Questions
•
9 months ago
This is the original code that I plan to modify, anw this code works well to detect any motion, this code use common webcam attach to my computer, and I take it from here:
http://www.learningprocessing.com/examples/chapter-16/example-16-13/
here is the code, that works:
- import processing.video.*;
- // Variable for capture device
- Capture video;
- // Previous Frame
- PImage prevFrame;
- // How different must a pixel be to be a "motion" pixel
- float threshold = 50;
- void setup() {
- size(320,240);
- video = new Capture(this, width, height, 30);
- // Create an empty image the same size as the video
- prevFrame = createImage(video.width,video.height,RGB);
- }
- void draw() {
- // Capture video
- if (video.available()) {
- // Save previous frame for motion detection!!
- prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height);
- prevFrame.updatePixels();
- video.read();
- }
- loadPixels();
- video.loadPixels();
- prevFrame.loadPixels();
- // Begin loop to walk through every pixel
- for (int x = 0; x < video.width; x++ ) {
- for (int y = 0; y < video.height; y++ ) {
- int loc = x + y*video.width; // Step 1, what is the 1D pixel location
- color current = video.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 (diff > threshold) {
- // If motion, display black
- pixels[loc] = color(0);
- } else {
- // If not, display white
- pixels[loc] = color(255);
- }
- }
- }
- updatePixels();
- }
After I modify it, because I would like to do motion detection not using webcam but using my kinect camera, so I modify like this:
- import SimpleOpenNI.*;
- SimpleOpenNI kinect;
- // Previous Frame
- PImage currentFrame;
- PImage prevFrame;
- // How different must a pixel be to be a "motion" pixel
- float threshold =50;
- void setup()
- {
- size(640,480);
- kinect = new SimpleOpenNI (this);
- kinect.enableRGB ();
- // Create an empty image the same size as the currentFrame
- prevFrame = createImage(640,480,RGB);
- }
- void draw()
- {
- kinect.update();
- currentFrame = kinect.rgbImage ();
- if (kinect.enableRGB() == true ){
- prevFrame.copy (currentFrame,0,0, 640,480,0,0,640,480);
- prevFrame.updatePixels();
- image (currentFrame,0,0);
- }
- loadPixels();
- currentFrame.loadPixels ();
- prevFrame.loadPixels ();
- // Begin loop to walk through every pixel
- for (int x = 0; x < currentFrame.width; x++ ) {
- for (int y = 0; y < currentFrame.height; y++ ) {
- int loc = x + y*currentFrame.width; // Step 1, what is the 1D pixel location
- color current = currentFrame.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 (diff > threshold) {
- // If motion, display black
- pixels[loc] = color(0);
- } else {
- // If not, display white
- pixels[loc] = color(255);
- }
- }
- }
- updatePixels();
- }
But it result on white screen window.
It seems dont work well.
Anyone could help me to fix this problem ? thank you!!
1