OK, a better attempt:
As far as I can see in your code you do not make use of any morphological functions nor of Nearest
- but this could speed up your code considerably. Already MorphologicalComponents
can disassemble your structures perfectly:
img = DeleteSmallComponents[Binarize[
Import["https://community.wolfram.com//c/portal/getImageAttachment?filename=img0.png&userId=549626"]], 20];
MorphologicalComponents[img] // Colorize

In this sense I do the following:
(* list of points according to structures: *)
structPts = Position[#, 1] &@*Normal@*Last /@ ComponentMeasurements[img, "Mask"];
(* global: matrix to be filled with neares neighbor pixel distances: *)
distMat = ConstantArray[-1, ImageDimensions[img]];
(* calculation of nearest neighbor distances *)
lineDistCalc[indx_] := Module[{lpts, environment, nfunc},
{lpts, environment} = TakeDrop[structPts, {indx}];
lpts = First[lpts];
environment = Flatten[environment, 1];
nfunc = Nearest[environment];
With[{r = First[#], c = Last[#]},
distMat[[r, c]] = N@EuclideanDistance[First@nfunc[#], #]] & /@
lpts;]
(* filling matrix 'distMat': *)
Do[lineDistCalc[indx], {indx, Length[structPts]}]
Now distMat
is filled with distance values in place for each pixel - the essential information!
A nice colored image can be calculated from this like so:
(* range of distances: *)
distRange = MinMax[Select[Flatten[distMat], # > 0 &]];
(* according image data for colored image: *)
distImgData = Map[If[# > 0, ColorData["BrightBands"][Rescale[#, distRange]], Black] &, distMat, {2}];
Image[distImgData]

I guess this is more what you are expecting - and (due to usage of Wolfram functions) it runs reasonable fast!
Best regards to Freiburg! -- Henrik