Image Processing Toolbox | ![]() ![]() |
Displaying Binary Images
To display a binary image, the syntax is
imshow(BW)
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.
BW1 = zeros(20,20); BW1(2:2:18,2:2:18)=1; imshow(BW1, 'notruesize'); whos Name Size Bytes Class BW1 20x20 3200 double array Grand total is 400 elements using 3200 bytes
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
.
BW2=uint8(BW1); figure,imshow(BW2,'notruesize')
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.
BW3 = BW2 ~= 0; figure, imshow(BW3,'notruesize') whos Name Size Bytes Class BW1 20x20 3200 double array BW2 20x20 400 uint8 array BW3 20x20 400 uint8 array (logical) Grand total is 1225 elements using 4025 bytes
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.
imwrite(BW3, 'grid.tif'); % MATLAB supports writing 1-bit TIFFs.
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.
imfinfo('BW1.tif')
ans =
Filename: 'd:\mystuff\grid.tif'
FileModDate: '25-Nov-1998 11:36:17'
FileSize: 340
Format: 'tif'
FormatVersion: []
Width: 20
Height: 20
BitDepth: 1
ColorType: 'grayscale'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubfileType: 0
BitsPerSample: 1
Compression: 'CCITT 1D'
...
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,
BW = imread(`circles.tif'); imshow(~BW)
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.
imshow(BW,[1 0 0; 0 0 1])
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.
BW = double(BW); imshow(BW + 1,[1 0 0; 0 0 1])
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:
CData
is set to the data in BW
.CDataMapping
property is set to direct
.CLim
property is set to [0 1]. Colormap
property is set to a grayscale colormap whose values range from black to white.![]() | Displaying Intensity Images | Displaying RGB Images | ![]() |