# distance between nearest points [closed]

I have an image where i found the skeleton (red) and edges (blue). How can I calculate for each red points the distance to closest blue point? I though of something like this:

ComponentMeasurements[MaxDetect[dist], "Centroid"]


But i do not know how to apply it to all red points.

for edges:

binary // GradientFilter[#, 2] & // ImageAdjust // ColorNegate //
ImageRotate[#, -90 Degree] & // Binarize //
ImageData // (borders = #) & // Position[#, 0] & // Point // Graphics[{Blue,
#}] & // (edgy = #) &;

binary // Erosion[#, DiskMatrix[2]] & // ColorNegate // SkeletonTransform //
Pruning[#, 25] & // (skel = #) &//ImageRotate[#, -90 Degree] & // Binarize
// ImageData // (mask = #) &// Position[#, 1] & // (points = #) &//// Point
// Graphics[{Red, #}] & // (sk = #) &

Show[binary, edgy, sk]


## closed as off-topic by Daniel Lichtblau, MarcoB, Henrik Schumacher, José Antonio Díaz Navas, rcollyerJul 23 '18 at 3:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Daniel Lichtblau, MarcoB, Henrik Schumacher, José Antonio Díaz Navas, rcollyer
If this question can be reworded to fit the rules in the help center, please edit the question.

• You could create a binary image where all blue pixels are 0 and all other pixels are 1. Then use DistanceTransform to get an image where each pixel contains the distance to the nearest blue pixel. Then look up the red pixel's position in the distance transform – Niki Estner Jul 4 '18 at 8:49
• You ought to provide the original image and the code used to find the red and blue parts to make it possible for people to experiment. – C. E. Jul 4 '18 at 8:53
• done. there might be ColorReverse missing but i hope not – dziakku Jul 4 '18 at 9:03
• Doesn't the SkeletonTransform result contain the distance to the nearest background pixel for each skeleton pixel? It seems that's the exactly information you want, except you throw it away by binarizing the skeleton image. – Niki Estner Jul 4 '18 at 10:31
• I do not see what is the full set of code steps to get to the blue and red point sets. – Daniel Lichtblau Jul 8 '18 at 18:31

If you have the arrays of coordinates of blue and red points, then you can take the point of shorter list (let's say the bluelist) and use Nearest

pairs={};
rs=10;
Do[
pt=Nearest[redlist, bluelist[[i]],{1,rs}];
redlist=DeleteCases[redlist,pt];
pairs=Join[pairs,{pt,bluelist[[i]]}]
,{i,1,Length@bluelist}]


The rs defines the search radius within which it will search the corresponding point. DeleteCases needed to exclude the situations when one point will be used for some pairs.

Topological network... how long ago this was the theme of my work :)

• few questions: the outcome {{{2, 102}}, {1, 102}, {{2, 103}},....} are those positions of those pairs? and how come I have 2x pairs compared to length of shorter list? – dziakku Jul 4 '18 at 9:57
• pairs = {}; rs = 50; Table[pt = Nearest[blue, red[[i]], {1, rs}][[1]]; blue = DeleteCases[blue, pt]; distances = EuclideanDistance[red[[i]], pt], {i, 1, Length@red}] this code works:) thank you! – dziakku Jul 4 '18 at 10:47