Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Steve on Image Processing

March 16th, 2007

Computing distance using image file information

Patrick asked for an example on how to compute distances between objects based on resolution information stored in a file. The TIFF format is one that can store resolution information, and the imfinfo function can tell you about it.

Let's look at a simple example:

url = 'http://blogs.mathworks.com/images/steve/122/blobs.tif';
bw = imread(url);
imshow(bw)

Here's the output of imfinfo:

info = imfinfo(url)
info = 

                     Filename: 'C:\TEMP\tp281629'
                  FileModDate: '06-Mar-2007 09:28:16'
                     FileSize: 2176
                       Format: 'tif'
                FormatVersion: []
                        Width: 308
                       Height: 242
                     BitDepth: 1
                    ColorType: 'grayscale'
              FormatSignature: [73 73 42 0]
                    ByteOrder: 'little-endian'
               NewSubFileType: 0
                BitsPerSample: 1
                  Compression: 'CCITT 1D'
    PhotometricInterpretation: 'WhiteIsZero'
                 StripOffsets: [10x1 double]
              SamplesPerPixel: 1
                 RowsPerStrip: 26
              StripByteCounts: [10x1 double]
                  XResolution: 100
                  YResolution: 100
               ResolutionUnit: 'Inch'
                     Colormap: []
          PlanarConfiguration: 'Chunky'
                    TileWidth: []
                   TileLength: []
                  TileOffsets: []
               TileByteCounts: []
                  Orientation: 1
                    FillOrder: 1
             GrayResponseUnit: 0.0100
               MaxSampleValue: 1
               MinSampleValue: 0
                 Thresholding: 1

Notice the XResolution and YResolution fields, as well as the ResolutionUnit field. According to the TIFF specification, the XResolution and YResolution fields are the number of pixels per resolution unit.

info.XResolution
ans =

   100

info.YResolution
ans =

   100

info.ResolutionUnit
ans =

Inch

Now let's use bwlabel and regionprops to compute the centroids of the objects.

L = bwlabel(bw);
s = regionprops(L, 'Centroid')
s = 

4x1 struct array with fields:
    Centroid

Compute the distance (in pixel units) between the first two objects.

delta_x = s(1).Centroid(1) - s(2).Centroid(1);
delta_y = s(1).Centroid(2) - s(2).Centroid(2);
pixel_distance = hypot(delta_x, delta_y)
pixel_distance =

  103.6742

Finally, use the resolution information from the file to convert to physical distance. (Note: this calculation assumes that the horizontal and vertical resolutions are the same.)

physical_distance = pixel_distance / info.XResolution
physical_distance =

    1.0367


Get the MATLAB code

Published with MATLAB® 7.4

12 Responses to “Computing distance using image file information”

  1. Daphne W replied on :

    Isn’t this only good for pixel resolution distances and not actual distances?

    For example, if you have a Tif taken from a camera (e.g. connected to a microscope) you need to factor in the size of each pixel independently (pixel size & magnification). This usually requires some sort of external calibration.

  2. Steve replied on :

    Daphne - Yes, you’re right, actual distance requires camera calibration. Thanks for the clarification.

  3. ziela replied on :

    but what about image captured from webcam? i can’t access the imfinfo because the filename isn’t a string. For example :

    obj=videoinput(’winvideo’);
    frame=getsnapshot(obj);

    what command should I use to access the imfinfo?

  4. Steve replied on :

    ziela - There is information in the Image Acquisition Toolbox Users Guide about getting information about the camera hardware, as well as getting information about a video input object. Also, see the documentation for the getdata function.

  5. Enas replied on :

    Is it necessary to use the preview function before getsnapshot to acquire one frame?
    thanks

  6. Steve replied on :

    Enas—I don’t know; I’m not that familiar with the details of the Image Acquisition Toolbox.

  7. Sribalamurugan replied on :

    Hi steve….
    Im a research student…..you need to clarify me one doubt…in the above example you find the distance for tiff image…i need to find the distane in jpeg files…
    i dont have any idea about this….coz in tiff every detail will be available….

  8. Steve replied on :

    Sribalamurugan—I don’t any suggestions for you.

  9. Khairi replied on :

    Hello Steve. I need your help to find the matlab code for distance from centroid of an image to top, bottom, left and right. I will be using this for my final year project. Hope you can help me. This is my image [IMG]http://img296.imageshack.us/img296/567/psmbx0.jpg[/IMG]

    Regards

  10. Steve replied on :

    Khairi—A centroid would be specified as an (x,y) coordinate pair, which will directly give you distances to the image edges. Binary object centroids can be computed via regionprops. See also my previous post “Intensity-weighted centroids.”

  11. Oliver replied on :

    Hi Steve,

    do you know how to change those tiff tags when you write a new tif image which are not listed in the imwrite specification for tif images. For example: How can I set the TileWidth parameter to 512 when storing an tif image by using imwrite?
    By the way XResolution and YResolution are specified in the imwrite specification of matlab and this works fine.

    I can also live with a trick like:
    unix(sprintf(’convert *badHeader.tiff *goodHeader.tiff’));
    or something from the libtiff
    unix(sprintf(’tiffcp *badHeader.tiff *goodHeader.tiff’));

    Thanks a lot for ideas.

  12. Steve replied on :

    Oliver—imwrite does not support writing tiled TIFF files. It’s not just a matter of changing a field in the header; image data in tiled TIFF is arranged in the file in a completely different fashion than with baseline TIFFs. There may be something in the libtiff utils that can do the conversion for you, but I don’t know.

Leave a Reply


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Mikr: I look for answers before asking people… “But we still can’t see the coordinates!...
  • Steve: Mikr—You might want to take a look at the Getting Started section of the MATLAB documentation in order...
  • Mikr: thanks but is it possible to see and write to file (Excel ?) that matrix of pixel coordinates ? instead of...
  • Steve: Mikr—An image in MATLAB is simply a matrix of pixel values. It can be saved (exported) to several common...
  • Mikr: thanks, Steve just started to learn matlab and to clarify matlab saves image files as a matrix of pixel...
  • Steve: Mikr—As far as I know, the commonly used image file formats such as TIFF, JPEG, PNG, etc., do not...
  • Mikr: how to write pixel coordinates in file ?
  • Steve: M.S.—Code for the bwtraceboundaries function ships with the Image Processing Toolbox.
  • M.S.Cheema: i need to know the detailed algorithm for bwtraceboundaries. i want how that function works. so please...
  • Steve: Wagas—It depends on how much memory you have on your computer. You should be able to load a 94 MB TIFF...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics