5
$\begingroup$

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

$\endgroup$
1

1 Answer 1

9
$\begingroup$

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

dots=Import["https://i.sstatic.net/SKNn4.png"];
edges=Import["https://i.sstatic.net/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.

$\endgroup$
4
  • $\begingroup$ I voted to close this one, now I am ashamed. Good one +1 $\endgroup$ Commented Nov 5, 2013 at 1:55
  • $\begingroup$ @belisarius -- the DistanceTransform is useful in a surprising number of situations. $\endgroup$
    – bill s
    Commented Nov 5, 2013 at 3:05
  • $\begingroup$ @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? $\endgroup$
    – Thomas
    Commented Nov 5, 2013 at 17:35
  • $\begingroup$ 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. $\endgroup$
    – bill s
    Commented Nov 5, 2013 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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