07.01.2013 Views

Fractal.Invaders, Substrate (Interview with Jared Tarbell) - Processing

Fractal.Invaders, Substrate (Interview with Jared Tarbell) - Processing

Fractal.Invaders, Substrate (Interview with Jared Tarbell) - Processing

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 4: Brightness tracking<br />

// Tracks the brightest pixel in a live video signal<br />

import processing.video.*;<br />

Capture video;<br />

void setup(){<br />

size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480<br />

video = new Capture(this, width, height, 30);<br />

noStroke();<br />

smooth();<br />

}<br />

void draw() {<br />

if (video.available()) {<br />

video.read();<br />

image(video, 0, 0, width, height); // Draw the webcam video onto the screen<br />

int brightestX = 0; // X-coordinate of the brightest video pixel<br />

int brightestY = 0; // Y-coordinate of the brightest video pixel<br />

float brightestValue = 0; // Brightness of the brightest video pixel<br />

// Search for the brightest pixel: For each row of pixels in the video image and<br />

// for each pixel in the yth row, compute each pixel's index in the video<br />

video.loadPixels();<br />

int index = 0;<br />

for (int y = 0; y < video.height; y++) {<br />

for (int x = 0; x < video.width; x++) {<br />

// Get the color stored in the pixel<br />

int pixelValue = video.pixels[index];<br />

// Determine the brightness of the pixel<br />

float pixelBrightness = brightness(pixelValue);<br />

// If that value is brighter than any previous, then store the<br />

// brightness of that pixel, as well as its (x,y) location<br />

if (pixelBrightness > brightestValue){<br />

brightestValue = pixelBrightness;<br />

brightestY = y;<br />

brightestX = x;<br />

}<br />

index++;<br />

}<br />

}<br />

// Draw a large, yellow circle at the brightest pixel<br />

fill(255, 204, 0, 128);<br />

ellipse(brightestX, brightestY, 200, 200);<br />

}<br />

}<br />

560 Extension 3: Vision

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!