Steve on Image Processing

Concepts, algorithms & MATLAB

Computing distance using image file information 25

Posted by Steve Eddins,

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 = '';
bw = imread(url);


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.

ans =


ans =


ans =


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:

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 =


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 =


Get the MATLAB code

Published with MATLAB® 7.4

25 CommentsOldest to Newest

Daphne W replied on : 1 of 25

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.

Steve replied on : 2 of 25

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

ziela replied on : 3 of 25

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


what command should I use to access the imfinfo?

Steve replied on : 4 of 25

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.

Sribalamurugan replied on : 7 of 25

Hi steve….
Im a research student… 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….

Khairi replied on : 9 of 25

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][/IMG]


Oliver replied on : 11 of 25

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.

Steve replied on : 12 of 25

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.

Rachel replied on : 13 of 25

Hi Steve –

What about computing distances in spectral images (e.g. FFT)? As you move from the center of a FFT spectrum, each pixel position is related to a spatial distance x = A/p where p = pixel position in the r-direction and A = a constant relating to the size of the image. How does matlab choose A and its units when using fft2 followed by fftshift? I have seen other image processing programs choose a value based on the size and “resolution unit” of the image (e.g. 4 inches).


Steve replied on : 14 of 25

Rachel—The MATLAB fft and fft2 functions compute the discrete Fourier transform (DFT). Transform input and output samples are defined on a domain of integers from 0 to N-1. The input and output domains are unitless. And that’s all those functions do. There is no automatic connection made to physical units of any kind. So you’ll need to make the association with physical units in your own code.

The Signal Processing Toolbox has frequency-spectra computation and plotting functions that handle physical units automatically, but the Image Processing Toolbox does not have similar functions for 2-D signals.

robs replied on : 15 of 25

Hi Steve,
I wanted to calculate the distance of 2 objects in the image. What are the external parameters required for me. Also is it possible to extract the focal length of the camera form the image using matlab.

jseabra replied on : 17 of 25

Hello Steve,

I saw this question in another post and still can’t find the answer: what if the image is not tif format but jpeg? With a simple software for image edition I can easily access the image resolution, particularly the dpi values. Do you have any idea how can I get this info with MatLab?


jack replied on : 19 of 25

Hi Steve,

I am new to Matlab and I am trying to find the distance between pixels in a JPG image. I have got the coordinates of these pixels as well as the coordinate for a reference point.

My idea is to calculate the distance for a few pixels to the reference point. How can this be done?

The coordinate of interested pixels are given by

y =


x =


and the coordinate of centroid is given by

centroid =

   289   315

Alex replied on : 21 of 25

Hi Steve,
I am trying to find the distance to an A4 sheet of paper from a camera, using the picture that it provides.
Would it be possible to do so using this method, and then applying the formula ” (1/p) + (1/q) = 1/f “, to find the real distance p, given the distance between pixels q and focal length f?
I’m not sure if this is correct formula to use.


Simon Silk replied on : 22 of 25

Hi Steve,

I am wondering, if I have performed connected component labeling on a binary image, and I now want to calculate the distance from an arbitrary pixel, say (i,j), to the nearest part of one of the labeled objects, say object 4. Is there a way to calculate this?

Obviously I could go through the entire image and for each pixel first check if it is part of the object to which I want to know the distance, and if so, calculate the euclidean distance to that pixel and track the minimum. However, I was hoping there would be a more compact way to do this.

Thanks again for any recommendations.

Simon Peter Silk replied on : 24 of 25

Thanks Steve, that worked great.

For anyone else who would like to simultaneously calculate the distance to two or more blobs, what I did is copied them into separate images, the used bwdist on each image. Then you can look at a particular pixel and know how far is it from both blob A and bob B, etc.

Add A Comment

What is 1 + 3?

Preview: hide