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 = '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.
ans = 100
ans = 100
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
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.