Lookup tables for binary image processing—makelut and applylut
This is the second in a short series on using lookup tables for binary image neighborhood operations. See the first post for the basic idea.
The Image Processing Toolbox has two related functions:
- applylut - Process binary image using lookup table
- makelut - Utility function to help you construct a lookup table.
Since you have to have a lookup table in order to use applylut, let's start with makelut. It's syntax is:
lut = makelut(f, n)
f is a function handle, and n is either 2 or 3, depending on whether your lookup table is to be used with 2-by-2 or 3-by-3 neighborhoods.
For 3-by-3 neighborhoods, makelut will call your function handle f 512 times. In each call, makelut will pass as an argument to f one of the 512 possible 3-by-3 binary neighborhoods. f simply has to return the desired output value for a given neighborhood.
For example, suppose we want the output pixel to be 1 if and only if the corresponding input neighborhood looks like this:
1 0 0 0 1 0 0 0 1
The you would call makelut as follows:
pattern = [1 0 0; 0 1 0; 0 0 1]; lut = makelut(@(neighborhood) isequal(neighborhood, pattern), 3); size(lut)
ans = 512 1
Let's try that on a small sample image.
bw = eye(5) | fliplr(eye(5))
bw = 1 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1
bw2 = applylut(bw, lut)
bw2 = 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
So we see that only two input pixels, (2,2) and (4,4), have neighborhoods that exactly match the pattern.
Here's another example: Find all "endpoint" pixels. I'll define an endpoint pixel as a foreground pixel with exactly one foreground neighbor.
endpoint_fcn = @(nhood) (nhood(2,2) ~= 0) && (sum(nhood(:)) == 2); endpoint_lut = makelut(endpoint_fcn, 3);
Here's the result on sample image from above:
bw3 = applylut(bw, endpoint_lut)
bw3 = 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
Only the four corner pixels are endpoints.
In my next post on this topic, I'll use makelut and applylut to play Conway's Game of Life. Look for it in a week or so.
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.