motion detection
in
Core Library Questions
•
6 months ago
hello everyone,
This is my first post on this forum and there will be lots more in future
This is my first post on this forum and there will be lots more in future
i`m a beginner in processing and working on a motion detector using my webcam.
i`ve done my research and gathered some codings through Google.
MY real problem is that when an object is detected, i`m not able to save it on my pc. there`s some kind of error occurring. (line 65)
MY real problem is that when an object is detected, i`m not able to save it on my pc. there`s some kind of error occurring. (line 65)
if i comment this line, processing runs like a charm.
can someone please help me out with this please.
awaiting replies,
cheers
can someone please help me out with this please.
awaiting replies,
cheers
- 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 = 300;
- void setup() {
- size(320,240);
- video = new Capture(this, width, height);
- video.start() ;
- // 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); // Before we read the new frame, we always save the previous frame for comparison!
- prevFrame.updatePixels();
- video.read();
- }
- //initialsing time
- int sec = second(); // values from 0- 59
- int m = minute(); // Values from 0 - 59
- int h = hour(); // Values from 0 - 23
- int d = day(); // Values from 1 - 31
- int mo = month(); // Values from 1 - 12
- int year = year(); // 2003, 2004, 2005, etc.
- 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);
- pixels[loc]=video.pixels[loc];
- // Step 5, How we engage the detection part
- // If the color at that pixel has changed, then there is motion at that pixel.
- if (diff > threshold) {
- // If motion, save frame
- println("Object detected at " +h+ "/" + m + "/" +sec+ " "+ d+ "/"+ mo+ "/"+ year); // objected detected with time
- saveFrame("Object-######.jpg"); // Saves each frame as object-000000.jpg, object-000001.jpg, etc.
- } else {
- continue;
- }
- }
- }
- updatePixels();
- }
1