Image file information using imfinfo
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.
评论
要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。