Detecting brightness in live video

[ update: There's an evolution of this code, now using objects ]

I’m working on a group project right now, where we need to detect the brightness of the incoming video, sorting the place where that bright spot is, and then doing something else (that sth else will come later).

So far, I seem to have gotten it to work with a simple sketch.

This is the code so far:

/*
 
 Capture live video, and sort it like a checkerboard (large pixels), 
 then detect bright pixels according to a predefined threshold.
 
 Then act upond, sending serial data.
 
 Sergio Majluf
 ITP - October 2012
 
 */

import processing.video.*;

int pixelSizeX = 64 ;
int pixelSizeY = 48 ;
int gutter = 0;
int threshold = 150; // rise to make more sensitive 

Capture video;

void setup() {
  size(640, 480);
  noStroke();
  video = new Capture (this, width, height);
  video.start();
  background(0);
}

void draw() {

  if (video.available()) {

    video.read();
    video.loadPixels();

    for (int x = 0 ; x < width; x += (pixelSizeX+gutter)) {
      for (int y = 0 ; y < height; y += (pixelSizeY+gutter)) {

        // this is from Mirror2 Example
        int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image

        // Each rect is colored white with a size determined by brightness
        color c = video.pixels[loc];

        // fill(video.pixels[x+ y*width]);

        if (brightness(c) > threshold) fill(255);
        if (brightness(c) <= threshold) fill(0);

        // fill(brightness(c));

        rect (x, y, pixelSizeX, pixelSizeY);
        
      }
    }

  }

}

One Response to “Detecting brightness in live video”

Leave a Reply