3
$\begingroup$

I have an image of an irregular hole in the sidewalk and i want to know how to find the diameter of the hole?

enter image description here

Below is what i have tried so far

Length[image]

Lenght[image[[1]]]
$\endgroup$
  • 1
    $\begingroup$ Hmmmmm.... You're going to have to give us a little more to go on. Image? Link? $\endgroup$ – kale Oct 25 '16 at 1:48
  • $\begingroup$ I agree with kale, how about the picture you are working with? $\endgroup$ – bobbym Oct 25 '16 at 1:51
  • $\begingroup$ donnasbigredchair.love/wp-content/uploads/2013/12/… sorry about that, here is a link to the image $\endgroup$ – Isaac Oct 25 '16 at 1:51
  • $\begingroup$ Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Oct 25 '16 at 4:32
10
$\begingroup$

Your image is of dimensions:

ImageDimensions[img]

{500, 375}

Here's a way to measure the area of the irregular hole:

ComponentMeasurements[Closing[
 DeleteSmallComponents[ColorNegate@Image@MorphologicalComponents[img, 0.05]], 1], "Area"]

{1 -> 16823.}

Or 9.0% of the total image area.

Since the hole is irregular, we must use MeanCaliperDiameter.

ComponentMeasurements[Closing[
 DeleteSmallComponents[ColorNegate@Image@MorphologicalComponents[img, 0.05]], 1], "MeanCaliperDiameter"]

{1 -> 173.481}

This is the average diameter of all caliper measurements. If you want the min or max diameter substitute with CaliperWidth or CaliberLength, respectively.

For a step-by-step of what this does:

MorphologicalComponents isolates components of an image (use Colorize to visualize):

MorphologicalComponents[img, 0.05]

Mathematica graphics

Next, we negate the color and convert to an image:

ColorNegate[Image[%]]

Mathematica graphics

Next, we delete the small components of the image. The default threshold works well.

DeleteSmallComponents[%]

Mathematica graphics

The we use Closing to close the interior:

Closing[%, 1]

Mathematica graphics

And finally use ComponentMeasurements to find the area:

ComponentMeasurements[%, "MeanCaliperDiameter"]
$\endgroup$
  • $\begingroup$ Why did u put "@" signs in between commmands? $\endgroup$ – Isaac Oct 25 '16 at 2:36
  • $\begingroup$ @Isaac That is prefix notation in Mathematica. f@x is essentially equal to f[x]. $\endgroup$ – kale Oct 25 '16 at 2:38
  • $\begingroup$ Oh nevermind you edited your post $\endgroup$ – Isaac Oct 25 '16 at 2:38
  • 3
    $\begingroup$ The original question asks for the diameter, which I believe is given by ComponentMeasurements[%, "CaliperLength"]. $\endgroup$ – Rahul Oct 25 '16 at 5:37
  • $\begingroup$ Area is not the same as diameter. That is why my down vote. $\endgroup$ – yarchik Oct 25 '16 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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