Image Processing Toolbox    

Displaying Binary Images

To display a binary image, the syntax is

In MATLAB, a binary image is a logical two-dimensional uint8 or double matrix that contains only 0's and 1's. (The toolbox does not support uint16 binary images.) All toolbox functions that return a binary image, return them as uint8 logical arrays.

Generally speaking, working with binary images with the toolbox is very straightforward. In most cases you will load a 1-bit binary image, and MATLAB will create a logical uint8 image in memory. You will normally not encounter double binary images unless you create them yourself using MATLAB.

If you load an image with a bit depth greater than 1 bit per pixel, or use MATLAB to create a new double or uint8 image containing only 0's and 1's, you may encounter unexpected results. For example, the mere presence of all 0's and 1's does not always indicate a binary image. For MATLAB to interpret the image as binary, it must be logical, meaning that its logical flag must be turned "on." Therefore, intensity images that happen to contain only 0's and 1's are not binary images.

To change the default behavior of imshow, set the toolbox preferences. See Setting the Preferences for imshow for more information.

This example underscores the importance of having the logical flag turned on if you want your image to behave like a binary image.

Create an image of class double that contains only 0's and 1's.

While this image may look like a binary image, it is really a grayscale image -- it will not be recognized as a binary image by any toolbox function. If you were to save this image to a file (without specifying a bit depth) it would be saved as an 8-bit grayscale image containing 0's and 255's.

The fact that BW1 is not really a binary image becomes evident when you convert it to class uint8.

BW1 still contains 0's and 1's, but since the dynamic range of an uint8 intensity image is [0 255], the value 1 is very close to black.

To make BW1 a true binary image use the NOT EQUAL (~=) operator, which will turn the logical flag on.

Write BW3 to a file using one of the formats that supports writing 1-bit images. You will not need to specify a bit depth, because MATLAB automatically saves logical uint8 or double images as 1-bit images if the file format supports it.

You can check the bit depth of grid.tif by calling imfinfo. As you will see, the BitDepth field indicates that it has been saved as a 1-bit image, with the beginning of your output looking something like this.

Changing the Display Colors of a Binary Image

You may prefer to invert binary images when you display them, so that 0 values display as white and 1 values display as black. To do this, use the NOT (~) operator in MATLAB. For example,

You can also display a binary image using a colormap. If the image is of class uint8, 0's display as the first color in the colormap, and 1's values display as the second color. For example, the following command displays 0's as red and 1's as blue.

Figure 3-1: Binary Image Displayed with a Colormap

If the image is of class double, you need to add 1 to each value in the image matrix, because there is no offset in the colormap indexing.

The Image and Axes Properties of a Binary Image

In most cases, it is not necessary to concern yourself with the Handle Graphics property settings made when you call imshow. Therefore, this section is not required reading, but rather information for those who really "want to know."

imshow sets the Handle Graphics properties that control how colors display, as follows:


 Displaying Intensity Images Displaying RGB Images