Faster morphological reconstruction in R2009b
We've been working for a while now to make Image Processing Toolbox functions run faster. The R2009b release notes mention several performance improvements. We've gotten some feedback, though, that our release notes are pretty vague about the improvements. I can't argue with that impression. We tend to be vague because performance optimization is a very complex topic, and it can be quite difficult to characterize performance changes in a way that is brief, understandable, and accurate for every user's own hardware and data.
But I've decided to start posting more detailed information here about the performance improvements. I have more flexibility (and room!) here than we have with the release notes.
Today I'll tackle imreconstruct, which performs morphological reconstruction. Reconstruction is a very useful operation that I've written about here before. For example, see my post from last year on opening by reconstruction. Several other Image Processing Toolbox functions call imreconstruct, including imclearborder, imfill, imhmax, imhmin, imextendedmax, imextendedmin, and imimposemin.
Let me use the opening by reconstruction example as a benchmark case.
url = 'https://blogs.mathworks.com/images/steve/2008/book_text.png'; text = imread(url); imshow(text, 'InitialMagnification', 25) title('918-by-2018 image displayed at 25% magnification')
The example task was to find letters containing vertical strokes by eroding with a vertical structuring element and then performing reconstruction.
se = strel(ones(51, 1)); marker = imerode(text, se); text2 = imreconstruct(marker, text); imshow(text2, 'InitialMagnification', 25) title('Output image displayed at 25% magnification')
So how long does that call to imreconstruct take in R2009b? I'll use my function timeit, which you can download from the MATLAB Central File Exchange.
timeit(@() imreconstruct(marker, text))
ans = 0.0241
That time is about 45 times faster than the same operation performed in R2009a. Note that I'm running on two-core computer; the improved imreconstruct is multithreaded, so the performance improvement would be greater on a four-core computer, for example.
Now let's time gray-scale reconstruction. I'll make a 1024-by-1024 test image and compute a marker image by subtraction. This kind of operation is often used to suppress small peaks in an image.
I = repmat(imread('rice.png'), 4, 4);
marker = I - 20;
timeit(@() imreconstruct(marker, I))
ans = 0.0201
This time is about 30 times faster than R2009a, again running on my two-core laptop.
Now for some key caveats you should know. For now, the performance improvements described here only work for 2-D inputs that are uint8, uint16, or single, and only when the specified connectivity is 4 or 8. We'll be working in the future to extend the speed improvements to other inputs.
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.