Steve on Image Processing

May 7th, 2008

Lookup tables for binary image processing

Today I'm introducing a short series about using lookup tables to implement neighborhood operations in binary images. This is a fast implementation technique that works for small neighborhoods, typically 2-by-2 or 3-by-3.

My tentative plan is to do three posts on this algorithm topic:

1. Explain the basic idea (today)

2. Show some examples using Image Processing Toolbox functions

3. Explain an algorithm optimization we recently put in the toolbox.

Let's talk 3-by-3 binary neighborhoods. Here's one:

bw = [0 0 1; 1 1 0; 1 0 0]
bw =

     0     0     1
     1     1     0
     1     0     0

Here's another:

bw = [1 0 0; 1 0 0; 0 1 0]
bw =

     1     0     0
     1     0     0
     0     1     0

OK, that might get old real fast.

QUESTION: How many different 3-by-3 binary neighborhoods are there?

Each pixel in the neighborhood can take on only two values, and there are nine pixels in the neighborhood, so the answer is:

2^9
ans =

   512

That's not so many different possibilities, although it would certainly be tedious to write them all out by hand! Is there an easy way to generate them?

I'm sure there are lots of reasonable methods. One way, used by the Image Processing Toolbox function makelut, is to use the 9-bit binary representation of the integers from 0 to 511.

label = 5;
bin = dec2bin(label, 9)
bin =

000000101

bin is a 9-character string. A logical comparison and a reshape turns it into a binary neighborhood:

bw = reshape(bin == '1', 3, 3)
bw =

     0     0     1
     0     0     0
     0     0     1

Just repeat this for the other 511 integers in the range [0,511] and you'll have all possible 3-by-3 neighborhoods. Compute the output of your operation for each one and save the result in a table.

Then use this procedure to implement your operation:

For each input pixel:
  Extract the 3-by-3 neighborhood
  Compute the table index corresponding to the neighborhood
  Insert the table value at that index into the output image

Computing the table index corresponding to a given neighborhood can be done by using bin2dec. Note that bin2dec takes a string, so we have to do a little work first to convert the binary neighborhood to a string of '0' and '1' characters.

str = repmat('0', 1, 9);
str(bw(:)') = '1';
bin2dec(str)
ans =

     5

And that's the heart of the lookup table algorithm for binary image neighborhood processes.

In my next post on this topic, I'll introduce the Image Processing Toolbox functions makelut and applylut and show examples illustrating their use.


Get the MATLAB code

Published with MATLAB® 7.6

3 Responses to “Lookup tables for binary image processing”

  1. Chen Sagiv replied on :

    Hi Steve,

    Very Nice !

    Seems that some connection to morphology can be expected. Looking forwards for the next post.

    Best,

    Chen

  2. Sai BPO Services replied on :

    Thanks!!
    Waiting for 2nd & 3rd posts
    Regards
    Sai BPO Services
    www.saibposervices.co.uk/photoshop-Image_masking.aspx

  3. Steve replied on :

    Chen, Sai—I’m glad I found a topic you’re so interested in! Chen, the function bwmorph makes extensive use of applylut.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Steve: Kezia—Try imrotate.
  • kezia: steve, how to perform rotation of structuring element by 15 degrees. kindly answer my question. thank u kezia...
  • Steve: Tasha—I only accept comments that are relevant to the particular blog post or are questions or comments...
  • Tasha: Steve,I send you a comment here but still didn’t get any reply yet.I did not see my comment posted here...
  • Steve: Carsten—Thanks for your input.
  • Carsten: Another vote for either imtranslate.m, or at least a blurb in the imtransform help why pure translation...
  • Loren Shure: If you look towards the end of the fftfilt program, you will see that there’s a check to see if...
  • Steve: Sonja—My imwritesize submission on the MATLAB Central File Exchange might be helpful. It was posted...
  • Steve: Grant—Sorry, but it won’t be for R2010a. That development deadline has already passed.
  • Sonja: My publisher is wanting images for a new book to be 300 dpi. Only 5 of the 19 images are 300, the rest are...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.