Saturday, October 07, 2006

Motion Detection

CamCapture was working perfectly and was uploading every picture it took to the internet. However, this feature was slowly becoming very annoying. CamCapture took pictures at fixed intervals of time, and most of the pictures had nothing but an empty room! It uploaded hundreds of picture of an empty room and bombarded my email account with useless notifications. I wanted my application to take pictures only when someone entered my room. In other words, I had to make my program detect motion!

Well at first, I had no clue where to start. I had to spend several hours thinking of ways to detect motion. I found some very helpful articles on the Internet related to motion detection. After some research, I came up with an algorithm which detected motion by comparing each frame captured by the camera with the previous one. If the frames were more or less the same, fine, but if they had differences above a certain limit, the program would trigger a motion detection event. So, for every frame captured, I compared the pixels with the previous frame for brightness changes. If the brightness change of a certain pixel exceeded a tolerance limit, the program would increment a counter. At the end of the scan, it checked the value of the counter and if it exceeded a certain limit, it triggered a motion detection event. There was actually a time when I scanned each and every pixel for brightness changes. However, I quickly realized that it wasn’t a very good idea because scanning each pixel took several seconds, and time is a crucial factor for quick motion detection. So, after some experimentation, I decided to scan each frame diagonally and check only a limited number of pixels for changes. This reduced the scan time considerably. This algorithm works quite nicely. Now, I won’t have to delete several useless CamCapture notifications from my mail account because I only get notifications when something actually moves. :) Click here to see a snapshot of my motion detector. Before I created the actual motion detector, I created a motion analyzer for testing different algorithms. Click here to see a snapshot of the motion analyzer.