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 & HelpSyntax Questions › Difference Imaging/Background subtraction
Page Index Toggle Pages: 1
Difference Imaging/Background subtraction (Read 492 times)
Difference Imaging/Background subtraction
Feb 26th, 2008, 6:31pm
 
Hey all,

I'm trying to take the example sketch provided by Processing of Background Subtraction and make it black and white rather than RGB. So that the background is black and all interruptions become solid white.

Code:

import processing.video.*;

int numPixels;
int[] backgroundPixels;
Capture video;

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

video = new Capture(this, width, height, 24);
numPixels = video.width * video.height;
// Create array to store the background image
backgroundPixels = new int[numPixels];
// Make the pixels[] array available for direct manipulation
loadPixels();
}

void draw() {
if (video.available()) {
video.read(); // Read a new video frame
video.loadPixels(); // Make the pixels of video available
// Difference between the current frame and the stored background
int presenceSum = 0;
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
// Fetch the current color in that location, and also the color
// of the background in that spot
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
println(presenceSum); // Print out the total amount of movement
}
}

// 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);
}


Also, is there a way to track  how many people are in each frame? I.E. There are 2 people in the frame *person walks away* there is one person in the frame.

Much love,
TSG
Re: Difference Imaging/Background subtraction
Reply #1 - Feb 27th, 2008, 10:35pm
 
Code:

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

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

video = new Capture(this, width, height, 24);
numPixels = video.width * video.height;
// Create array to store the background image
backgroundPixels = new int[numPixels];
// Make the pixels[] array available for direct manipulation
loadPixels();
}

void draw() {
if (video.available()) {
video.read(); // Read a new video frame
video.loadPixels(); // Make the pixels of video available
// Difference between the current frame and the stored background
int presenceSum = 0;
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
// Fetch the current color in that location, and also the color
// of the background in that spot
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

// This is the change I've made:
int threshold = 50;

pixels[i] = (diffR + diffG + diffB) > threshold ? color(255) : color(0);

// the ? : stuff if you haven't seen it before means -> (is it like this)? yes : no;
// Because a web cam image tends to be grainy, it means that what we get isn't so clear
// cut as we would expect - so you use a threshold value to tweak for the output you want

}
updatePixels(); // Notify that the pixels[] array has changed
println(presenceSum); // Print out the total amount of movement
}
}

// 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: Difference Imaging/Background subtraction
Reply #2 - Feb 27th, 2008, 10:40pm
 
Yes you can use the filter() command, but I was having trouble getting it to make the background as black as possible with my webcam.

You're not going to be able to detect if two people are there if one is standing in front of the other, or if they stand side by side. But that's vision for you.

You're also best off looking at some of the vision libraries mentioned in the reference section. Try combining those with either the change in the code I've given you or some of the filter() commands and you should be able to get a good head start on your idea.
Re: Difference Imaging/Background subtraction
Reply #3 - Feb 28th, 2008, 1:29am
 
Thanks so much. Like you said...it's not perfect...but it's definately a 22 mile head start in a marathon :-P

I appreciate you helping out a n00b.

~TSG
Re: Difference Imaging/Background subtraction
Reply #4 - Feb 28th, 2008, 1:34pm
 
I've just noticed the cross-post x-( since I answered on the other one:

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Video;action=display;num=1204085145;start=1#1

This is a really annoying thing to do...
Re: Difference Imaging/Background subtraction
Reply #5 - Feb 28th, 2008, 4:57pm
 
^ He's right - no cross posting, it ruins search results and the mods have to clean up your rubbish.

Don't do it again thesprucegoose

Here's jorgecardoso's comment in the other thread:

jorgecardoso wrote on Feb 27th, 2008, 11:03am:
Hi,

You might want to check my code and examples: http://jorgecardoso.eu/blog/index.php/plugin/tag/Background+Subtraction

jorge


Hopefully they can remove the other one now.
Re: Difference Imaging/Background subtraction
Reply #6 - Feb 28th, 2008, 7:47pm
 
Sorry. I realized there was a forum for specifically video questions after I posted in syntax. I was simply trying to get better feedback. I didn't realize it was "annoying".

TSG
Re: Difference Imaging/Background subtraction
Reply #7 - Feb 29th, 2008, 10:33am
 
No worries, we just get cross posting a lot when new people come along. Doesn't mean you're not welcome.

I usually keep up with the board here:

http://processing.org/discourse/yabb_beta/YaBB.cgi?action=recent

So cross posting really isn't necessary.
Page Index Toggle Pages: 1