Group Abstract Group Abstract

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 = ReplacePar   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

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