Speed up your image registration (template matching) code in MATLAB
Friday, April 21st, 2006Recently, I’ve encounter the problem of finding a template in an image, which is a common issue in image registration or computer vision.
With the built-in function “normxcorr2″ in MATLAB, it is rather easy to write a program to do this. However, the performance is not that great.
Luckily, I came across the following discussion by Daniel Eaton:
“How to do Normalized Cross-Correlation Fast”
By wrapping the C++ code from OpenCV project and making it available as a MATLAB MEX-file, Daniel claims that he achieved a huge performance boost.
I was a bit suspicious at first, but decided to give it a shot. As I am using a n Apple PowerMac G5, I found out that I need to compile the MEX-file on Mac OS X, which turns out not be a trivial task (see my dedicated post).
Anyway, the outcome really surprised me. I can assure you now, that it is well worth the time and trouble to compile the MEX-file!
By embedding a random grayscale pattern in a larger image, I was able to benchmark the performance on an Apple PowerMac G5 (Dual 2.5GHz PowerPC CPUs). Take a look:
The template stays the same (100×100 pixels), while the size of search image is increasing (from 200×200 to 800×800, indicated by the X-axis). The Y-axis shows the cpu time. Clearly, there is a speedup of 15x – 20x, using the compiled MEX-file over the built-in “normxcorr2″!
Life is better, right?