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.

296 <strong>Learning</strong> <strong>Processing</strong><br />

When you ultimately get <strong>to</strong> running this example, step out of the frame, click the mouse <strong>to</strong> memorize<br />

the background without you in it, and then step back in<strong>to</strong> the frame; you will see the result as seen in<br />

Figure 16.10.<br />

If this sketch does not seem <strong>to</strong> work for you at all, check and see what “ au<strong>to</strong>matic ” features are<br />

enabled on your camera. For example, if your camera is set <strong>to</strong> au<strong>to</strong>matically adjust brightness or<br />

white balance, you have a problem. Even though the background image is memorized, once the<br />

entire image becomes brighter or changes hue, this sketch will think all the pixels have changed<br />

and are therefore part of the foreground! For best results, disable all au<strong>to</strong>matic features on your<br />

camera.<br />

16.7<br />

Exercise 16-6: Instead of replacing the background with green pixels, replace it with another<br />

image. What values work well for threshold and what values do not work at all? Try<br />

controlling the threshold variable with the mouse.<br />

Motion Detection<br />

Today is a happy day. Why? Because all of the work we did <strong>to</strong> learn how <strong>to</strong> remove the background<br />

from a video gets us motion detection for free. In the background removal example, we examined each<br />

pixel’s relationship <strong>to</strong> a s<strong>to</strong>red background image. Motion in a video image occurs when a pixel color<br />

diff ers greatly from what it used <strong>to</strong> be one frame earlier. In other words, motion detection is exactly the<br />

same algorithm, only instead of saving a background image once, we save the previous frame of video<br />

constantly!<br />

Th e following example is identical <strong>to</strong> the background removal example with only one important change—<br />

the previous frame of video is always saved whenever a new frame is available.<br />

// Capture video<br />

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

// Save previous frame for motion detection!!<br />

prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height);<br />

video.read();<br />

}<br />

(Th e colors displayed are also changed <strong>to</strong> black and white and some of the variable names are diff erent,<br />

but these are trivial changes.)

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

Saved successfully!

Ooh no, something went wrong!