Image Processing Toolbox | ![]() ![]() |
Displaying Binary Images
To display a binary image, the syntax is
In MATLAB, a binary image is of class logical
. Binary images contain only 0's and 1's. Pixels with the value 0 display as black; pixels with the value 1 display as white.
Note
For the toolbox to interpret the image as binary, it must be of class logical . Intensity images that happen to contain only 0's and 1's are not binary images.
|
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. (In this example, a box is drawn around the image to show the image boundary.) For example,
You can also display a binary image using a colormap. For example, the following command displays 0's as red and 1's as blue.
Reading and Writing Binary Images
In certain file formats, a binary image can be stored in a 1-bit format. When you read in a binary image in 1-bit format, MATLAB represents it in the workspace as a logical
array.
By default, MATLAB writes binary images as 1-bit images, if the file format supports it.
To verify the bit depth of test.tif
, call 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('test.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'
...
The Image and Axes Properties of a Binary Image
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 | ![]() |