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

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

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

270 <strong>Learning</strong> <strong>Processing</strong><br />

// New color is difference between pixel and left neighbor<br />

float diff = abs(brightness(pix) - brightness(leftPix));<br />

pixels[loc] = color(diff);<br />

}<br />

}<br />

Example 15-12 is a simple vertical edge detection algorithm. When pixels diff er greatly from their<br />

neighbors, they are most likely “ edge ” pixels. For example, think of a picture of a white piece of paper<br />

on a black table<strong>to</strong>p. Th e edges of that paper are where the colors are most diff erent, where white<br />

meets black.<br />

In Example 15-12, we look at two pixels <strong>to</strong> fi nd edges. More sophisticated algorithms, however, usually<br />

involve looking at many more neighboring pixels. After all, each pixel has eight immediate neighbors: <strong>to</strong>p<br />

left, <strong>to</strong>p, <strong>to</strong>p right, right, bot<strong>to</strong>m right, bot<strong>to</strong>m, bot<strong>to</strong>m left, and left. See Figure 15.14 .<br />

Th ese image processing algorithms are often referred <strong>to</strong> as a “ spatial convolution. ” Th e process uses a<br />

weighted average of an input pixel and its neighbors <strong>to</strong> calculate an output pixel. In other words, that new<br />

pixel is a function of an area of pixels. Neighboring areas of diff erent sizes can be employed, such as a<br />

3 � 3 matrix, 5 � 5, and so on.<br />

Diff erent combinations of weights for each pixel result in various eff ects. For example, we “ sharpen ”<br />

an image by subtracting the neighboring pixel values and increasing the centerpoint pixel. A blur is<br />

achieved by taking the average of all neighboring pixels. (Note that the values in the convolution matrix<br />

add up <strong>to</strong> 1.)<br />

For example,<br />

Sharpen:<br />

– 1 – 1 – 1<br />

– 1 9 – 1<br />

– 1 – 1 – 1<br />

Blur:<br />

1/9 1/9 1/9<br />

1/9 1/9 1/9<br />

1/9 1/9 1/9<br />

its neighbor<br />

( x � 1, y)<br />

fi g. 15.14<br />

A pixel and its Friendly Neighbors<br />

a pixel<br />

at<br />

x, y<br />

its neighbor<br />

(x + 1, y + 1)

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

Saved successfully!

Ooh no, something went wrong!