We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Motion/direction detection
Page Index Toggle Pages: 1
Motion/direction detection (Read 654 times)
Motion/direction detection
Feb 27th, 2009, 2:16pm
 
Hi

I have been looking into some of the motion detection scripts around the processing site.

It is quite clear and easy to implement.
My challenge is that I would like to detect the direction of the movement, so the set up is:
A small webcam pointing at a hallway, and each time someone passes by it detects which way they were going.
I have this program that detects the motion, but I need an idea or push in which direction it would be wise to go if I need the direction of the motion.

So suggestions, ideas or pseudo examples is very appreciated :)

import processing.video.*;

Capture cam;


PImage prevFrame;
float threshold = 100;

void setup() {
 size(640,480);
 String[] devices = Capture.list();  
 println(devices);
 cam = new Capture(this, width, height, devices[2], 30);
 //cam.settings();
 prevFrame = createImage(cam.width, cam.height, RGB);
}

void draw() {
 prevFrame = createImage(cam.width, cam.height, RGB);
 if (cam.available()) {
   prevFrame.copy(cam, 0, 0, cam.width, cam.height, 0, 0, cam.width, cam.height); // Before we read the new prevFrame,
   prevFrame.updatePixels();
   println(prevFrame.pixels[9]);
   cam.read();
 }

 cam.loadPixels();
 prevFrame.loadPixels();

 for (int x = 0; x < cam.width; x++ ) {
   for (int y = 100; y < cam.height-100; y++ ) {
     int movementSum = 0;
     int loc = x + y * cam.width;
     color current = cam.pixels[loc];
     color previous = prevFrame.pixels[loc];

     int r1 = (current >> 16) & 0xFF;
     int g1 = (current >> 8) & 0xFF;
     int b1 = current & 0xFF;

     int r2 = (previous >> 16) & 0xFF;
     int g2 = (previous >> 8) & 0xFF;
     int b2 = previous & 0xFF;

     int diffR = abs(r1 - r2);
     int diffG = abs(g1 - g2);
     int diffB = abs(b1 - b2);
     movementSum += diffR + diffG + diffB;

     if (movementSum > threshold) {
       stroke(0);
     }
     else {
       stroke(255);
     }
     point(x,y);
   }
 }
}





Re: Motion/direction detection
Reply #1 - Mar 1st, 2009, 4:03am
 
Do a search on this site for "Optical Flow".  You basically choose a grid of pixels from your captured frame, then you track those pixels for movement for each subsequent frame.  In your case, you can average the pixel movements to get a general direction.

It's what I used for this sketch, though I use the local movement rather than the average:

http://vimeo.com/2633447
Re: Motion/direction detection
Reply #2 - Mar 2nd, 2009, 9:11am
 
Hi Johnny

Thank you, that was just the search term I needed to continue.
I liked your snow example:)
Page Index Toggle Pages: 1