Making a Motion Detector

Capture isn’t just about writing data to disk. You can grab images as they come in and analyze or manipulate them.

Tip

A great example of “grabbing different” is Lisa Lippincott’s ScrollPlate, a demo shown at ADHOC 2004. She used her iSight camera as a scroll wheel, by holding up a Styrofoam plate with either a large green arrow (for up) or a large red arrow (for down). Her code presumably grabbed from the camera, looked at the grabbed image for an abundance of green or red, and scrolled the top window in response.

This example offers a simple motion detector, which will display an alarm message if two subsequent grabs are markedly different. The idea is that if the camera is not moving, a significant difference between two subsequent grabs indicates that something in view of the camera has moved.

How do I do that?

In this case, what you want to do is to set up video-only capture, but instead of saving the data to disk, you do a little bit of image processing each time you idle() . Specifically, there is a method in QTImage called getSimilarity( ), which compares two images (one as a QDGraphics and the other as an EncodedImage). Motion—objects entering, exiting, or significantly moving within the camera’s field of vision—can be understood as a significant difference between two consecutive grabbed images.

Note

See Chapter 5 for more on QTImage, QDGraphics, and EncodedImage.

Unfortunately, this requires jumping through quite a bit of QuickDraw hoops once ...

Get QuickTime for Java: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.