Steve on Image Processing

Concepts, algorithms & MATLAB

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Image file information using imfinfo 6

Posted by Steve Eddins,

How can you find out what's in an image file before you read in the data? The answer is the function imfinfo. Today I want to explore the basics of using this function.

The function imfinfo returns a struct. The fields of the struct contain information about the image file.

Here's an example.

imfinfo('peppers.png')
ans = 

                  Filename: '/Applications/MATLAB_R2014a.app/toolbox/matla...'
               FileModDate: '02-Apr-2013 15:55:52'
                  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 fields returned by imfinfo aren't the same for all image files. Here's the output for a TIFF file.

imfinfo('forest.tif')
ans = 

                     Filename: '/Applications/MATLAB_R2014a.app/toolbox/im...'
                  FileModDate: '25-Sep-2013 20:12:00'
                     FileSize: 124888
                       Format: 'tif'
                FormatVersion: []
                        Width: 447
                       Height: 301
                     BitDepth: 8
                    ColorType: 'indexed'
              FormatSignature: [73 73 42 0]
                    ByteOrder: 'little-endian'
               NewSubFileType: 0
                BitsPerSample: 8
                  Compression: 'PackBits'
    PhotometricInterpretation: 'RGB Palette'
                 StripOffsets: [1x17 double]
              SamplesPerPixel: 1
                 RowsPerStrip: 18
              StripByteCounts: [1x17 double]
                  XResolution: 72
                  YResolution: 72
               ResolutionUnit: 'Inch'
                     Colormap: [256x3 double]
          PlanarConfiguration: 'Chunky'
                    TileWidth: []
                   TileLength: []
                  TileOffsets: []
               TileByteCounts: []
                  Orientation: 1
                    FillOrder: 1
             GrayResponseUnit: 0.0100
               MaxSampleValue: 255
               MinSampleValue: 0
                 Thresholding: 1
                       Offset: 122964
             ImageDescription: 'Carmanah Ancient Forest, British Columbia,...'

Only the first nine fields of the returned struct are always the same:

  • Filename
  • FileModDate
  • FileSize
  • Format
  • FormatVersion
  • Width
  • Height
  • BitDepth
  • ColorType

FileModDate is the file modification date (as reported by the operating system). FileSize is the size of the file in bytes. The peppers.png file takes up 287,677 bytes on disk.

Format is an abbreviation indicating the type of image file (PNG, TIFF, JPEG, etc.). Although frequently the filename extension indicates the image format type, this isn't always the case. The Format field shows you the format type whether or not the filename extension indicates it.

FormatVersion has turned out to be less useful than I thought would be back in the mid-1990s. You generally don't need to pay attention to it.

Width is the number of image columns, and Height is the number of rows.

If I were designing imfinfo from scratch today, I'd probably call the next field BitsPerPixel instead of BitDepth. I might also add a field called SamplesPerPixel. Anyway, images such as peppers.png used to be frequently called "24-bit images" because each pixel is represented by three samples (one number for red, one for green, and one for blue), and each sample is represented by 8 bits. 3*8 is 24, hence 24-bit image.

The file forest.tif, on the other hand, uses only one sample for each pixel, and each sample is represented by 8 bits, so forest.tif is sometimes called an "8-bit image," and its BitDepth is 8.

ColorType tells us how the pixel values are interpreted as colors on the screen. The most common values for ColorType are 'grayscale', 'indexed', and 'truecolor'. In a grayscale image, each pixel value represents a shade of gray. In an indexed image, each pixel value is really a lookup index used to get the pixel color from a colormap (also called a palette). Truecolor (in this context) originally referred to a 24-bit image with three samples per pixels, and with the samples representing red, green, and blue. In MATLAB, though, we use the term to refer to any RGB image represented by three samples per pixel, regardless of the bit depth.

Here's an example of using the information returned by imfinfo to compute the compression ratio for a JPEG file. Let's use ngc6543a.jpg, the first truecolor sample image shipped with MATLAB.

info = imfinfo('ngc6543a.jpg');
file_bytes = info.FileSize;
image_bytes = info.Width * info.Height * info.BitDepth / 8;

I divided by 8 in the line above because BitDepth is in bits instead of bytes.

compression_ratio = image_bytes / file_bytes
compression_ratio =

   42.7210

So what about all those other fields after the first nine? Well, they vary depending on the file format and the specific information contained in the file. For example, TIFF files optionally contain horizontal and vertical resolution information.

info = imfinfo('forest.tif');
info.XResolution
ans =

    72

info.YResolution
ans =

    72

info.ResolutionUnit
ans =

Inch

Most image file formats allow you to store text descriptions in them.

info = imfinfo('peppers.png');
info.Description
ans =

Zesty peppers

These days, most cameras store a lot more information in image files than the older sample images that are in MATLAB and the Image Processing Toolbox. This information is also available using imfinfo.

imshow('IMG_1046.JPG','InitialMagnification',20)
info = imfinfo('IMG_1046.JPG')
info = 

            Filename: '/Users/steve/Dropbox/MATLAB/Blogs/IMG_1046.JPG'
         FileModDate: '28-Jun-2014 22:07:32'
            FileSize: 1595137
              Format: 'jpg'
       FormatVersion: ''
               Width: 2816
              Height: 1880
            BitDepth: 24
           ColorType: 'truecolor'
     FormatSignature: ''
     NumberOfSamples: 3
        CodingMethod: 'Huffman'
       CodingProcess: 'Sequential'
             Comment: {}
    ImageDescription: '                               '
                Make: 'Canon'
               Model: 'Canon PowerShot S95'
         Orientation: 1
         XResolution: 180
         YResolution: 180
      ResolutionUnit: 'Inch'
            DateTime: '2014:06:28 19:07:32'
    YCbCrPositioning: 'Co-sited'
       DigitalCamera: [1x1 struct]
       ExifThumbnail: [1x1 struct]

That extra information is in the DigitalCamera field.

info.DigitalCamera
ans = 

                ExposureTime: 0.0040
                     FNumber: 4.9000
             ISOSpeedRatings: 80
                 ExifVersion: [48 50 51 48]
            DateTimeOriginal: '2014:06:28 19:07:32'
           DateTimeDigitized: '2014:06:28 19:07:32'
     ComponentsConfiguration: 'YCbCr'
      CompressedBitsPerPixel: 3
           ShutterSpeedValue: 7.9688
               ApertureValue: 4.5938
           ExposureBiasValue: 0
            MaxApertureValue: 4.5938
                MeteringMode: 'Pattern'
                       Flash: 'Flash did not fire, no strobe return detect...'
                 FocalLength: 22.5000
                   MakerNote: [1x2764 double]
                 UserComment: [1x264 double]
             FlashpixVersion: [48 49 48 48]
                  ColorSpace: 'sRGB'
            CPixelXDimension: 2816
            CPixelYDimension: 1880
         InteroperabilityIFD: [1x1 struct]
       FocalPlaneXResolution: 9.6438e+03
       FocalPlaneYResolution: 9.6575e+03
    FocalPlaneResolutionUnit: 2
               SensingMethod: 'One-chip color area sensor'
                  FileSource: 'DSC'
              CustomRendered: 'Normal process'
                ExposureMode: 'Auto exposure'
                WhiteBalance: 'Auto white balance'
            DigitalZoomRatio: 1
            SceneCaptureType: 'Standard'
                 UnknownTags: [1x1 struct]

Do you have questions about imfinfo? Do you ever use it for writing batch processing scripts? I'd be interested to hear about it, so please leave a comment below.


Get the MATLAB code

Published with MATLAB® R2014a

Note

Comments are closed.

6 CommentsOldest to Newest

fabio replied on : 1 of 6
How to get the serial number of the camera? I have some couples of photos taken from two 'equal' cameras (the same model and conditions). How to distiguish one camera from the other?
Will replied on : 2 of 6
My wife has just started an "event photography" business. She had a requirement to modify photos streaming in from the camera over wifi. Essentially continuously monitor a folder and when a photo arrives perform a number of basic steps:
1. Work out, from the exif data, what orientation the photo is (portrait or landscape)
2. Rotate the image accordingly
3. Resample the image to a much smaller size suitable for image browsing purposes
4. Overlay a watermark graphic
5. Save the modified file to another folder
Yes, this probably could have been done with Photoshop, but with the resulting compiled application (actually a sort of daemon really as it just ran in the background) it could be deployed on the required PC with no need for an additional license. Not complicated, but all of this took minutes with Matlab - love it!
Will replied on : 3 of 6
in answer to fabio, for me in the DigitalCamera structure of imfinfo yin addition to "camera make" and "model" there is also a "UnknownTags" structure which has a an ID field. Perhaps this could be the Serial Number? Or at least a method of unique identification?
a=imfinfo('C:\MyImage.jpg');
a.DigitalCamera.UnknownTags.ID % can use this field?
Steve Eddins replied on : 4 of 6
Nice application, Will! Also, that's a good idea about looking in the UnknownTags field. Those are usually proprietary manufacturer tags, so it can be hard to tell for sure what they mean.
John replied on : 5 of 6
Steve, I use IMFINFO quite a bit. In my environment we have many custom, or one-off tailored versions of, image file formats, in addition to many of the popular commercial formats. After many years of struggling to deal with these many formats (how to read, how to display, how to separate reads & format issues from display & interface issues) I finally broke down and embraced the way of MATLAB, which is, evidently, properly called the way of Steve. Now I simply define a custom IMXXXINFO file for each custom format. In it, I designed my own extension to the standard fields, that are particularly relevant for me. Namely, endianness, complexity, blocked/not blocked, data type, number of image segments, void values, etc... These are all easy adds to the predefined set, which I think I took from a buried private function in the IPT. I update the the image format registry on startup by adding the handles for IMXXXINFO, as well as, of course, handles to read (my own reader that deals with all the weirdness), and sometimes write. This has made things 1000x simpler. I only wish I would have "gotten it" so much earlier. So thanks Steve. Your system really works!
Steve Eddins replied on : 6 of 6
John—Fascinating! This is the first time I've heard of someone taking advantage of the extensibility of imread, imfinfo, and imwrite.