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

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.
Daphne - Yes, you’re right, actual distance requires camera calibration. Thanks for the clarification.
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?
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.
Is it necessary to use the preview function before getsnapshot to acquire one frame?
thanks
Enas—I don’t know; I’m not that familiar with the details of the Image Acquisition Toolbox.
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….
Sribalamurugan—I don’t any suggestions for you.
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
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.”
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.
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.