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 & HelpVideo Capture,  Movie Playback,  Vision Libraries › brightness tracking with frame differencing
Page Index Toggle Pages: 1
brightness tracking with frame differencing (Read 2257 times)
brightness tracking with frame differencing
Jul 24th, 2008, 4:34pm
 
Hey,
Im trying to take the frame differencing example (of the video library not JMyron), and use brightness tracking with it, but I want to track the brightest pixel after the frame differencing, not of the overall video input. Since the frame differencing area is black where there is no movement, Im hoping this will allow me do basic motion tracking. What I cant figure out is how to check the brightest pixel on the new image that is being displayed rather than the video input. Code is below, and any help (or even telling me if this is a stupid idea and to give up!) would be great, thanks



import processing.video.*;

int brightestX;
int brightestY;



int numPixels;
int[] previousFrame;

Capture video;

void setup() {
 size(1200, 720); // Change size to 320 x 240 if too slow at 640 x 480
 // Uses the default video input, see the reference if this causes an error
 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];
 loadPixels();
}

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
   
   /*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
    int index = 0;
   video.loadPixels(); // Make its pixels[] array available
   
   int movementSum = 0; // Amount of movement in the frame
   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
     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
   
   
   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 - 20 > brightestValue) {
         brightestValue = pixelBrightness;
         brightestY = y;
         brightestX = x;
       }
       index++;
     }
   }
 
 }


 background(255);
 fill(255, 204, 0, 128);
 ellipse(brightestX, brightestY, 200, 200);


 }
}

Re: brightness tracking with frame differencing
Reply #1 - Jul 28th, 2008, 1:43am
 
Hi,

What kind of motion tracking are you trying to achieve? I have performed motion tracking in the past both by having performed optical flow on a difference image as well as having used blob detection on a difference image.

In order to get what you want working, instead of reading from video.pixels[], you should read from "pixels[]" instead, but before updatePixels() is called.
Re: brightness tracking with frame differencing
Reply #2 - Jul 31st, 2008, 9:57pm
 
Hi,

Thanks a million, that did what I was hoping it would, I know its not the best method, but its something (Im still finding my feet with programming!)

Im trying to achieve motion tracking of people walking by a window and using their position to affect particles using the Traer library. Because of the position light levels are really changeable so frame differencing wont work.

I was looking at your other posts on optical flow, in the Tracking Movement thread, and I think the idea of it could be exactly what I need, but the link you posted has expired or changed. Do you happen to have a link to where that has gone to?

Thanks for the help!
Re: brightness tracking with frame differencing
Reply #3 - Jul 31st, 2008, 11:01pm
 
Hi,

It does seem that the page is not there anymore. That is a big shame. Luckily, I still have some of the source.

This is for optical flow of the whole webcam image:

http://www.andybest.net/content/programming/optical_flow.pde

This is for moving about smaller regions (defined in code):

http://www.andybest.net/content/programming/optical_flow_region.pde

They both need the jMyron library.
Re: brightness tracking with frame differencing
Reply #4 - Aug 13th, 2008, 3:58pm
 
That worked fantastically. Thanks a million for the links and help.
Page Index Toggle Pages: 1