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.
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
25 CommentsOldest to Newest
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 :
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?
Enas—I don’t know; I’m not that familiar with the details of the Image Acquisition Toolbox.
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]
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.”
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.
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).
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.
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.
Robs—Try doing a search for “camera calibration MATLAB”
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?
Jseabra—I’m sorry, I don’t know.
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 = 161 288 288 213 247 288 209 x = 191 291 294 322 335 347 398
and the coordinate of centroid is given by
centroid = 289 315
Jack—Try something like this:
hypot(y-centroid(1), x - centroid(2))
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.
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—Try using bwdist.
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.
I am looking for the way to get the pixel size according to my figure in here
I want to specify the value of X1, X2, Y1 and Y2 then I will know the size of the pixel. Finally, I will have the size of its to get the location of pixel in terms of X and Y.
Please advise me and I am looking forward to hearing from you good news.
Thanks and best regards