# 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 = 'https://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

Published with MATLAB® 7.4

### Note

Comments are closed.

## 25 CommentsOldest to Newest

**1**of 25

**2**of 25

**3**of 25

**4**of 25

`getdata`function.

**5**of 25

**6**of 25

**7**of 25

**8**of 25

**9**of 25

**10**of 25

`regionprops`. See also my previous post "Intensity-weighted centroids."

**11**of 25

**12**of 25

`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.

**13**of 25

**14**of 25

`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.

**15**of 25

**16**of 25

**17**of 25

**18**of 25

**19**of 25

y = 161 288 288 213 247 288 209 x = 191 291 294 322 335 347 398and the coordinate of centroid is given by

centroid = 289 315

**20**of 25

hypot(y-centroid(1), x - centroid(2))

**21**of 25

**22**of 25

**23**of 25

**24**of 25

**25**of 25

## Recent Comments