06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

286 <strong>Learning</strong> <strong>Processing</strong><br />

fi g. 16.6<br />

Example 16-8: Brightness mirror<br />

// Each pixel from the video source is drawn as a<br />

// rectangle with size based on brightness.<br />

import processing.video.*;<br />

// Size of each cell in the grid<br />

int videoScale = 10;<br />

// Number of columns and rows in our system<br />

int cols, rows;<br />

// Variable for capture device<br />

Capture video;<br />

void setup() {<br />

size(640,480);<br />

// Initialize columns and rows<br />

cols = width/videoScale;<br />

rows = height/videoScale;<br />

smooth();<br />

// Construct the Capture object<br />

video = new Capture(this,cols,rows,15);<br />

}<br />

void draw() {<br />

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

video.read();<br />

}<br />

background(0);<br />

video.loadPixels();<br />

// Begin loop for columns<br />

for (int i = 0; i � cols; i + + ) { In order <strong>to</strong> mirror the image, the column<br />

// Begin loop for rows<br />

is reversed with the following formula:<br />

for (int j = 0; j � rows; j + + ) {<br />

// Where are we, pixel-wise?<br />

mirrored column � width – column – 1<br />

int x = i*videoScale;<br />

int y = j*videoScale;<br />

// Reversing x <strong>to</strong> mirror the image<br />

int loc = (video.width – i – 1) + j*video.width;

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

Saved successfully!

Ooh no, something went wrong!