RickiG
Junior Member
Offline
Posts: 81
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); } } }