{"id":391,"date":"2011-09-27T07:00:27","date_gmt":"2011-09-27T11:00:27","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2011\/09\/27\/digital-image-processing-using-matlab-reading-image-files\/"},"modified":"2019-10-29T16:55:10","modified_gmt":"2019-10-29T20:55:10","slug":"digital-image-processing-using-matlab-reading-image-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/09\/27\/digital-image-processing-using-matlab-reading-image-files\/","title":{"rendered":"Digital image processing using MATLAB: reading image files"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><i>Today's post is part of an ongoing <a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/dipum-tutorials\/\">tutorial series on digital image processing using MATLAB<\/a>. I'm covering topics in roughly the order used in the book <a href=\"http:\/\/imageprocessingplace.com\/DIPUM-2E\/dipum2e_main_page.htm\">Digital Image Processing Using MATLAB<\/a>.<\/i><\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Reading images<\/a><\/li>\r\n         <li><a href=\"#5\">Reading from multi-image TIFF files<\/a><\/li>\r\n         <li><a href=\"#6\">Reading subsets of an image<\/a><\/li>\r\n         <li><a href=\"#9\">Sample image files<\/a><\/li>\r\n         <li><a href=\"#11\">For more information<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Reading images<a name=\"1\"><\/a><\/h3>\r\n   <p>The MATLAB function <tt>imread<\/tt> reads image data from a variety of formats, including:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Windows Bitmap (BMP)<\/li>\r\n         <li>Windows Cursor resources (CUR)<\/li>\r\n         <li>Flexible Image Transport System (FITS)<\/li>\r\n         <li>Graphics Interchange Format (GIF)<\/li>\r\n         <li>Hierarchical Data Format (HDF)<\/li>\r\n         <li>Windows Icon resources (ICO)<\/li>\r\n         <li>JPEG 2000<\/li>\r\n         <li>Joint Photographic Experts Group (JPEG)<\/li>\r\n         <li>Portable Bitmap (PBM)<\/li>\r\n         <li>Windows Paintbrush (PCX)<\/li>\r\n         <li>Portable Graymap (PGM)<\/li>\r\n         <li>Portable Network Graphics (PNG)<\/li>\r\n         <li>Portable Any Map (PNM)<\/li>\r\n         <li>Portable Pixmap (PPM)<\/li>\r\n         <li>Sun Raster (RAS)<\/li>\r\n         <li>Tagged Image File Format (TIFF)<\/li>\r\n         <li>X Window Dump (XWD)<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>For example, the following line reads the pixels from a PNG file into the MATLAB variable <tt>I<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'rice.png'<\/span>);<\/pre><p>After you run the code above, the Workspace Browser shows you that your variable <tt>I<\/tt> is a 256x256 matrix of uint8 (unsigned eight-bit integer) values in the range [40,204].\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/workspace-browser-screenshot.png\"> <\/p>\r\n   <p>Although <tt>imread<\/tt> uses the file extension (such as .png) to help determine the file format, it can also determine the format automatically.\r\n      Suppose, for example, you have a set of files in which the file extension is used to indicate a particular data series, which\r\n      I'll simulate here by copying rice.png to another filename:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = which(<span style=\"color: #A020F0\">'rice.png'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\ns =\r\n\r\nC:\\MATLABs\\R2011b\\toolbox\\images\\imdemos\\rice.png\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">copyfile(s, <span style=\"color: #A020F0\">'rice.series-x03a'<\/span>)\r\ndir(<span style=\"color: #A020F0\">'rice.*'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\nrice.series-x03a  \r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'rice.series-x03a'<\/span>);\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/dipum_2_2_reading_images_01.jpg\"> <h3>Reading from multi-image TIFF files<a name=\"5\"><\/a><\/h3>\r\n   <p>TIFF files can store multiple independent images. The function <tt>imread<\/tt> has a syntax for specifying which image you want. The file mri.tif (<a href=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/mri.tif\">download link<\/a>) contains 27 images. Here is how you would read the 1st, 6th, and 11th images in the file.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I1 = imread(<span style=\"color: #A020F0\">'mri.tif'<\/span>, <span style=\"color: #A020F0\">'Index'<\/span>, 1);\r\nI6 = imread(<span style=\"color: #A020F0\">'mri.tif'<\/span>, <span style=\"color: #A020F0\">'Index'<\/span>, 6);\r\nI11 = imread(<span style=\"color: #A020F0\">'mri.tif'<\/span>, <span style=\"color: #A020F0\">'Index'<\/span>, 11);<\/pre><h3>Reading subsets of an image<a name=\"6\"><\/a><\/h3>\r\n   <p>For very large image files it can be useful to read in only a subset of the image pixels. The <tt>imread<\/tt> 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:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'shadow.tif'<\/span>, <span style=\"color: #A020F0\">'PixelRegion'<\/span>, {[50 100], [150 200]});\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/dipum_2_2_reading_images_02.jpg\"> <p>You can use the MATLAB function <tt>imfinfo<\/tt> to read <i>metadata<\/i> about an image file without reading in all the pixel data.  Here's an example:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imfinfo(<span style=\"color: #A020F0\">'peppers.png'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\nans = \r\n\r\n                  Filename: 'C:\\MATLABs\\R2011b\\toolbox\\images\\imdemos\\peppers.png'\r\n               FileModDate: '16-Dec-2002 06:10:58'\r\n                  FileSize: 287677\r\n                    Format: 'png'\r\n             FormatVersion: []\r\n                     Width: 512\r\n                    Height: 384\r\n                  BitDepth: 24\r\n                 ColorType: 'truecolor'\r\n           FormatSignature: [137 80 78 71 13 10 26 10]\r\n                  Colormap: []\r\n                 Histogram: []\r\n             InterlaceType: 'none'\r\n              Transparency: 'none'\r\n    SimpleTransparencyData: []\r\n           BackgroundColor: []\r\n           RenderingIntent: []\r\n            Chromaticities: []\r\n                     Gamma: []\r\n               XResolution: []\r\n               YResolution: []\r\n            ResolutionUnit: []\r\n                   XOffset: []\r\n                   YOffset: []\r\n                OffsetUnit: []\r\n           SignificantBits: []\r\n              ImageModTime: '16 Jul 2002 16:46:41 +0000'\r\n                     Title: []\r\n                    Author: []\r\n               Description: 'Zesty peppers'\r\n                 Copyright: 'Copyright The MathWorks, Inc.'\r\n              CreationTime: []\r\n                  Software: []\r\n                Disclaimer: []\r\n                   Warning: []\r\n                    Source: []\r\n                   Comment: []\r\n                 OtherText: []\r\n\r\n<\/pre><p>The display shows that peppers.png is a truecolor image with 24 bits per pixel. The size of the file is 287677 bytes, and\r\n      it was last modified in the morning of December 16, 2002, during a period of heavy snowfall. By capturing the output of <tt>imfinfo<\/tt> in a variable, you can write code based on this information, such as:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">info = imfinfo(<span style=\"color: #A020F0\">'peppers.png'<\/span>);\r\nnum_rows = info.Height;\r\nnum_cols = info.Width;<\/pre><h3>Sample image files<a name=\"9\"><\/a><\/h3>\r\n   <p>The files rice.png, shadow.tif, and peppers.png read by the code above are sample images that ships with Image Processing\r\n      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\r\n      in my posts. You can see a list of the sample image files in the toolbox with this command:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">imdemos<\/span><\/pre><pre style=\"font-style:oblique\">  Image Processing Toolbox --- demos and sample images\r\n \r\n    iptdemos           - Index of Image Processing Toolbox demos.\r\n \r\n    ipexaerial         - Registering an Aerial Photo to an Orthophoto.\r\n    ipexangle          - Measuring Angle of Intersection.\r\n    ipexbatch          - Batch Processing ImageFiles in Parallel.\r\n    ipexblind          - Deblurring Images Using a Blind Deconvolution Filter.\r\n    ipexblockprocedge  - Block Processing Large Images.\r\n    ipexblockprocstats - Computing Statistics for Large Images.\r\n    ipexcell           - Detecting a Cell Using Image Segmentation.\r\n    ipexcheckerboard   - Creating a Gallery of Transformed Images.\r\n    ipexconformal      - Exploring a Conformal Mapping.\r\n    ipexcontrast       - Contrast Enhancement Techniques.\r\n    ipexfabric         - Color-based Segmentation Using the L*a*b* Color Space.\r\n    ipexhistology      - Color-based Segmentation Using K-Means Clustering.\r\n    ipexlanstretch     - Enhancing Multispectral Color Composite Images.\r\n    ipexlucy           - Deblurring Images Using a Lucy-Richardson Filter.\r\n    ipexndvi           - Finding Vegetation in a Multispectral Image.\r\n    ipexnormxcorr2     - Registering an Image Using Normalized Cross-Correlation\r\n    ipexpendulum       - Finding the Length of a Pendulum in Motion.\r\n    ipexprops          - Measuring Regions in Grayscale Images.\r\n    ipexradius         - Measuring the Radius of a Roll of Tape.\r\n    ipexreconstruct    - Reconstructing an Image from Projection Data.\r\n    ipexregularized    - Deblurring Images Using a Regularized Filter.\r\n    ipexrice           - Correcting Nonuniform Illumination.\r\n    ipexrotate         - Finding the Rotation and Scale of a Distorted Image.\r\n    ipexroundness      - Identifying Round Objects.\r\n    ipexshear          - Padding and Shearing an Image Simultaneously.\r\n    ipexsnow           - Granulometry of Snowflakes. \r\n    ipextexturefilter  - Texture Segmentation Using Texture Filters.\r\n    ipextraffic        - Detecting Cars in a Video of Traffic.\r\n    ipexwatershed      - Marker-controlled watershed segmentation.\r\n    ipexwiener         - Deblurring Images Using the Wiener Filter.\r\n \r\n  Extended-example helper files.\r\n    HistogramAccumulator           - Used by blockproc stats example.\r\n    batchDetectCells               - Used by batch processing example.\r\n    batchProcessFiles              - Used by batch processing example.\r\n    conformalForward1              - Used by conformal mapping example.\r\n    conformalForward2              - Used by conformal mapping example.\r\n    conformalInverse               - Used by conformal mapping example.\r\n    conformalInverseClip           - Used by conformal mapping example.\r\n    conformalSetupInputAxes        - Used by conformal mapping example.\r\n    conformalSetupOutputAxes       - Used by conformal mapping example.\r\n    conformalShowLines             - Used by conformal mapping example.\r\n    conformalShowCircles           - Used by conformal mapping example.\r\n    conformalShowInput             - Used by conformal mapping example.\r\n    conformalShowOutput            - Used by conformal mapping example.\r\n    propsSynthesizeImage           - Used by measuring regions example.\r\n    LanAdapter                     - Used by blockproc stats example.\r\n \r\n  Sample MAT-files.\r\n    imdemos.mat           - Images used in demos.\r\n    pendulum.mat          - Used by ipexpendulum.\r\n    regioncoordinates.mat - Used by ipexfabric.\r\n    trees.mat             - Scanned painting.\r\n    westconcordpoints.mat - Used by aerial photo registration example.\r\n    mristack.mat          - Used by help example in IMPLAY.\r\n    cellsequence.mat      - Used by help example in IMPLAY.\r\n \r\n  Sample FITS images.\r\n    solarspectra.fts\r\n \r\n  Sample HDR images.\r\n    office.hdr\r\n \r\n  Sample JPEG images.\r\n    football.jpg\r\n    greens.jpg\r\n \r\n  Sample PNG images.\r\n    bag.png\r\n    blobs.png\r\n    circles.png\r\n    coins.png\r\n    concordorthophoto.png\r\n    concordaerial.png\r\n    fabric.png\r\n    gantrycrane.png\r\n    glass.png\r\n    hestain.png\r\n    liftingbody.png\r\n    onion.png\r\n    pears.png\r\n    peppers.png\r\n    pillsetc.png\r\n    rice.png\r\n    saturn.png\r\n    snowflakes.png\r\n    tape.png\r\n    testpat1.png\r\n    text.png\r\n    tissue.png\r\n    westconcordorthophoto.png\r\n    westconcordaerial.png\r\n \r\n  Sample TIFF images.\r\n    AT3_1m4_01.tif\r\n    AT3_1m4_02.tif\r\n    AT3_1m4_03.tif\r\n    AT3_1m4_04.tif\r\n    AT3_1m4_05.tif\r\n    AT3_1m4_06.tif\r\n    AT3_1m4_07.tif\r\n    AT3_1m4_08.tif\r\n    AT3_1m4_09.tif\r\n    AT3_1m4_10.tif\r\n    autumn.tif  \r\n    board.tif\r\n    cameraman.tif\r\n    canoe.tif   \r\n    cell.tif\r\n    circbw.tif\r\n    circuit.tif\r\n    eight.tif   \r\n    forest.tif\r\n    kids.tif\r\n    logo.tif\r\n    mandi.tif\r\n    m83.tif\r\n    moon.tif\r\n    mri.tif\r\n    paper1.tif\r\n    pout.tif\r\n    shadow.tif\r\n    spine.tif\r\n    tire.tif\r\n    trees.tif\r\n \r\n  Sample Landsat images.\r\n    littlecoriver.lan\r\n    mississippi.lan\r\n    montana.lan\r\n    paris.lan\r\n    rio.lan\r\n    tokyo.lan\r\n \r\n  Sample AVI files.\r\n    rhinos.avi\r\n    traffic.avi\r\n \r\n  Sample Analyze 7.5 images.\r\n    brainMRI.img\r\n \r\n  Photo credits\r\n    board:\r\n \r\n      Computer circuit board, courtesy of Alexander V. Panasyuk,\r\n      Ph.D., Harvard-Smithsonian Center for Astrophysics.\r\n \r\n    cameraman:\r\n \r\n      Copyright Massachusetts Institute of Technology.  Used with\r\n      permission.\r\n \r\n    cell:\r\n    AT3_1m4_01:\r\n    AT3_1m4_02:\r\n    AT3_1m4_03:\r\n    AT3_1m4_04:\r\n    AT3_1m4_05:\r\n    AT3_1m4_06:\r\n    AT3_1m4_07:\r\n    AT3_1m4_08:\r\n    AT3_1m4_09:\r\n    AT3_1m4_10:\r\n \r\n      Cancer cells from rat prostates, courtesy of Alan W. Partin, M.D,\r\n      Ph.D., Johns Hopkins University School of Medicine.\r\n \r\n    circuit:\r\n \r\n      Micrograph of 16-bit A\/D converter circuit, courtesy of Steve\r\n      Decker and Shujaat Nadeem, MIT, 1993. \r\n \r\n    concordaerial and westconcordaerial:\r\n \r\n      Visible color aerial photographs courtesy of mPower3\/Emerge.\r\n \r\n    concordorthophoto and westconcordorthophoto:\r\n \r\n      Orthoregistered photographs courtesy of Massachusetts Executive Office\r\n      of Environmental Affairs, MassGIS.\r\n \r\n    forest:\r\n \r\n      Photograph of Carmanah Ancient Forest, British Columbia, Canada,\r\n      courtesy of Susan Cohen. \r\n \r\n    gantrycrane:\r\n \r\n      Gantry crane used to build a bridge, courtesy of Jeff Mather.\r\n    \r\n    hestain:\r\n \r\n      Image of tissue stained with hemotoxylin and eosin (H&amp;E) at 40X\r\n      magnification, courtesy of Alan W. Partin, M.D., Ph.D., Johns Hopkins\r\n      University School of Medicine.\r\n \r\n    liftingbody:\r\n \r\n      Public domain image of M2-F1 lifting body in tow, courtesy of NASA,\r\n      1964-01-01, Dryden Flight Research Center #E-10962, GRIN database\r\n      #GPN-2000-000097.\r\n \r\n    mandi:\r\n \r\n      Bayer pattern-encoded image taken by a camera with a sensor\r\n      alignment of 'bggr', courtesy of Jeremy Barry.\r\n \r\n    m83:\r\n \r\n      M83 spiral galaxy astronomical image courtesy of Anglo-Australian\r\n      Observatory, photography by David Malin. \r\n \r\n    moon:\r\n \r\n      Copyright Michael Myers.  Used with permission.\r\n \r\n    pears:\r\n \r\n      Copyright Corel.  Used with permission.\r\n \r\n    tissue:\r\n \r\n      Cytokeratin CAM 5.2 stain of human prostate tissue, courtesy of \r\n      Alan W. Partin, M.D, Ph.D., Johns Hopkins University School\r\n      of Medicine.\r\n \r\n    trees:\r\n \r\n      Trees with a View, watercolor and ink on paper, copyright Susan\r\n      Cohen.  Used with permission. \r\n \r\n    LAN files:\r\n \r\n      Permission to use Landsat TM data sets provided by Space Imaging,\r\n      LLC, Denver, Colorado.\r\n \r\n    saturn:\r\n \r\n      Public domain image courtesy of NASA, Voyager 2 image, 1981-08-24, \r\n      NASA catalog #PIA01364\r\n \r\n    solarspectra:\r\n \r\n      Solar spectra image courtesy of Ann Walker, Boston University.\r\n \r\n  See also COLORSPACES, IMAGES, IMAGESLIB, IMUITOOLS, IPTFORMATS, IPTUTILS.\r\n\r\n<\/pre><p>The output also shows you the image credit information for the images that are not copyrighted by MathWorks.<\/p>\r\n   <h3>For more information<a name=\"11\"><\/a><\/h3>\r\n   <p>For more information, see Section 2.2 of <a href=\"http:\/\/imageprocessingplace.com\/DIPUM-2E\/dipum2e_main_page.htm\"> <i>Digital Image Processing Using MATLAB<\/i><\/a>.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/dipum-cover.png\"> <\/p>\r\n   <p>See also the reference pages for <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/imread.html\"><tt>imread<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/imfinfo.html\"><tt>imfinfo<\/tt><\/a>, as well as the section <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/images\/f13-19056.html\">Reading and Writing Image Data<\/a> in the Image Processing Toolbox User's Guide.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_5d3c8af85be942078e5c513d640bc412() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='5d3c8af85be942078e5c513d640bc412 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5d3c8af85be942078e5c513d640bc412';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Steve Eddins';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_5d3c8af85be942078e5c513d640bc412()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.13<br><\/p>\r\n<\/div>\r\n<!--\r\n5d3c8af85be942078e5c513d640bc412 ##### SOURCE BEGIN #####\r\n%%\r\n% _Today's post is part of an ongoing\r\n% <https:\/\/blogs.mathworks.com\/steve\/category\/dipum-tutorials\/ tutorial\r\n% series on digital image processing using MATLAB>. I'm covering topics in\r\n% roughly the order used in the book\r\n% <http:\/\/imageprocessingplace.com\/DIPUM-2E\/dipum2e_main_page.htm Digital\r\n% Image Processing Using MATLAB>._\r\n%\r\n%% Reading images\r\n% The MATLAB function |imread| reads image data from a variety of formats,\r\n% including:\r\n%\r\n% * Windows Bitmap (BMP)\r\n% * Windows Cursor resources (CUR)\r\n% * Flexible Image Transport System (FITS)\r\n% * Graphics Interchange Format (GIF)\r\n% * Hierarchical Data Format (HDF)\r\n% * Windows Icon resources (ICO)\r\n% * JPEG 2000\r\n% * Joint Photographic Experts Group (JPEG)\r\n% * Portable Bitmap (PBM)\r\n% * Windows Paintbrush (PCX)\r\n% * Portable Graymap (PGM)\r\n% * Portable Network Graphics (PNG)\r\n% * Portable Any Map (PNM)\r\n% * Portable Pixmap (PPM)\r\n% * Sun Raster (RAS)\r\n% * Tagged Image File Format (TIFF)\r\n% * X Window Dump (XWD)\r\n%\r\n% For example, the following line reads the pixels from a PNG file into the\r\n% MATLAB variable |I|:\r\n\r\nI = imread('rice.png');\r\n\r\n%%\r\n% After you run the code above, the Workspace Browser shows you that your\r\n% variable |I| is a 256x256 matrix of uint8 (unsigned eight-bit integer)\r\n% values in the range [40,204].\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/workspace-browser-screenshot.png>>\r\n%\r\n% Although |imread| uses the file extension (such as .png) to help\r\n% determine the file format, it can also determine the format\r\n% automatically. Suppose, for example, you have a set of files in which the\r\n% file extension is used to indicate a particular data series, which I'll\r\n% simulate here by copying rice.png to another filename:\r\n\r\ns = which('rice.png')\r\n\r\n%%\r\ncopyfile(s, 'rice.series-x03a')\r\ndir('rice.*')\r\n\r\n%%\r\nI = imread('rice.series-x03a');\r\nimshow(I)\r\n\r\n%% Reading from multi-image TIFF files\r\n% TIFF files can store multiple independent images. The function |imread|\r\n% has a syntax for specifying which image you want. The file mri.tif \r\n% (<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/mri.tif download\r\n% link>) contains 27 images. Here is how you would read the 1st, 6th, and\r\n% 11th images in the file.\r\n\r\nI1 = imread('mri.tif', 'Index', 1);\r\nI6 = imread('mri.tif', 'Index', 6);\r\nI11 = imread('mri.tif', 'Index', 11);\r\n\r\n%% Reading subsets of an image\r\n% For very large image files it can be useful to read in only a subset of\r\n% the image pixels. The |imread| function can do this for both TIFF and\r\n% JPEG 2000 files. Here's how to read rows 50-100 and columns 150-200 of\r\n% shadow.tif:\r\n\r\nI = imread('shadow.tif', 'PixelRegion', {[50 100], [150 200]});\r\nimshow(I)\r\n\r\n%%\r\n% You can use the MATLAB function |imfinfo| to read _metadata_ about an\r\n% image file without reading in all the pixel data.  Here's an example:\r\n\r\nimfinfo('peppers.png')\r\n\r\n%%\r\n% The display shows that peppers.png is a truecolor image with 24 bits per\r\n% pixel. The size of the file is 287677 bytes, and it was last modified\r\n% in the morning of December 16, 2002, during a period of heavy snowfall.\r\n% By capturing the output of |imfinfo| in a variable, you can write code\r\n% based on this information, such as:\r\n\r\ninfo = imfinfo('peppers.png');\r\nnum_rows = info.Height;\r\nnum_cols = info.Width;\r\n\r\n%% Sample image files\r\n% The files rice.png, shadow.tif, and peppers.png read by the code above\r\n% are sample images that ships with Image Processing Toolbox. I use sample\r\n% images from the toolbox a lot in this blog because I want readers to be\r\n% able to run the code examples in my posts. You can see a list of the\r\n% sample image files in the toolbox with this command:\r\n\r\nhelp imdemos\r\n\r\n%%\r\n% The output also shows you the image credit information for the images\r\n% that are not copyrighted by MathWorks.\r\n\r\n%% For more information\r\n% For more information, see Section 2.2 of\r\n% <http:\/\/imageprocessingplace.com\/DIPUM-2E\/dipum2e_main_page.htm\r\n%  _Digital Image Processing Using MATLAB_>. \r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/dipum-cover.png>>\r\n%\r\n% See also the reference pages for\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/imread.html |imread|> and\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/imfinfo.html |imfinfo|>, as\r\n% well as the section\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/images\/f13-19056.html Reading and\r\n% Writing Image Data> in the Image Processing Toolbox User's Guide.\r\n%\r\n% \r\n##### SOURCE END ##### 5d3c8af85be942078e5c513d640bc412\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      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... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/09\/27\/digital-image-processing-using-matlab-reading-image-files\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[829,154,831,332,76,36,827],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/391"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=391"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":3757,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/391\/revisions\/3757"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}