Main Content

bwlookup

Nonlinear filtering using lookup tables

Description

example

A = bwlookup(BW,lut) performs a 2-by-2 or 3-by-3 nonlinear neighborhood filtering operation on binary image BW. The neighborhood processing determines an integer index value used to access values in lookup table lut. The fetched lut value becomes the pixel value in output image A at the targeted position.

Examples

collapse all

Construct a lookup table lut such that the filtering operation places a 1 at the targeted pixel location in the input image only when all four pixels in the 2-by-2 neighborhood of BW are set to 1.

lutfun = @(x)(sum(x(:))==4);
lut = makelut(lutfun,2);

Load a binary image.

BW1 = imread("text.png");

Perform 2-by-2 neighborhood processing with 16-element vector lut.

BW2 = bwlookup(BW1,lut);

Display the original and eroded image.

h1 = subplot(1,2,1); imshow(BW1); title("Original Image")
h2 = subplot(1,2,2); imshow(BW2); title("Eroded Image")

Zoom in to see the effects of erosion on the text.

set(h1,Ylim=[1 64],Xlim=[1 64]);
set(h2,Ylim=[1 64],Xlim=[1 64]);

Input Arguments

collapse all

Binary image to be transformed by the nonlinear neighborhood filtering operation, specified as a 2-D logical matrix or 2-D numeric matrix. For numeric input, any nonzero pixels are considered to be 1 (true).

Lookup table of output pixel values, specified as a 16- or 512-element vector. The size of lut determines which of the two neighborhood operations is performed. You can use the makelut function to create a lookup table.

  • If lut contains 16 data elements, then the neighborhood matrix is 2-by-2.

  • If lut contains 512 data elements, then the neighborhood matrix is 3-by-3.

Output Arguments

collapse all

Output image, returned as a grayscale or binary image whose pixel values are determined by the content of the lookup table, lut. The output image A is the same size as the input image BW and the same data type as lut.

Algorithms

collapse all

The bwlookup function performs these steps to determine the value of each pixel in the processed image A:

  • Locate the pixel neighborhood in input image BW based on the coordinates of the target pixel in A. The function zero-pads border pixels of image BW when the neighborhood extends past the edge of BW.

  • Calculate an index, idx, based on the binary pixel pattern of the neighborhood.

  • Set the target pixel in A as the value of the lookup table at the index idx, in other words, the value of lut(idx).

For an example that demonstrates each step of the algorithm, see Look up Value of Sample Pixel.

2-by-2 Neighborhood Lookup

For 2-by-2 neighborhoods, there are four pixels in each neighborhood. Each binary pixel has two possible states, therefore the total number of permutations is 24 and the length of the lookup table lut is 16.

To find the value of the target output pixel at (row, column) coordinate (r,c), bwlookup uses the 2-by-2 pixel neighborhood in the input binary image BW whose top left pixel is at coordinate (r,c). The index idx into the lookup table is the weighted sum of the four pixels in the neighborhood, plus 1.

Pixel weights start with a weight of 1 for the top left pixel in the neighborhood, and increase as powers of two along rows then along columns, with a final weight of 8 for the bottom right pixel.

3-by-3 Neighborhood Lookup

For 3-by-3 neighborhoods, there are nine pixels in each neighborhood. Each binary pixel has two possible states, therefore the total number of permutations is 29 and the length of the lookup table lut is 256.

To find the value of the target output pixel at (row, column) coordinate (r,c), bwlookup uses the 3-by-3 pixel neighborhood in the input binary image BW centered on coordinate (r,c). The index idx into the lookup table is the weighted sum of the nine pixels in the neighborhood, plus 1.

Pixel weights start with a weight of 1 for the top left pixel in the neighborhood, and increase as powers of two along rows then along columns, with a final weight of 256 for the bottom right pixel.

Look up Value of Sample Pixel

This example shows how to determine the index into a lookup table for a target pixel based on the 2-by-2 neighborhood of the pixel.

Create a random 16-element lookup table. Set the random seed so that the results are reproducible.

rng("default")
lut = randperm(16)
lut = 1×16

     6     3    16    11     7    14     8     5    15     1     2     4    13     9    10    12

Create a small sample binary image.

BW = checkerboard(2,1,1)>0.5
BW = 4x4 logical array

   0   0   1   1
   0   0   1   1
   1   1   0   0
   1   1   0   0

Assume for this example that the targeted pixel location is location BW(2,1). Find the 2-by-2 neighborhood of the target pixel.

targetRow = 2;
targetColumn = 1;
neighborhood = BW(targetRow:targetRow+1,targetColumn:targetColumn+1)
neighborhood = 2x2 logical array

   0   0
   1   1

Calculate the index into the lookup table.

idx = 1 + 1*neighborhood(1,1) + 2*neighborhood(2,1) ...
        + 4*neighborhood(1,2) + 8*neighborhood(2,2)
idx = 11

Alternatively, you can represent the neighborhood as a binary string and convert the string to an index using the bin2dec function. Create the string by listing the values of the neighborhood in the order (2,2), (2,1), (1,2), (1,1). In other words, the value of neighborhood(2,2) contributes to the most significant bit, which comes first in the string. The value of neighborhood(1,1) contributes to the least significant bit, which comes last in the string.

binStr = "1 0 1 0";
idx = 1 + bin2dec(binStr)
idx = 11

Calculate the value of the target pixel by finding the value of the lookup table at the index idx.

targetPixelValue = lut(idx) 
targetPixelValue = 2

The above calculation predicts that output image A should contain the value 2 at the target position A(2,1). Confirm the prediction by performing the 2-by-2 nonlinear neighborhood filtering operation on the original binary image BW.

A = bwlookup(BW,lut)
A = 4×4

     6    13    12    11
     2     8    14     3
    12    11     6     6
    14     3     6     6

Extended Capabilities

Version History

Introduced in R2012b

expand all

See Also