mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
blue book examples
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
// Quantify the amount of movement in the video frame using frame-differencing
|
||||
import processing.video.*;
|
||||
|
||||
int numPixels;
|
||||
int[] previousFrame;
|
||||
Capture video;
|
||||
void setup() {
|
||||
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
|
||||
video = new Capture(this, width, height, 24);
|
||||
numPixels = video.width * video.height;
|
||||
// Create an array to store the previously captured frame
|
||||
previousFrame = new int[numPixels];
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (video.available()) {
|
||||
// When using video to manipulate the screen, use video.available() and
|
||||
// video.read() inside the draw() method so that it's safe to draw to the screen
|
||||
video.read(); // Read the new frame from the camera
|
||||
video.loadPixels(); // Make its pixels[] array available
|
||||
int movementSum = 0; // Amount of movement in the frame
|
||||
loadPixels();
|
||||
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
|
||||
color currColor = video.pixels[i];
|
||||
color prevColor = previousFrame[i];
|
||||
// Extract the red, green, and blue components from current pixel
|
||||
int currR = (currColor >> 16) & 0xFF; // Like red(), but faster (see p. 673)
|
||||
int currG = (currColor >> 8) & 0xFF;
|
||||
int currB = currColor & 0xFF;
|
||||
// Extract red, green, and blue components from previous pixel
|
||||
int prevR = (prevColor >> 16) & 0xFF;
|
||||
int prevG = (prevColor >> 8) & 0xFF;
|
||||
int prevB = prevColor & 0xFF;
|
||||
// Compute the difference of the red, green, and blue values
|
||||
int diffR = abs(currR - prevR);
|
||||
int diffG = abs(currG - prevG);
|
||||
int diffB = abs(currB - prevB);
|
||||
// Add these differences to the running tally
|
||||
movementSum += diffR + diffG + diffB;
|
||||
// Render the difference image to the screen
|
||||
pixels[i] = color(diffR, diffG, diffB);
|
||||
// The following line is much faster, but more confusing to read
|
||||
//pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
|
||||
// Save the current color into the 'previous' buffer
|
||||
previousFrame[i] = currColor;
|
||||
}
|
||||
// To prevent flicker from frames that are all black (no movement),
|
||||
// only update the screen if the image has changed.
|
||||
if (movementSum > 0) {
|
||||
updatePixels();
|
||||
println(movementSum); // Print the total amount of movement to the console
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// Detect the presence of people and objects in the frame using a simple
|
||||
// background-subtraction technique. To initialize the background, press a key.
|
||||
import processing.video.*;
|
||||
|
||||
int numPixels;
|
||||
int[] backgroundPixels;
|
||||
Capture video;
|
||||
|
||||
void setup() {
|
||||
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 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);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// Determines whether a test location (such as the cursor) is contained within
|
||||
// the silhouette of a dark object
|
||||
import processing.video.*;
|
||||
|
||||
color black = color(0);
|
||||
color white = color(255);
|
||||
int numPixels;
|
||||
Capture video;
|
||||
|
||||
void setup() {
|
||||
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
|
||||
strokeWeight(5);
|
||||
video = new Capture(this, width, height, 24);
|
||||
numPixels = video.width * video.height;
|
||||
noCursor();
|
||||
smooth();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (video.available()) {
|
||||
video.read();
|
||||
video.loadPixels();
|
||||
int threshold = 127; // Set the threshold value
|
||||
float pixelBrightness; // Declare variable to store a pixel's color
|
||||
// Turn each pixel in the video frame black or white depending on its brightness
|
||||
loadPixels();
|
||||
for (int i = 0; i < numPixels; i++) {
|
||||
pixelBrightness = brightness(video.pixels[i]);
|
||||
if (pixelBrightness > threshold) { // If the pixel is brighter than the
|
||||
pixels[i] = white; // threshold value, make it white
|
||||
}
|
||||
else { // Otherwise,
|
||||
pixels[i] = black; // make it black
|
||||
}
|
||||
}
|
||||
updatePixels();
|
||||
// Test a location to see where it is contained. Fetch the pixel at the test
|
||||
// location (the cursor), and compute its brightness
|
||||
int testValue = get(mouseX, mouseY);
|
||||
float testBrightness = brightness(testValue);
|
||||
if (testBrightness > threshold) { // If the test location is brighter than
|
||||
fill(black); // the threshold set the fill to black
|
||||
}
|
||||
else { // Otherwise,
|
||||
fill(white); // set the fill to white
|
||||
}
|
||||
ellipse(mouseX, mouseY, 20, 20);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Tracks the brightest pixel in a live video signal
|
||||
import processing.video.*;
|
||||
|
||||
Capture video;
|
||||
|
||||
void setup() {
|
||||
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
|
||||
video = new Capture(this, width, height, 30);
|
||||
noStroke();
|
||||
smooth();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (video.available()) {
|
||||
video.read();
|
||||
image(video, 0, 0, width, height); // Draw the webcam video onto the screen
|
||||
int brightestX = 0; // X-coordinate of the brightest video pixel
|
||||
int brightestY = 0; // Y-coordinate of the brightest video pixel
|
||||
float brightestValue = 0; // Brightness of the brightest video pixel
|
||||
// Search for the brightest pixel: For each row of pixels in the video image and
|
||||
// for each pixel in the yth row, compute each pixel's index in the video
|
||||
video.loadPixels();
|
||||
int index = 0;
|
||||
for (int y = 0; y < video.height; y++) {
|
||||
for (int x = 0; x < video.width; x++) {
|
||||
// Get the color stored in the pixel
|
||||
int pixelValue = video.pixels[index];
|
||||
// Determine the brightness of the pixel
|
||||
float pixelBrightness = brightness(pixelValue);
|
||||
// If that value is brighter than any previous, then store the
|
||||
// brightness of that pixel, as well as its (x,y) location
|
||||
if (pixelBrightness > brightestValue) {
|
||||
brightestValue = pixelBrightness;
|
||||
brightestY = y;
|
||||
brightestX = x;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
// Draw a large, yellow circle at the brightest pixel
|
||||
fill(255, 204, 0, 128);
|
||||
ellipse(brightestX, brightestY, 200, 200);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user