Logical Indexing Example

From MATLAB Techniques for Image Processing by Steve Eddins.

Contents

Every MATLAB user is familiar with ordinary matrix indexing notation.

clear
A = magic(5)
A =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

A(2,3)
ans =

     7

A(2,3) extracts the 2nd row, 3rd column of the matrix A. You can extract more than one row and column at the same time:

A(2:4,3:5)
ans =

     7    14    16
    13    20    22
    19    21     3

When an indexing expression appears on the left-hand side of the equals sign, it's assignment:

A(5,5) = 100
A =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2   100

About every 13.6 days, someone asks this question on comp.soft-sys.matlab:

How do I replace all the NaNs in my matrix B with 0s?

This is generally followed 4.8 minutes later with this reply from one of the newsgroup regulars:

B(isnan(B)) = 0;

For example:

B = rand(3,3);
B(2,2:3) = NaN
B =

    0.2122    0.1750    0.8944
    0.0985       NaN       NaN
    0.8236    0.6660    0.7027

Replace the NaNs with zeros:

B(isnan(B)) = 0
B =

    0.2122    0.1750    0.8944
    0.0985         0         0
    0.8236    0.6660    0.7027

The expression

B(isnan(B))

is an example of logical indexing. Logical indexing is a compact and expressive notation that's very useful for many image processing operations.

Let's talk about the basic rules of logical indexing, and then we'll reexamine the expression B(isnan(B)).

If D is a logical array, then C(D) is a logical indexing expression.

"Logical" is one of the builtin types, or classes, of MATLAB matrices. Logical operators, such as == or >, produce logical matrices.

C = hilb(4)
C =

    1.0000    0.5000    0.3333    0.2500
    0.5000    0.3333    0.2500    0.2000
    0.3333    0.2500    0.2000    0.1667
    0.2500    0.2000    0.1667    0.1429

D = C > 0.4
D =

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

whos
  Name      Size            Bytes  Class      Attributes

  A         5x5               200  double               
  B         3x3                72  double               
  C         4x4               128  double               
  D         4x4                16  logical              
  ans       3x3                72  double               

You can see from the output of whos that the class of the variable D is logical. The logical indexing expression C(D) extracts all the values of C corresponding to nonzero values of D and returns them as a column vector.

C(D)
ans =

    1.0000
    0.5000
    0.5000

Now we know enough to break down the B(isnan(B)) example to see how it works.

B = rand(3,3);
B(2,2:3) = NaN;

nan_locations = isnan(B)
nan_locations =

     0     0     0
     0     1     1
     0     0     0

whos nan_locations
  Name               Size            Bytes  Class      Attributes

  nan_locations      3x3                 9  logical              

B(nan_locations)
ans =

   NaN
   NaN

B(nan_locations) = 0
B =

    0.1536    0.6797    0.7486
    0.9535         0         0
    0.5409    0.8092    0.5250

Connection to binary images

Functions in the Image Processing Toolbox, as well as the MATLAB functions imread and imwrite, follow the convention that logical matrices are treated as binary (black and white) images. For example, when you read a 1-bit image file using imread, it returns a logical matrix:

bw = imread('text.png');
imshow(bw)
whos bw
  Name        Size             Bytes  Class      Attributes

  bw        256x256            65536  logical              

This convention, together with logical indexing, makes it very convenient and expressive to use binary images as pixel masks for extracting or operating on sets of pixels.

Example: Histogram of foreground pixels

Given a gray-scale image and a binary segmentation, compute the histogram of just the foreground pixels in the image.

Here's our original image:

I = imread('rice.png');
imshow(I)

Here's the segmentation result (computed and saved earlier), represented as a binary image:

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

Now use the segmentation result as a logical index into the original image to extract the foreground pixel values.

foreground_pixels = I(bw);
whos foreground_pixels
  Name                       Size            Bytes  Class    Attributes

  foreground_pixels      17679x1             17679  uint8              

Finally, compute the histogram of the foreground pixels.

figure
imhist(foreground_pixels)

Or use logical indexing with the complement of the segmentation result to compute the histogram of the background pixels.

imhist(I(~bw))