0
$\begingroup$

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.

enter image description here

binary 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]
$\endgroup$
5
  • $\begingroup$ 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 $\endgroup$ Jul 4 '18 at 8:49
  • $\begingroup$ 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. $\endgroup$
    – C. E.
    Jul 4 '18 at 8:53
  • $\begingroup$ done. there might be ColorReverse missing but i hope not $\endgroup$
    – dziakku
    Jul 4 '18 at 9:03
  • $\begingroup$ 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. $\endgroup$ Jul 4 '18 at 10:31
  • $\begingroup$ I do not see what is the full set of code steps to get to the blue and red point sets. $\endgroup$ Jul 8 '18 at 18:31
2
$\begingroup$

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 :)

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – dziakku
    Jul 4 '18 at 9:57
  • $\begingroup$ 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! $\endgroup$
    – dziakku
    Jul 4 '18 at 10:47

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