Video Capturing Brightest Pixel Sketch

Daniel's Interactive Map

Heb vorig jaar al gewerkt met video tracking een brightest pixel, waar die dan een cirkel tekent. Maar wat als je beweging wil weergeven 'over time', oftewel bv een cirkel op ieder punt waar een vorige brightest pixel was. Of natuurlijk eerder een lijn tussen een punt en het vorige punt, bijv:
line(brightestX, brightestY, previousBrightestX, previousbrightestY)

Uiteindelijk zou je dan een netwerk van lijnen krijgen van alle bewegingen tot dan toe. moet je hier iets van een array voor gebruiken (om de value van de previousbrightest x en y op te slaan), of niet? Anders gezegd: hoe zou je die previous brightestX/Y moeten definieren?

import processing.video.*;
 
Capture video;
 
void setup() {
  size(640, 480); // 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, 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
    background(255, 50);
    fill(255, 0, 0, 128);
    ellipse(brightestX, brightestY, 200, 200);
  }
}

— Indeed, you'll need to use an array if you want to store all the previous brightest pixel locations. Take a look at two more examples I posted in the code section:

Especially the second example will help you with storing the previous brightest pixel locations.

And of course there is a limit to the amount of data that you can store in memory like this. (You have to set the size of the array in advance). If you would like to store ALL the history data of this brightest pixel, you'll eventually have to write this data back to file (because your harddisk is much bigger than your RAM memory). See Processing's PrintWriter reference to learn you can create an output file with createWriter(), store data in that file using the println() function, flush the file to disk and close the file.

Improved code (Extended help):

import processing.video.*;
Capture video;
 
/* Declaring the brightestArrays and the indexPosition */
int num = 50; // num is the size of the arrays
int[] brightestArrayX = new int[num];
int[] brightestArrayY = new int[num];
int indexPosition = 0;
 
void setup() {
  size(640, 480); // 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, 30);
  noStroke();
  smooth();
}
 
void draw() {
  if (video.available()) {
    video.read();
    image(video, 0, 0, width, height); // Draw the webcam video onto the screen
    /* instead of declaring the int's brightestX and brightestY here, we use an array, declared at the top */
    //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;
          /* Store the brightest pixel in an array instead of in an int */
          brightestArrayX[indexPosition] = x;
          brightestArrayY[indexPosition] = y;
          // brightestY = y;
          // brightestX = x;
        }
        index++;
      }
    }
    indexPosition = (indexPosition + 1) % num;
    // Draw a large, yellow circle at the brightest pixel
    background(255, 50);
    fill(255, 0, 0, 128);
    /* Draw the trace of all previous brightest pixels */
      for (int i = 0; i < num; i++) {
      // Set the array position to read
      int pos = (indexPosition + i) % num;
      float radius = (num-i) / 2.0;
      ellipse(brightestArrayX[pos], brightestArrayY[pos], radius, radius);
    }
    //ellipse(brightestX, brightestY, 200, 200);
  }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License