Digital image processing using MATLAB: reading image files
Today's post is part of an ongoing tutorial series on digital image processing using MATLAB. I'm covering topics in roughly the order used in the book Digital Image Processing Using MATLAB.
Contents
Reading images
The MATLAB function imread reads image data from a variety of formats, including:
- Windows Bitmap (BMP)
- Windows Cursor resources (CUR)
- Flexible Image Transport System (FITS)
- Graphics Interchange Format (GIF)
- Hierarchical Data Format (HDF)
- Windows Icon resources (ICO)
- JPEG 2000
- Joint Photographic Experts Group (JPEG)
- Portable Bitmap (PBM)
- Windows Paintbrush (PCX)
- Portable Graymap (PGM)
- Portable Network Graphics (PNG)
- Portable Any Map (PNM)
- Portable Pixmap (PPM)
- Sun Raster (RAS)
- Tagged Image File Format (TIFF)
- X Window Dump (XWD)
For example, the following line reads the pixels from a PNG file into the MATLAB variable I:
I = imread('rice.png');
After you run the code above, the Workspace Browser shows you that your variable I is a 256x256 matrix of uint8 (unsigned eight-bit integer) values in the range [40,204].
Although imread uses the file extension (such as .png) to help determine the file format, it can also determine the format automatically. Suppose, for example, you have a set of files in which the file extension is used to indicate a particular data series, which I'll simulate here by copying rice.png to another filename:
s = which('rice.png')
s = C:\MATLABs\R2011b\toolbox\images\imdemos\rice.png
copyfile(s, 'rice.series-x03a') dir('rice.*')
rice.series-x03a
I = imread('rice.series-x03a');
imshow(I)
Reading from multi-image TIFF files
TIFF files can store multiple independent images. The function imread has a syntax for specifying which image you want. The file mri.tif (download link) contains 27 images. Here is how you would read the 1st, 6th, and 11th images in the file.
I1 = imread('mri.tif', 'Index', 1); I6 = imread('mri.tif', 'Index', 6); I11 = imread('mri.tif', 'Index', 11);
Reading subsets of an image
For very large image files it can be useful to read in only a subset of the image pixels. The imread function can do this for both TIFF and JPEG 2000 files. Here's how to read rows 50-100 and columns 150-200 of shadow.tif:
I = imread('shadow.tif', 'PixelRegion', {[50 100], [150 200]}); imshow(I)
You can use the MATLAB function imfinfo to read metadata about an image file without reading in all the pixel data. Here's an example:
imfinfo('peppers.png')
ans = Filename: 'C:\MATLABs\R2011b\toolbox\images\imdemos\peppers.png' FileModDate: '16-Dec-2002 06:10:58' FileSize: 287677 Format: 'png' FormatVersion: [] Width: 512 Height: 384 BitDepth: 24 ColorType: 'truecolor' FormatSignature: [137 80 78 71 13 10 26 10] Colormap: [] Histogram: [] InterlaceType: 'none' Transparency: 'none' SimpleTransparencyData: [] BackgroundColor: [] RenderingIntent: [] Chromaticities: [] Gamma: [] XResolution: [] YResolution: [] ResolutionUnit: [] XOffset: [] YOffset: [] OffsetUnit: [] SignificantBits: [] ImageModTime: '16 Jul 2002 16:46:41 +0000' Title: [] Author: [] Description: 'Zesty peppers' Copyright: 'Copyright The MathWorks, Inc.' CreationTime: [] Software: [] Disclaimer: [] Warning: [] Source: [] Comment: [] OtherText: []
The display shows that peppers.png is a truecolor image with 24 bits per pixel. The size of the file is 287677 bytes, and it was last modified in the morning of December 16, 2002, during a period of heavy snowfall. By capturing the output of imfinfo in a variable, you can write code based on this information, such as:
info = imfinfo('peppers.png');
num_rows = info.Height;
num_cols = info.Width;
Sample image files
The files rice.png, shadow.tif, and peppers.png read by the code above are sample images that ships with Image Processing Toolbox. I use sample images from the toolbox a lot in this blog because I want readers to be able to run the code examples in my posts. You can see a list of the sample image files in the toolbox with this command:
help imdemos
Image Processing Toolbox --- demos and sample images iptdemos - Index of Image Processing Toolbox demos. ipexaerial - Registering an Aerial Photo to an Orthophoto. ipexangle - Measuring Angle of Intersection. ipexbatch - Batch Processing ImageFiles in Parallel. ipexblind - Deblurring Images Using a Blind Deconvolution Filter. ipexblockprocedge - Block Processing Large Images. ipexblockprocstats - Computing Statistics for Large Images. ipexcell - Detecting a Cell Using Image Segmentation. ipexcheckerboard - Creating a Gallery of Transformed Images. ipexconformal - Exploring a Conformal Mapping. ipexcontrast - Contrast Enhancement Techniques. ipexfabric - Color-based Segmentation Using the L*a*b* Color Space. ipexhistology - Color-based Segmentation Using K-Means Clustering. ipexlanstretch - Enhancing Multispectral Color Composite Images. ipexlucy - Deblurring Images Using a Lucy-Richardson Filter. ipexndvi - Finding Vegetation in a Multispectral Image. ipexnormxcorr2 - Registering an Image Using Normalized Cross-Correlation ipexpendulum - Finding the Length of a Pendulum in Motion. ipexprops - Measuring Regions in Grayscale Images. ipexradius - Measuring the Radius of a Roll of Tape. ipexreconstruct - Reconstructing an Image from Projection Data. ipexregularized - Deblurring Images Using a Regularized Filter. ipexrice - Correcting Nonuniform Illumination. ipexrotate - Finding the Rotation and Scale of a Distorted Image. ipexroundness - Identifying Round Objects. ipexshear - Padding and Shearing an Image Simultaneously. ipexsnow - Granulometry of Snowflakes. ipextexturefilter - Texture Segmentation Using Texture Filters. ipextraffic - Detecting Cars in a Video of Traffic. ipexwatershed - Marker-controlled watershed segmentation. ipexwiener - Deblurring Images Using the Wiener Filter. Extended-example helper files. HistogramAccumulator - Used by blockproc stats example. batchDetectCells - Used by batch processing example. batchProcessFiles - Used by batch processing example. conformalForward1 - Used by conformal mapping example. conformalForward2 - Used by conformal mapping example. conformalInverse - Used by conformal mapping example. conformalInverseClip - Used by conformal mapping example. conformalSetupInputAxes - Used by conformal mapping example. conformalSetupOutputAxes - Used by conformal mapping example. conformalShowLines - Used by conformal mapping example. conformalShowCircles - Used by conformal mapping example. conformalShowInput - Used by conformal mapping example. conformalShowOutput - Used by conformal mapping example. propsSynthesizeImage - Used by measuring regions example. LanAdapter - Used by blockproc stats example. Sample MAT-files. imdemos.mat - Images used in demos. pendulum.mat - Used by ipexpendulum. regioncoordinates.mat - Used by ipexfabric. trees.mat - Scanned painting. westconcordpoints.mat - Used by aerial photo registration example. mristack.mat - Used by help example in IMPLAY. cellsequence.mat - Used by help example in IMPLAY. Sample FITS images. solarspectra.fts Sample HDR images. office.hdr Sample JPEG images. football.jpg greens.jpg Sample PNG images. bag.png blobs.png circles.png coins.png concordorthophoto.png concordaerial.png fabric.png gantrycrane.png glass.png hestain.png liftingbody.png onion.png pears.png peppers.png pillsetc.png rice.png saturn.png snowflakes.png tape.png testpat1.png text.png tissue.png westconcordorthophoto.png westconcordaerial.png Sample TIFF images. AT3_1m4_01.tif AT3_1m4_02.tif AT3_1m4_03.tif AT3_1m4_04.tif AT3_1m4_05.tif AT3_1m4_06.tif AT3_1m4_07.tif AT3_1m4_08.tif AT3_1m4_09.tif AT3_1m4_10.tif autumn.tif board.tif cameraman.tif canoe.tif cell.tif circbw.tif circuit.tif eight.tif forest.tif kids.tif logo.tif mandi.tif m83.tif moon.tif mri.tif paper1.tif pout.tif shadow.tif spine.tif tire.tif trees.tif Sample Landsat images. littlecoriver.lan mississippi.lan montana.lan paris.lan rio.lan tokyo.lan Sample AVI files. rhinos.avi traffic.avi Sample Analyze 7.5 images. brainMRI.img Photo credits board: Computer circuit board, courtesy of Alexander V. Panasyuk, Ph.D., Harvard-Smithsonian Center for Astrophysics. cameraman: Copyright Massachusetts Institute of Technology. Used with permission. cell: AT3_1m4_01: AT3_1m4_02: AT3_1m4_03: AT3_1m4_04: AT3_1m4_05: AT3_1m4_06: AT3_1m4_07: AT3_1m4_08: AT3_1m4_09: AT3_1m4_10: Cancer cells from rat prostates, courtesy of Alan W. Partin, M.D, Ph.D., Johns Hopkins University School of Medicine. circuit: Micrograph of 16-bit A/D converter circuit, courtesy of Steve Decker and Shujaat Nadeem, MIT, 1993. concordaerial and westconcordaerial: Visible color aerial photographs courtesy of mPower3/Emerge. concordorthophoto and westconcordorthophoto: Orthoregistered photographs courtesy of Massachusetts Executive Office of Environmental Affairs, MassGIS. forest: Photograph of Carmanah Ancient Forest, British Columbia, Canada, courtesy of Susan Cohen. gantrycrane: Gantry crane used to build a bridge, courtesy of Jeff Mather. hestain: Image of tissue stained with hemotoxylin and eosin (H&E) at 40X magnification, courtesy of Alan W. Partin, M.D., Ph.D., Johns Hopkins University School of Medicine. liftingbody: Public domain image of M2-F1 lifting body in tow, courtesy of NASA, 1964-01-01, Dryden Flight Research Center #E-10962, GRIN database #GPN-2000-000097. mandi: Bayer pattern-encoded image taken by a camera with a sensor alignment of 'bggr', courtesy of Jeremy Barry. m83: M83 spiral galaxy astronomical image courtesy of Anglo-Australian Observatory, photography by David Malin. moon: Copyright Michael Myers. Used with permission. pears: Copyright Corel. Used with permission. tissue: Cytokeratin CAM 5.2 stain of human prostate tissue, courtesy of Alan W. Partin, M.D, Ph.D., Johns Hopkins University School of Medicine. trees: Trees with a View, watercolor and ink on paper, copyright Susan Cohen. Used with permission. LAN files: Permission to use Landsat TM data sets provided by Space Imaging, LLC, Denver, Colorado. saturn: Public domain image courtesy of NASA, Voyager 2 image, 1981-08-24, NASA catalog #PIA01364 solarspectra: Solar spectra image courtesy of Ann Walker, Boston University. See also COLORSPACES, IMAGES, IMAGESLIB, IMUITOOLS, IPTFORMATS, IPTUTILS.
The output also shows you the image credit information for the images that are not copyrighted by MathWorks.
For more information
For more information, see Section 2.2 of Digital Image Processing Using MATLAB.
See also the reference pages for imread and imfinfo, as well as the section Reading and Writing Image Data in the Image Processing Toolbox User's Guide.
- Category:
- DIPUM tutorials
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.