Faster Blob Detector

It is amazing how much faster you can make something when the tight loops get called a zillion times a second.

Anyhow, the blob detector i posted yesterday was the first clean, working version. But it hadnt really been optimized (never optimize before it’s time). So i went back in and looked at the tightest loop: specifically scanSrc in the detector. If you looked at the code you can see that it is written to be more programmer-friendly than fast. I tend to think that whenever you can afford to, programmer-friendliness is a good trade off for minor performance loss. However, in this case there were a few spots that needed to be changed.

The biggest (performance wise) change is the movement of the initial ‘goodness’ check into the loop proper. also instead of calling validLoopPixel, i just put the threshold check right there. It is generally considered bad coding to put the same code in two places. but this is a concession I am willing to make. Just taking out the call to validLoopPixel shaved off 20 or 30% of the time it was taking in that loop (let’s see. 640×480 pixels times 30 frames a second = 9216000 runs through that loop per second. anything you can do to shave off even the tiniest amount of time in that tight loop will yield great performance payback)

So, basically that one change made the biggest performance change, but i changed a few other things too: I added a regionOfInterest rectangle into the mix (to cut down on the number of times through the loop). and added a class method to the image processor to make the ROI rect in the image (ie the black pixel border that keeps the blob detection algorithm from going out of the ROI)

Thats about it as far as code: here is the tar:


Now, this whole tar with an incrementing value in the name is great and all, but it is not a good way to release code. Luckily for me, I had a long iChat with Pawel from the NUIGroup, and he is helping me get this stuff up onto the opentouch google code repository. While I have used SVN before, I havent used the google code repositories yet. I am going to attack that problem later today. I will be putting up the same code that is in BBTouch.2 initially, but then all the updates will go to the google repository.

This entry was posted in BBTouch, code, FTO xpost, multitouch. Bookmark the permalink.

2 Responses to Faster Blob Detector

  1. Pingback: Some thoughts on BBTouch optimization at

  2. Ben says:

    wow, i got a pingback from.. myself! neato.

Leave a Reply