I want to measure minimal distance of small dots from the edge of an image:
Image of bright dots:
Edge image
ImageAdd looks like following:
I want to measure minimal distance of small dots from the edge of an image:
Image of bright dots:
Edge image
ImageAdd looks like following:
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
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.
DistanceTransform
is useful in a surprising number of situations.
$\endgroup$