Priedas3

From Kulki

(Difference between revisions)

Current revision as of 19:07, 6 April 2006

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

}

Personal tools