## Steve on Image Processing with MATLABImage processing concepts, algorithms, and MATLAB

Note

Steve on Image Processing with MATLAB has been archived and will not be updated.

# Performance optimization for applylut

I thought I would finish my discussion of applylut and makelut by describing a performance optimization we implemented for version 6 (R2007b) of the Image Processing Toolbox.

A couple of years ago, a developer on our team wanted to know the details of the 'thin' option of bwmorph. This syntax "thins" connected strokes in a binary image. For example:

bw = imread('text.png');
imshow(bw)

bwt = bwmorph(bw, 'thin');
imshow(bwt)

With the syntax bwmorph(bw, 'thin', N), you can tell bwmorph to repeat the thinning operation N times. If you use bwmorph(bw, 'thin', Inf), then bwmorph will repeat the operation until the image stops changing.

bwt = bwmorph(bw, 'thin', Inf);
imshow(bwt)

So, in this long-ago conversation about bwmorph, I explained that many of the operations supported by bwmorph are implemented using applylut. About halfway through explaining applylut, I had a light bulb moment. It dawned on me that the thinning operation of bwmorph never changes a 0-valued pixel to a 1! So for any 0-valued input pixel, it is kind of pointless to examine the surrounding 3-by-3
neighborhood, which is what applylut does. One could just immediately determine that the corresponding output pixel should be 0.

There are other operations ('thicken', for example) that never change a 1-valued pixel to a 0.

After some further discussion, we realized that applylut could examine any lookup table to determine whether it had one of these properties. So that's what we did. Starting in version
6 of the toolbox, applylut examines the input lookup table to see if it always transforms a 0-valued pixel to 0, or a 1-valued pixel to 1. If so, special-case
code is used that skips the neighborhood lookup step for pixels that are 0 (or 1).

Here's the time required on my Windows laptop to apply the thinning operation to text.png. (You can find the function timeit on the MATLAB Central File Exchange.)

f = @() bwmorph(bw, 'thin', Inf);
timeit(f)
ans =

0.0044



Running with an older version of the Image Processing Toolbox, the same operation takes about 104 milliseconds. That's about
a factor of 23 reduction in execution time!

[Note added June 16, 2008:] The applylut optimization was not the only change made to speed up the 'thin' option of bwmorph. We also realized that work being done using several lookup tables could be combined in a single lookup table.

These optimizations went into the product in R2007b, which shipped in September 2007. Your mileage will certainly vary depending
on your particular data. The speedup you see depends on how many 0-valued (or 1-valued) pixels the image contains.

For your reference, here are my previous posts on applylut and makelut:

Published with MATLAB® 7.6

|