# Detecting the outer edge of an object in an image

I am trying to extract the outer edge of the attached image. Could you please advice me how to do it?

• Have you tried anything? Is it about Mathematica at all?
– Kuba
Commented Feb 3, 2019 at 14:04
• Yes I have tried it with Mathematica. The problem is that I don't know how to extract the outer contour only. Commented Feb 3, 2019 at 14:23
• @Florin In the future, it would be really great if you could include what you have done, as a code snippet. This is an interesting problem; what is this shape for? Interfaces between liquids? Commented Feb 3, 2019 at 16:02
• @Florin If your question has been answered, please consider marking one of the answers as Accepted! Commented Feb 4, 2019 at 8:15
• Hi drN! OK! It is indeed an interface. However I am a real beginner. Not yet able to share codes. I am learning the ABC. Commented Feb 7, 2019 at 19:24

Assuming the question is about Mathematica...

SelectComponents[
MorphologicalPerimeter@MorphologicalBinarize@i, "Count", -2]

• Thank you Carl !! Thank you very much! Commented Feb 3, 2019 at 14:41

One method to isolate the outer edge in Mathematica is to use the EdgeDetect[] function.

Note: I hand tuned the program for this image (it will not work for other images).

img = Import["https://i.sstatic.net/0wc6Q.jpg"];
EdgeDetect[img, 1, .43] - EdgeDetect[img, 1, .45]

• Thank you Conor !! I have tried with EdgeDetect but I did not know about using it this way. Thank you very much! Commented Feb 3, 2019 at 14:42
• @ConorCosnett 👋Always nice to see another Irish Mathematica user! Commented Feb 3, 2019 at 14:53
• Haha thanks, unfortunately we are a rare breed. (Everyone seems to be wasting their time, reinventing the wheel, in MATLAB and C in these parts.) Commented Feb 3, 2019 at 15:49
• @ConorCosnett Tell me about it. If you're in Dublin feel free to give me a shout and we can grab a coffee/pint and chat about how much better us Mathematica users are than our peers 😅 Commented Feb 3, 2019 at 16:53
• Certainly. I couldn't turn down an opportunity to do that 😂. Commented Feb 3, 2019 at 17:45

An approach that doesn't need too much tweaking is to use LocalAdaptiveBinarize followed by the selection of the largest component:

img = Import["https://i.sstatic.net/0wc6Q.jpg"];
object = ColorNegate@DeleteSmallComponents[LocalAdaptiveBinarize[img, 30]]
MorphologicalPerimeter@SelectComponents[object, "Count", -1]

• Thank you Bill! Thank you very much! Commented Feb 7, 2019 at 19:24