Priedas0

From Kulki

Revision as of 20:42, 12 March 2006 by Admin (Talk | contribs)

<code> <pre> Code Listing 1: Frame Differencing. // Processing code for detecting and quantifying // the amount of movement in the video frame // using a simple Frame-Differencing technique. int video_width = 320; int video_height = 240; int num_pixels = (video_width * video_height); int previous_frame[]; //------------------------------------------- void setup(){ size(320, 240); previous_frame = new int [num_pixels]; // Allocate memory for storing the previous for (int i=0; i<num_pixels; i++){ // frame of video, and initialize this buffer previous_frame[i] = 0; // with blank values. } beginVideo(video_width, video_height, 30); } //------------------------------------------- void loop(){ int curr_color, prev_color; // Declare variables to store a pixel's color. float curr_r, curr_g, curr_b; // Declare variables to hold current color values. float prev_r, prev_g, prev_b; // Declare variables to hold previous color values. float diff_r, diff_g, diff_b; // Declare variables to hold computed differences. float movement_sum = 0; // This stores the amount of movement in this frame. for (int i=0; i<num_pixels; i++){ // For each pixel in the video frame, curr_color = video.pixels[i]; // Fetch the current color in that location, prev_color = previous_frame[i]; // and also the previous color from that spot. curr_r = red (curr_color); // Extract the red, green, and blue components curr_g = green (curr_color); // of the current pixel's color. curr_b = blue (curr_color); prev_r = red (prev_color); // Extract the red, green, and blue components prev_g = green (prev_color); // of the previous pixel's color. prev_b = blue (prev_color); diff_r = abs (curr_r - prev_r); // Compute the difference of the red values. diff_g = abs (curr_g - prev_g); // Compute the difference of the green values. diff_b = abs (curr_b - prev_b); // Compute the difference of the blue values. movement_sum += diff_r+diff_g+diff_b; // Add these differences to the running tally. pixels[i] = color(diff_r,diff_g,diff_b); // Render the difference image to the screen. previous_frame[i] = curr_color; // Swap the current information into the previous. } println(movement_sum); // Print out the total amount of movement } Code Listing 2: Background Subtraction. // Processing code for detecting the presence of people and objects in the frame using a // simple Background-Subtraction technique. Initialize the background by pressing a key. int video_width = 320; int video_height = 240; int num_pixels = (video_width * video_height); int background_img[]; //------------------------------------------- void setup(){ size(320, 240); beginVideo(video_width, video_height, 30); background_img = new int [num_pixels]; // Allocate memory for storing the background for (int i=0; i<num_pixels; i++){ // image, and initialize this buffer background_img[i] = 0; // with blank values. } } //------------------------------------------- void keyPressed(){ // When a key is pressed, capture the background for (int i=0; i<num_pixels; i++){ // image into the background_img buffer, by copying background_img[i] = video.pixels[i]; // each of the current frame's pixels into it. } } //------------------------------------------- void loop(){ int curr_color, bkgd_color; // Declare variables to store a pixel's color. float curr_r, curr_g, curr_b; // Declare variables to hold current color values. float bkgd_r, bkgd_g, bkgd_b; // Declare variables to hold previous color values. float diff_r, diff_g, diff_b; // Declare variables to hold computed differences. float presence_sum = 0; // This stores, for the current frame, the difference // between the current frame and the stored background for (int i=0; i<num_pixels; i++){ // For each pixel in the video frame, curr_color = video.pixels[i]; // Fetch the current color in that location, bkgd_color = background_img[i]; // and also the color of the background in that spot. curr_r = red (curr_color); // Extract the red, green, and blue components curr_g = green (curr_color); // of the current pixel's color. curr_b = blue (curr_color); bkgd_r = red (bkgd_color); // Extract the red, green, and blue components bkgd_g = green (bkgd_color); // of the background pixel's color. bkgd_b = blue (bkgd_color); diff_r = abs (curr_r - bkgd_r); // Compute the difference of the red values. diff_g = abs (curr_g - bkgd_g); // Compute the difference of the green values. diff_b = abs (curr_b - bkgd_b); // Compute the difference of the blue values. presence_sum += diff_r+diff_g+diff_b; // Add these differences to the running tally. pixels[i] = color(diff_r,diff_g,diff_b); // Render the difference image to the screen. } println (presence_sum); // Print out the total amount of movement } Code Listing 3: Brightness Thresholding. // Processing code for determining whether a test location (such as the cursor) // is contained within the silhouette of a dark object. int video_width = 320; int video_height = 240; int num_pixels = (video_width * video_height); //------------------------------------------- void setup(){ size(320, 240); beginVideo(video_width, video_height, 30); } //------------------------------------------- void loop(){ int black = color(0,0,0); // Declare some constants for colors. int white = color(255,255,255); int threshold = 127; int pix_val; // Declare variables to store a pixel's color. float pix_bri; // Split the image into dark and light areas: for (int i=0; i<num_pixels; i++){ // For each pixel in the video frame, pix_val = video.pixels[i]; // fetch the current color in that location, pix_bri = brightness (pix_val); // and compute the brightness of that pixel. if (pix_bri > threshold){ // Now "binarize" the video into black-and-white, pixels[i] = white; // depending on whether each pixel is brighter } else { // or darker than a threshold value. pixels[i] = black; } } // Test a location to see where it is contained. int test_val = get (mouseX, mouseY); // Fetch the pixel at the test location (the cursor), float test_bri = brightness (test_val); // and compute its brightness. if (test_bri > threshold){ // If the test location is brighter than threshold, fill (255,0,0); // draw a RED ellipse at the test location. ellipse (mouseX-10, mouseY-10, 20,20); } else { // Otherwise, fill (0,255,0); // draw a GREEN ellipse at the test location. ellipse (mouseX-10, mouseY-10, 20,20); } } Code Listing 4: Simple Object Tracking. // Processing code for tracking the brightest pixel in a live video signal int video_width = 320; int video_height = 240; //------------------------------------------- void setup(){ size(320, 240); beginVideo(video_width, video_height, 30); } //------------------------------------------- void loop(){ image(video, 0, 0, width, height); // Draw the webcam video onto the screen. // Declare some numbers to be computed later: int brightest_x = 0; // the x-coordinate of the brightest video pixel int brightest_y = 0; // the y-coordinate of the brightest video pixel float brightest_val = 0; // the brightness of the brightest video pixel // Now search for the brightest pixel: for (int y=0; y<video_height; y++){ // For each row of pixels in the video image, for (int x=0; x<video_width; x++){ // and for each pixel in the y'th row, int index = y*video_width + x; // compute each pixel's index in the video, int pix_val = video.pixels[index]; // fetch the color stored in that pixel, float pix_bri = brightness(pix_val); // and determine the brightness of that pixel. if (pix_bri > brightest_val){ // If that value is brighter than any previous, brightest_val = pix_bri; // then store the brightness of that pixel, brightest_y = y; // as well as its (x,y) location. brightest_x = x; } } } fill(255,0,0); // Set the fill color to red, and then ellipse( brightest_x-10, brightest_y-10, 20,20); // draw a circle at the brightest pixel. } </pre> </code>

Personal tools