Message Boards Message Boards

[✓] Measure distance between objects in an image?

GROUPS:

Hello, I would require to create an image measurement tool (to measure distance between objects in an image) in Mathematica. It is not available in the documentation.

Thanks

POSTED BY: Man Oj
Answer
5 months ago

Hello Man Oj,

with Mathematica it is no problem to make such a tool. Here comes a simple example:

fourLocators[img_Image, mag_: 1] := DynamicModule[{imgdim, pt0, pt},
  imgdim = ImageDimensions[img];
  pt0 = {imgdim[[1]]/2, imgdim[[2]]/2};
  pt = {pt0 + {1, 1}, pt0 + {1, -1}, pt0 + {-1, 1}, pt0 + {-1, -1}};
  Column@{LocatorPane[Dynamic[pt], 
     Image[img, ImageSize -> mag imgdim], 
     Appearance -> {Style["+", Red], Style["+", Red],
       Style["+", Green], Style["+", Green]}],
    Dynamic["DistRed" -> EuclideanDistance[pt[[1]], pt[[2]]]], 
    Dynamic["DistGreen" -> EuclideanDistance[pt[[3]], pt[[4]]]]}
  ]

Let's try:

img = Image@DensityPlot[Sin[x] Cos[y/2], {x, -5, 5}, {y, -5, 5}, Frame -> False, PlotPoints -> 50];
fourLocators[img]

gives

enter image description here

Here the distances are given in terms of image pixels.

Hope that helps, regards -- Henrik

POSTED BY: Henrik Schachner
Answer
5 months ago

A simpler alternative to @Henrik's nice version:

i = ExampleData[{"TestImage", "House"}];

Manipulate[
     Labeled[Show[i, Graphics[Line[pt]]], EuclideanDistance @@ pt],
    {{pt, Min[ImageDimensions[i]] {{.3, .3}, {.6, .6}}}, Locator}]

enter image description here

POSTED BY: Vitaliy Kaurov
Answer
5 months ago

Dear @Vitaliy Kaurov,

excellent - I always prefer simple solutions! Thanks for sharing!

Best regards -- Henrik

POSTED BY: Henrik Schachner
Answer
5 months ago

Hi Vitality/Henrik,

Thanks for help.

Regs

POSTED BY: Man Oj
Answer
5 months ago

Group Abstract Group Abstract