Loading...
Logo
Processing Forum
Hi,

I want to use motion detect to eraze the bacground. So far it worked, but the result draw the edges of the subject only.  Can I use this strategy in order to draw a more unite shape like a shadow.  In order words, can I erase the background and only keep the moving subject in solid colours.

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 16-13: Simple motion detection

import processing.video.*;
// Variable for capture device
Capture video;
// Previous Frame
PImage prevFrame;

color black2 = color(20,20,20,80);
color black = color(20,20,20,20);
color white = color(230,230,230);
// How different must a pixel be to be a "motion" pixel
float threshold = 80;
int numPixels;
void setup() {
  size(450,450);
   
 // background(250);
  video = new Capture(this, width, height,60);
 //filter(THRESHOLD,0.02);
  filter(BLUR,3);
 // frameRate(60);
  // Create an empty image the same size as the video
  prevFrame = createImage(video.width,video.height,RGB);
}

void draw() {
   float pixelBrightness;
 
   //filter(GRAY);
//
  // 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();
  }
 
  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 ++ ) {
     //filter(THRESHOLD,0.5);
      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(g1,b1,r1,g2,b2,r2);
//     
//      // Step 5, How different are the colors?
//      // If the color at that pixel has changed, then there is motion at that pixel.
      if (diff > threshold) {
//        // If motion, display black
//        pixels[loc] = color(45,45,45,40);
//      } else {
//        // If not, display white
//        pixels[loc] = color(250,250,250,50);
//      }
//    }
//  }
//filter(POSTERIZE, 4);
// for (int i = 0; i < numPixels; i++) {
//      pixelBrightness = brightness(video.pixels[i]);
//      if (pixelBrightness >threshold) { // If the pixel is brighter than the
        pixels[loc] = black; // threshold value, make it white
      }
      else { // Otherwise,
        pixels[loc] = white; // make it black
      }
   
  if (diff > threshold+100) {
//        // If motion, display black
//        pixels[loc] = color(45,45,45,40);
//      } else {
//        // If not, display white
//        pixels[loc] = color(250,250,250,50);
//      }
//    }
//  }
//filter(POSTERIZE, 4);
// for (int i = 0; i < numPixels; i++) {
//      pixelBrightness = brightness(video.pixels[i]);
//      if (pixelBrightness >threshold) { // If the pixel is brighter than the
        pixels[loc] = black2; // threshold value, make it white
      }
      }
  updatePixels();
}}