Steve on Image Processing

January 28th, 2008

Logical indexing

This is the first post in a short series on index techniques that are particularly useful for image processing in MATLAB. I'll start with logical indexing today. Later I'll cover linear indexing, and then a technique I like to call neighbor indexing.

Every MATLAB user is familiar with ordinary matrix indexing notation.

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.5470    0.1890    0.3685
    0.2963       NaN       NaN
    0.7447    0.1835    0.7802

Replace the NaNs with zeros:

B(isnan(B)) = 0
B =

    0.5470    0.1890    0.3685
    0.2963         0         0
    0.7447    0.1835    0.7802

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 C and D are matrices, then C(D) is a logical indexing expression if C and D are the same size, and D is a logical matrix.

"Logical" is one of the builtin types, or classes, of MATLAB matrices. Relational 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 D
  Name      Size            Bytes  Class      Attributes

  D         4x4                16  logical              

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.0811    0.4868    0.3063
    0.9294         0         0
    0.7757    0.4468    0.5108

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');
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.

Here's an example showing how to use logical indexing to compute the histogram of a subset of image pixels. Specifically, 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 a segmentation result (computed and saved earlier), represented as a binary image:

url = 'http://blogs.mathworks.com/images/steve/192/rice_bw.png';
bw = imread(url);
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      17597x1             17597  uint8              

Finally, compute the histogram of the foreground pixels.

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))


Get the MATLAB code

Published with MATLAB® 7.5

10 Responses to “Logical indexing”

  1. Andreas Korinek replied on :

    Hi Steve,

    I was wondering why the logical type uses 1 byte per entry and not 1 bit. Seems like a huge waste of memory for large binary images.

  2. Steve replied on :

    Andreas—It’s not a simple story. It’s tied up in the long history of MATLAB language design evolution. Before MATLAB 5, all MATLAB matrices were 2-D double precision, and “logicalness” was an attribute of those matrices. That’s 8 bytes per binary pixel! Then MATLAB 5 introduced integer array types (including uint8), but at first there was relatively little functional or operator support for them, except in the Image Processing Toolbox. Logicalness was still a numeric array attribute. Gradually, some operators that produced logical arrays began producing uint8 arrays instead of double arrays. These changes were made very cautiously because of obvious compatibility issues. Then in MATLAB 6.5 logical became its own class, instead of an attribute of other classes. Again this change had to be made very carefully to minimize the impact on the existing huge base of internal code, toolbox code (especially in the Image Processing Toolbox), and existing customer MEX code. By keeping to the 1-byte element, much existing internal MATLAB code as well customer code could continue to work with little or no change. A “compatibility layer” was introduced into the MEX API that helped smooth over compatibility issues for a couple of releases. Plus the fact that C had a builtin one-byte data type, but not a builtin one-bit data type, influenced decision making along the way.

  3. Ram replied on :

    Hi Steve,

    My work involves finding suitable pixels in an image and pseudo-randomly selecting those pixels. Considering the selected pixel as centre pixel i have to modify the value of the centre pixel and its 4-neighbours.
    The problem is that i must make sure none of neighbours are selected again so the values of already modified 5 pixels dont get remodified. Please suggest something.

  4. Steve replied on :

    Ram—Keep an auxiliary “mask” image (logical matrix) in which pixels already processed are marked with 1s.

  5. Joshua replied on :

    I have been trying to figure out a way to convolve specific pixels in an image. Could a logical mask be used to do this? Convolving all the pixels in an image is a waste for my algorithm that only needs to convolve sometimes just one pixel in a loop. The only solution I have found so far is to copy out a 3×3 region, convolve that, and copy the value back. But it isn’t very elegant.

  6. Steve replied on :

    Joshua—You might find roifilt to be helpful.

  7. Lydia replied on :

    Dear Steve,
    My images are quite big (169600×241 matrices, 326988800bytes) and when i try to display them it seems I don’t have enough memory or the image is too big to fit in the screen and use the imshow function… How can I make these images easier to handle and take less memory? I am not very familiar with all the available storage formats, which one could I try, any option?

  8. Steve replied on :

    Lydia—If your files are TIFF, and if you have R2007b or R2008a, you can use the PixelRegion parameter of imread to read in subsets of the image, or to read in a subsampled version of the image.

  9. Oliver A. Chapman, P.E. replied on :

    Steve,

    Thanks for directing me to this. It is very well written.

  10. Steve replied on :

    Oliver—I’m glad you found it useful.

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.

  • Camilo: Steve !!! I changed imwrite for saveas and it worked !!! Thanks for showing us your code. It was very...
  • Camilo: Steve, Your code is working almost perfectly, but, whenever I go to the folder where I am storing the images...
  • murat: Hi Steve, I have an rgb image of a kind of cream and it contains some small black particles (black dots). In...
  • Steve: Ernest—Look at setting the FaceColor property. The code for setting that is shown on the page you asked...
  • Ernest Miller: Hi Steve, Understood. However, can you explain how to change the colors? Thanks, Ernest
  • Jan: Hi Steve Very useful code, yet what if I parts of my rotated+translated object are outside the original...
  • Steve: MoHDa—It might be possible. You’ll need to use one of the options that produces closed edge...
  • MoHDa: I have one question about the ROIPOLY: I have an image with stripes, I use the “edge” command for...
  • Steve: Shahn—My November 17, 2006 post shows you how to do it.
  • Steve: Kay-Uwe—Thanks for following up. I am planning to make it easier to use test directories in a package....

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