Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Motion Detection

$
0
0

What's wrong with my code. When I run the code, it pops out a grey windows nothing inside there. The camera should be activated and detect motion in the screen

import processing.video.*;
int numPixels;
int[] previousFrame;
Capture video;

void setup(){
  size (640, 480);
  video = new Capture (this, width, height);
  numPixels = video.width * video.height;
  previousFrame = new int[numPixels];

}

void draw() {
  if (video.available()){
    video.read();
    video.loadPixels();
    int movementSum=0;
    loadPixels();

    for (int i = 0; i < numPixels; i++){
      color currColor = video.pixels[i];
      color prevColor = previousFrame[i];

      int currR = (currColor >> 16) & 0xFF;
      int currG = (currColor >> 8) & 0xFF;
      int currB = currColor & 0xFF;

      int prevR = (currColor >> 16) & 0xFF;
      int prevG = (currColor >> 8) & 0xFF;
      int prevB = currColor & 0xFF;

      int diffR = abs(currR - prevR);
      int diffG = abs(currG - prevG);
      int diffB = abs(currB - prevB);

      movementSum += diffR + diffG + diffB;
      pixels[i] = color(diffR,diffG,diffB);
      previousFrame[i] = currColor;
    }

   if (movementSum > 0){
     updatePixels();
     println(movementSum);
   }
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles