Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to measure minimal distance of small dots from the edge of an image:

Image of bright dots:

enter image description here

Edge image

enter image description here

ImageAdd looks like following:

enter image description here

share|improve this question
1  
What have you tried so far? –  Hector Nov 4 '13 at 23:59
    
add comment

1 Answer

up vote 9 down vote accepted

Here's an answer that's based on image manipulation rather than list manipulation. Name the two images

dots=Import["http://i.stack.imgur.com/SKNn4.png"];
edges=Import["http://i.stack.imgur.com/2Jbie.png"];

Find the components and visualize

components = MorphologicalComponents[ColorNegate[Dilation[edges, 1]]];
Colorize@components

enter image description here

We're looking for the exterior of the figure

mask = Image[1 - Sign[Abs[ImageData[Image[components]] - 2.]]]

enter image description here

The idea is to take the DistanceTransform and then find the point with the smallest distance transform from the edge. This can be visualized as

ImageAdjust@DistanceTransform[mask]

enter image description here

Now the product of the dots image times the distanceTransformed image gives pixel values that are the distances from the object:

Min[ImageData[dots] ImageData[DistanceTransform[mask]] /. {0. -> 100}]
3.

The smallest of these that is nonzero is the smallest distance. So the closest of the dots is three away from the nearest pixel on the edge image.

share|improve this answer
    
I voted to close this one, now I am ashamed. Good one +1 –  belisarius Nov 5 '13 at 1:55
    
@belisarius -- the DistanceTransform is useful in a surprising number of situations. –  bill s Nov 5 '13 at 3:05
    
@bills If you compare the image of dots with above multiplication, then both looks same. Does the list of distances from above multiplication are the distance of each dots? –  Thomas Nov 5 '13 at 17:35
    
The values you find in the distance transform are the distances between every point and the boundary of the object. By doing the matrix multiplication, we zero out everything that isn't one of the points of interest (i.e., the dots). So the min over all the values that occur where the dots are is the min you are looking for. The 0.->100 is just to remove all the interior points from the min. –  bill s Nov 5 '13 at 17:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.