Background Subtraction won't work in 2.0
in
Core Library Questions
•
11 months ago
I'm trying to switch over some code to 2.0. It uses back ground subtraction from the capture library. The problem is that when I press a key to set the background nothing happens.
Heres the code:
import processing.video.*;
int numPixels;
int[] backgroundPixels;
Capture video;
int cellSize=0;
void setup() {
// Change size to 320 x 240 if too slow at 640 x 480
size(500, 500);
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;
}
int a= 20000000;
int b= 25000000;
int c= 30000000;
int d= 35000000;
int e= 40000000;
int f= 45000000;
if ( presenceSum >= 0 && presenceSum <= a) {
cellSize=100;
}
if ( presenceSum >= a && presenceSum <= b) {
cellSize=75;
}
if ( presenceSum >= b && presenceSum <= c) {
cellSize=50;
}
if ( presenceSum >= c && presenceSum <= d) {
cellSize=25;
}
if ( presenceSum >= d && presenceSum <= e) {
cellSize=10;
}
if ( presenceSum >= e && presenceSum <= f) {
cellSize=10;
}
updatePixels(); // Notify that the pixels[] array has changed
println(presenceSum); // Print out the total amount of movement
for (int y=0; y <= width; y+=cellSize) {
for (int x=0; x <= height; x+=cellSize) {
color myColor = video.get(x, y); // grab the color from myPic at (a,b)
fill(myColor, 250); // set fill to use this new color
rect(x, y, cellSize, cellSize);
noStroke();
}
}
}
}
// 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);
}
1