# I have an image of an irregular hole in the sidewalk and i want to know how to find the diameter of the hole? Below is what i have tried so far

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

Below is what i have tried so far

Length[image]

Lenght[image[[1]]]

• Hmmmmm.... You're going to have to give us a little more to go on. Image? Link? – kale Oct 25 '16 at 1:48
• I agree with kale, how about the picture you are working with? – bobbym Oct 25 '16 at 1:51
• donnasbigredchair.love/wp-content/uploads/2013/12/… sorry about that, here is a link to the image – Isaac Oct 25 '16 at 1:51
• 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! – user9660 Oct 25 '16 at 4:32

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]


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

ColorNegate[Image[%]]


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

DeleteSmallComponents[%]


The we use Closing to close the interior:

Closing[%, 1]


And finally use ComponentMeasurements to find the area:

ComponentMeasurements[%, "MeanCaliperDiameter"]

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