KinectDuino

Kinect and Arduino one step ahead to Computer Science

Kinect Motion Detection

Finally I got this work since my last post, still using Kinect for Windows in this post I would like to share after some code modification on how to detect any motion or movements using Processing 1.5.1 and Simple OpenNI library. It is recommended to use Kinect and Simple OpenN, because it is very sensitive doing simple motion tracking compare with webcam camera with internal processing video library.

This is not the best or effective coding result, but it is enough to understand on how motion detection works by comparing frame by frame. It works well with my computer specification: Windows 7 Home Premium, i7 3.4 Ghz, 4GB RAM, 2GB VGA, I wish it works well in yours, so let me know whether you find any troubles or mistaken or ineffectiveness in the code.

It uses Kinect to detect any little movement or motion, I shake my self back and forward to get that black color. Without any motion, it will only display white.

It uses Kinect to detect any little movement or motion, I shake my self back and forward to get that black color. Without any motion, it will only display white.

import SimpleOpenNI.*;
SimpleOpenNI kinect;

// Previous Frame
PImage currentFrame;
PImage prevFrame;
// How different must a pixel be to be a "motion" pixel
float threshold =30;
long timer;

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 ();

 // for execution delay, giving time for processing do its job
 int m = millis();
 if ( m - timer > 50)
 {
 prevFrame.copy(currentFrame,0,0,currentFrame.width,currentFrame.height,0,0,prevFrame.width,prevFrame.height);
 prevFrame.updatePixels();
 timer = m;
 }

 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; //what is the 1D pixel location
color current = currentFrame.pixels[loc]; //what is the current color
color previous = prevFrame.pixels[loc]; //what is the previous color
//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);
//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();
}

MAKE IT MORE COOOOOOL! BY REPLACING WITH THIS CODE:

if (diff > threshold) {
// If motion, display black
pixels[loc] = color(255,0,0); //any motion object will be detected red
} else {
// If not, display white
pixels[loc] = color(previous); //any static object will be display as it is
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on January 7, 2013 by in Kinect and tagged , , .

Navigation

%d bloggers like this: