# measure minimum distance of dots from edge

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

Image of bright dots:

Edge image

-
– Dr. belisarius Nov 5 '13 at 0:06

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


We're looking for the exterior of the figure

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


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]


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.

-
I voted to close this one, now I am ashamed. Good one +1 – Dr. 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