Computation
Visualization
Programming
For Use with MATLAB
®
Image Processing
Toolbox
User’s Guide
Version 3
How to Contact The MathWorks:
www.mathworks.com Web
comp.soft-sys.matlab Newsgroup
support@mathworks.com Technical support
suggest@mathworks.com Product enhancement suggestions
bugs@mathworks.com Bug reports
doc@mathworks.com Documentation error reports
service@mathworks.com Order status, license renewals, passcodes
info@mathworks.com Sales, pricing, and general information
508-647-7000 Phone
508-647-7001 Fax
The MathWorks, Inc. Mail
3 Apple Hill Drive
Natick, MA 01760-2098
For contact information about worldwide offices, see the MathWorks Web site.
Image Processing Toolbox User’s Guide
COPYRIGHT 1993 - 2002 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or repro-
duced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by
or for the federal government of the United States. By accepting delivery of the Program, the government
hereby agrees that this software qualifies as "commercial" computer software within the meaning of FAR
Part 12.212, DFARS Part 227.7202-1, DFARS Part 227.7202-3, DFARS Part 252.227-7013, and DFARS Part
252.227-7014. The terms and conditions of The MathWorks, Inc. Software License Agreement shall pertain
to the government’s use and disclosure of the Program and Documentation, and shall supersede any
conflicting contractual terms or conditions. If this license fails to meet the government’s minimum needs or
is inconsistent in any respect with federal procurement law, the government agrees to return the Program
and Documentation, unused, to MathWorks.
MATLAB, Simulink, Stateflow, Handle Graphics, and Real-Time Workshop are registered trademarks, and
TargetBox is a trademark of The MathWorks, Inc.
Other product or brand names are trademarks or registered trademarks of their respective holders.
Printing History: August 1993 First printing Version 1
May 1997 Second printing Version 2
January 1998 Online only Revised for Version 2.1
January 1999 Online only Revised for Version 2.2 (Release 11)
September 2000 Online only Revised for Version 2.2.2 (Release 12)
April 2001 Third printing Revised for Version 3.0
June 2001 Online only Revised for Version 3.1 (Release 12.1)
July 2002 Online only Revised for Version 3.2 (Release 13)
i
Contents
Preface
What Is the Image Processing Toolbox? . . . . . . . . . . . . . . . . . . xii
Related Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Configuration Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
About the Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Structure of the Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Image Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
MATLAB Newsgroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii
Typographical Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Image Processing Demos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
1
Getting Started
Example 1 — Some Basic Topics . . . . . . . . . . . . . . . . . . . . . . . . 1-2
1. Read and Display an Image . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
2. Check the Image in Memory . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
3. Perform Histogram Equalization . . . . . . . . . . . . . . . . . . . . . . 1-4
4. Write the Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7
5. Check the Contents of the Newly Written File . . . . . . . . . . . 1-7
Example 2 — Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
1. Read and Display an Image . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
2. Use Morphological Opening to Estimate the Background . . 1-9
3. Display the Background Approximation as a Surface . . . . . 1-10
ii Contents
4. Subtract the Background Image from the Original Image . 1-12
5. Adjust the Image Contrast . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13
6. Apply Thresholding to the Image . . . . . . . . . . . . . . . . . . . . . 1-14
7. Determine the Number of Objects in the Image . . . . . . . . . 1-15
8. Examine the Label Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-16
9. Measure Object Properties in the Image . . . . . . . . . . . . . . . 1-18
10. Compute Statistical Properties of Objects in the Image . . 1-21
Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23
Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23
Toolbox Demos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23
2
Introduction
Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Images in MATLAB and the Image Processing Toolbox . . . 2-4
Storage Classes in the Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
Image Types in the Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Indexed Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Intensity Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Binary Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
RGB Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
Multiframe Image Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11
Summary of Image Types and Numeric Classes . . . . . . . . . . . 2-12
Converting Image Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12
Working with Image Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15
Reading a Graphics Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
Writing a Graphics Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17
Querying a Graphics File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18
Converting Image Storage Classes . . . . . . . . . . . . . . . . . . . . . . 2-19
Converting Graphics File Formats . . . . . . . . . . . . . . . . . . . . . . 2-20
Image Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-21
iii
Summary of Image Arithmetic Functions . . . . . . . . . . . . . . . . 2-22
Image Arithmetic Truncation Rules . . . . . . . . . . . . . . . . . . . . . 2-22
Adding Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-23
Subtracting Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-24
Multiplying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-25
Dividing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27
Nesting Calls to Image Arithmetic Functions . . . . . . . . . . . . . 2-27
Coordinate Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-28
Pixel Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-28
Spatial Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-29
3
Displaying and Printing Images
Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Displaying Indexed Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Displaying Intensity Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4
Displaying Binary Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
Displaying RGB Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10
Displaying Images Directly from Disk . . . . . . . . . . . . . . . . . . . 3-11
Special Display Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
Adding a Colorbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
Displaying Multiframe Images . . . . . . . . . . . . . . . . . . . . . . . . . 3-13
Displaying Multiple Images . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17
Setting Toolbox Display Preferences . . . . . . . . . . . . . . . . . . . 3-23
Toolbox Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23
Using the truesize Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
Zooming in on a Region of an Image . . . . . . . . . . . . . . . . . . . 3-26
Zooming In or Out with the Zoom Buttons . . . . . . . . . . . . . . . . 3-26
Zooming In or Out from the Command Line . . . . . . . . . . . . . . 3-26