Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

motion detection

$
0
0
hello everyone,

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)
if i comment this line, processing runs like a charm.

can someone please help me out with this please.

awaiting replies,
cheers


  1. import processing.video.*;
  2. // Variable for capture device
  3. Capture video;
  4. // Previous Frame

  5. PImage prevFrame;
  6. // How different must a pixel be to be a "motion" pixel

  7. float threshold = 300;

  8. void setup() {
  9.   size(320,240);

  10.    video = new Capture(this, width, height);
  11.    video.start() ;
  12.      
  13.   // Create an empty image the same size as the video
  14.   prevFrame = createImage(video.width,video.height,RGB);

  15. }

  16. void draw(){
  17.   
  18.   // Capture video
  19.   if (video.available()) {
  20.     // Save previous frame for motion detection!!
  21.     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!
  22.     prevFrame.updatePixels();
  23.     video.read();
  24.   }
  25.   //initialsing time
  26.   int sec = second(); // values from 0- 59
  27.    int m = minute();  // Values from 0 - 59
  28.   int h = hour();    // Values from 0 - 23
  29.  int d = day();    // Values from 1 - 31
  30.   int mo = month();  // Values from 1 - 12
  31.   int year = year();   // 2003, 2004, 2005, etc.
  32.   
  33.   
  34.   loadPixels();
  35.   video.loadPixels();
  36.   prevFrame.loadPixels();
  37.   
  38.   // Begin loop to walk through every pixel
  39.   for (int x = 0; x < video.width; x ++ ) {
  40.     for (int y = 0; y < video.height; y ++ ) {
  41.       
  42.       int loc = x + y*video.width;            // Step 1, what is the 1D pixel location
  43.       color current = video.pixels[loc];      // Step 2, what is the current color
  44.       color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color
  45.       
  46.       // Step 4, compare colors (previous vs. current)
  47.       float r1 = red(current); float g1 = green(current); float b1 = blue(current);
  48.       float r2 = red(previous); float g2 = green(previous); float b2 = blue(previous);
  49.       float diff = dist(r1,g1,b1,r2,g2,b2);
  50.       
  51.       pixels[loc]=video.pixels[loc]; 
  52.      
  53.       // Step 5, How we engage the detection part
  54.       // If the color at that pixel has changed, then there is motion at that pixel.
  55.       if (diff > threshold) { 
  56.         // If motion, save frame
  57.          println("Object detected at " +h+ "/" + m + "/" +sec+  "   "+ d+ "/"+ mo+ "/"+ year); // objected detected with time
  58.         saveFrame("Object-######.jpg");  // Saves each frame as object-000000.jpg, object-000001.jpg, etc.   
  59.           
  60.     } else {
  61.         continue;
  62.       }
  63.     }
  64.   }

  65.   updatePixels();
  66. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles