We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpVideo Capture,  Movie Playback,  Vision Libraries › tracking multiple movement - frame differencing
Page Index Toggle Pages: 1
tracking multiple movement - frame differencing (Read 737 times)
tracking multiple movement - frame differencing
Mar 10th, 2009, 10:06am
 
Hi,

Using the Golan Levin's frame differencing example I am trying to monitor movement in several zones. But no luck.
I think I understand the technique but I don't fully get the application of it.
You need two buffer. One to put a zone of the previous video frame and and another one to compare with the current frame.
I though I could reduce the zone and then multiple the zones but like I said no success.

Don't know if I am clear, I look on the forum and into libraries but couldn't find a answer.

Thanks
Re: tracking multiple movement - frame differencin
Reply #1 - Mar 11th, 2009, 10:15am
 
I got it working. I did the tests with background subtraction ( as it's using the same technique but easier for doing tests).

I think that the code could be improve as values tends to stay in hi-values after reaching those hi values, so if any anyone wants give advice Wink

I am using PGraphics, have made the example as object oriented.

I found my solution with looking for another problem, that happen a lot. I was looking for how to export video with transparent background but didn't found the solution.

Re: tracking multiple movement - frame differencin
Reply #2 - Mar 11th, 2009, 10:15am
 
Quote:
import processing.video.*;

int numPixels;
int[] backgroundPixels;
Capture video;
Zone playerOne, playerTwo;

void setup() {
  // Change size to 320 x 240 if too slow at 640 x 480
  size(640, 480, P2D);

  video = new Capture(this, width, height, 24);
  video.settings();
  numPixels = video.width * video.height;
  // Create array to store the background image
  backgroundPixels = new int[numPixels];
  // Create Zone
  playerOne = new Zone(width, height, 200, 10, 10, "Player One");
  playerTwo = new Zone(width, height, 200, width-210, 10, "Player Two");
}

void draw() {

  if (video.available()) {
    video.read(); // Read a new video frame
    image(video, 0, 0); //Display video for knowing where you are

    video.loadPixels(); // Make the pixels of video available

    playerOne.detect();
    playerTwo.detect();

  }

  playerOne.display();
  playerTwo.display();

}

// When a key is pressed, capture the background image into the backgroundPixels

// buffer, by copying each of the current frame’s pixels into it.
void keyPressed() {
  video.loadPixels();
  arraycopy(video.pixels, backgroundPixels);
}








Re: tracking multiple movement - frame differencin
Reply #3 - Mar 11th, 2009, 10:16am
 
Quote:
class Zone{
  PGraphics z;
  int zoneSize, xPos, yPos,  zoneX, zoneY;
  String name;

  Zone(int w, int h, int s, int x, int y, String n){
    z = createGraphics(w, h, P2D);
    //Make pixel[] available
    z.loadPixels();

//Settings for position and size of the zone
    zoneSize = s;
    xPos = x;
    yPos = y;
    zoneX = xPos+zoneSize;
    zoneY = yPos+zoneSize;
    name = n;

  }

  void detect(){

    // Difference between the current frame and the stored background
    int presenceSum = 0;
    for (int x = xPos; x < zoneX; x++) { // For each X pixel in the video frame...
      for (int y = yPos; y < zoneY; y++) { // For each Y pixel in the video frame...
        // Fetch the current color in that location, and also the color
        // of the background in that spot
        int i = y*width+x; //To travel trough the pixel array
        color currColor = video.pixels[i];
        color bkgdColor = backgroundPixels[i];
        // Extract the red, green, and blue components of the current pixel’s color
        int currR = (currColor >> 16) & 0xFF;
        int currG = (currColor >> 8) & 0xFF;
        int currB = currColor & 0xFF;
        // Extract the red, green, and blue components of the background pixel’s color
        int bkgdR = (bkgdColor >> 16) & 0xFF;
        int bkgdG = (bkgdColor >> 8) & 0xFF;
        int bkgdB = bkgdColor & 0xFF;
        // Compute the difference of the red, green, and blue values
        int diffR = abs(currR - bkgdR);
        int diffG = abs(currG - bkgdG);
        int diffB = abs(currB - bkgdB);
        // Add these differences to the running tally
        presenceSum += diffR + diffG + diffB;
        // Render the difference image to the screen
        //pixels[i] = color(diffR, diffG, diffB);
        // The following line does the same thing much faster, but is more technical
        z.pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB;
      }
    }
    z.beginDraw();
    z.updatePixels(); // Notify that the pixels[] array has changed
    z.endDraw();

    int movement = presenceSum/10000;
    if(movement>0){
      println(name+" :  "+movement); // Print out the total amount of movement
    }

  }

//Display the PGraphics
  void display(){
    image(z, 0, 0);
  }

}











Re: tracking multiple movement - frame differencin
Reply #4 - Mar 11th, 2009, 10:34am
 
It actually works without the PGraphics :)
It even seems to be a bit quicker, but I still have my values getting stuck sometimes

//MAIN

Quote:
import processing.video.*;

int numPixels;
int[] backgroundPixels;
Capture video;
Zone playerOne, playerTwo;

void setup() {
  // Change size to 320 x 240 if too slow at 640 x 480
  size(640, 480, P2D);

  video = new Capture(this, width, height, 24);
  video.settings();
  numPixels = video.width * video.height;
  // Create array to store the background image
  backgroundPixels = new int[numPixels];
  // Create Zone: zone size, x, y, name
  playerOne = new Zone(200, 10, 10, "Player One");
  playerTwo = new Zone(200, width-210, 10, "Player Two");
  loadPixels();
}

void draw() {

  if (video.available()) {
    video.read(); // Read a new video frame
    //image(video, 0, 0); //Display video for knowing where you are

    video.loadPixels(); // Make the pixels of video available

    playerOne.detect();
    playerTwo.detect();

  }


}

// When a key is pressed, capture the background image into the backgroundPixels

// buffer, by copying each of the current frame’s pixels into it.
void keyPressed() {
  video.loadPixels();
  arraycopy(video.pixels, backgroundPixels);
}
Re: tracking multiple movement - frame differencin
Reply #5 - Mar 11th, 2009, 10:34am
 
//CLASS

Quote:
class Zone{
  PGraphics z;
  int zoneSize, xPos, yPos,  zoneX, zoneY;
  String name;

  Zone(int s, int x, int y, String n){

//Settings for position and size of the zone
    zoneSize = s;
    xPos = x;
    yPos = y;
    zoneX = xPos+zoneSize;
    zoneY = yPos+zoneSize;
    name = n;

  }

  void detect(){

    // Difference between the current frame and the stored background
    int presenceSum = 0;
    for (int x = xPos; x < zoneX; x++) { // For each X pixel in the video frame...
      for (int y = yPos; y < zoneY; y++) { // For each Y pixel in the video frame...
        // Fetch the current color in that location, and also the color
        // of the background in that spot
        int i = y*width+x; //To travel trough the pixel array
        color currColor = video.pixels[i];
        color bkgdColor = backgroundPixels[i];
        // Extract the red, green, and blue components of the current pixel’s color
        int currR = (currColor >> 16) & 0xFF;
        int currG = (currColor >> 8) & 0xFF;
        int currB = currColor & 0xFF;
        // Extract the red, green, and blue components of the background pixel’s color
        int bkgdR = (bkgdColor >> 16) & 0xFF;
        int bkgdG = (bkgdColor >> 8) & 0xFF;
        int bkgdB = bkgdColor & 0xFF;
        // Compute the difference of the red, green, and blue values
        int diffR = abs(currR - bkgdR);
        int diffG = abs(currG - bkgdG);
        int diffB = abs(currB - bkgdB);
        // Add these differences to the running tally
        presenceSum += diffR + diffG + diffB;
        // Render the difference image to the screen
        //pixels[i] = color(diffR, diffG, diffB);
        // The following line does the same thing much faster, but is more technical
        pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB;
      }
    }
   
    updatePixels(); // Notify that the pixels[] array has changed

    int movement = presenceSum/10000;
    if(movement>0){
      println(name+" :  "+movement); // Print out the total amount of movement
    }

  }

}
Page Index Toggle Pages: 1