Tuesday, February 18, 2014

Histogram based Motion Detection

Motion tracking is a useful tool. This post demonstrates motion tracking in Processing and shows it in use.
3 Main Programs will constitute the project:
  1. A program to capture images and store them just as in the post on Creating a High Magnitude FPS Camera using webcams
  2. A program to generate  histograms. These histograms use the highest brightness value in a column of pixels within the image.
  3. A program to compare histograms of an image and the image that precedes or follows it. This program will also have a method of telling the user that movement has been detected.
First: A program to capture images from a camera and store them in a plain image format:
import processing.video.*;
float a=0;
Capture dacam;

void setup() {
  size (320,240);
  dacam= new Capture(this, 320, 240, 30);
  dacam.start();


}

void draw(){

  if(dacam.available()) {
    dacam.read();
  }
  a=a+1;
  image(dacam, 0, 0);
  save(a+".png");

}

Then a program to load these images and generate a histogram.The histogram is useful because it does not require sampling of all pixels and then using a cross referencing algorithm. For our histogram the  line # of the text file will be the x cooardinate and the value stored in that line will be the value contained in the line:
size(640, 360);

PImage img = loadImage("9.0.png");
image(img, 0, 0);
int[] hist = new int[256];


for (int t = 0; t < img.width; t++) {
  for (int r = 0; r < img.height; r++) {
    int bright = int(brightness(get(t, r)));
    hist[bright]++; 
  }
}

int histMax = max(hist);

stroke(255);

for (int t = 0; t < img.width; t += 1) {
  int which = int(map(t, 0, img.width, 0, 255));

  int y = int(map(hist[which], 0, histMax, img.height, 0));
  line(t, img.height, t, y);
}


Finally a program to compare histograms and tell the user whether movement is detected:


float r=0;
void setup() {
  size(600,50);
  String one[]=loadStrings("lines.txt");
  String two[]=loadStrings("lines2.txt");
  println(one.length);
  println(two.length);
  for( int t=0;r<one.length;r++){
    if(one[t] != two[t]) {
      textSize(50);
     text("MOVEMENT DETECTED",10,40);
}
}
}
 
Histogram mapping is excellent for Astronomical event detection. This is an image of the Crab Nebula also known as M1, NGC 1952, or Taurus A.
 
Display Message when movement is detected 

No comments:

Post a Comment