# 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 Feb 3 '19 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. – Florin Feb 3 '19 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? – dearN Feb 3 '19 at 16:02
• @Florin If your question has been answered, please consider marking one of the answers as Accepted! – Carl Lange Feb 4 '19 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. – Florin Feb 7 '19 at 19:24

Assuming the question is about Mathematica...

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


• Thank you Carl !! Thank you very much! – Florin Feb 3 '19 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.stack.imgur.com/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! – Florin Feb 3 '19 at 14:42
• @ConorCosnett 👋Always nice to see another Irish Mathematica user! – Carl Lange Feb 3 '19 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.) – Conor Cosnett Feb 3 '19 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 😅 – Carl Lange Feb 3 '19 at 16:53
• Certainly. I couldn't turn down an opportunity to do that 😂. – Conor Cosnett Feb 3 '19 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.stack.imgur.com/0wc6Q.jpg"];