Message Boards Message Boards

Distance of lines in image: works but slow

Hi all,

my typical (raster) image consists of many lines that are changing their distance to the other lines. The data are extracted from a biological sample that shows a pattern.

I want to calculate for each point of the lines the distance to the closest other line. My idea is to take a small portion of the image around the target point, delete all (bright) pixels that are directly connected, and calculate the minimum of all the distances to the left-over (bright) pixels.

s0 = 20; (* half size of the sub-image *)
d0 = ImageData[img0];
a0 = ReplacePart[
   ConstantArray[0, {2 s0 + 1, 2 s0 + 1}], {s0 + 1, s0 + 1} -> 
    1]; (* make an image with a white pixel in the center *)
pos1 = DeleteCases[
  Position[d0, 
   1], _?(#[[1]] <= s0 || #[[2]] <= s0 || #[[1]] >= 
       Dimensions[d0][[1]] - s0 || #[[2]] >= 
       Dimensions[d0][[2]] - s0 &), 
  1]; (* extract bright pixels from original image *)

repl =
  (imgsm1 = Take[d0, Sequence @@ Outer[Plus, #, {-s0, s0}]]; (* 
     Take sub-image from original image *)
     imgsm2 = Subtract[imgsm1, GeodesicDilation[a0, imgsm1]]; (* 
     delete pixels connected to center pixel *)
     (# -> 
       Min[Sqrt[
         N@(Plus @@@ ((# - {s0 + 1, s0 + 1} & /@ 
                Position[imgsm2, 1])^2))], 100]) (* 
     calculate min distance of all left-over pixles *)
     ) & /@ pos1;

d1 = ReplacePart[d0, repl]; 

img1 = Colorize[Image[d1/25], ColorFunction -> (Hue[1 - #] &), 
  ColorRules -> {0 -> Black}]

The original image and the result are here: img0

img1

The code works but is very slow. Does someone have an idea how to accelerate this?

Thanks!

Max

3 Replies

Dear Henrik,

this is exactly what I was looking for! As always, you nailed it!

Thanks so much again.

Best wishes,

Max

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

enter image description here

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]

enter image description here

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

POSTED BY: Henrik Schachner

Hi Maximilian,

one simple idea would be to overlap your image with its point density:

img = Binarize[ < your original image > ];
dims = ImageDimensions[img];
pos1 = ImageValuePositions[img, 1];
cimg = SmoothDensityHistogram[pos1, 15, Frame -> False, 
   ColorFunction -> "Rainbow", ImageSize -> dims, PlotPoints -> 100, 
   PlotRange -> Full];
ImageMultiply[cimg, img]

enter image description here

Does that help? It runs fast, at least ...

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract