Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an easy way to select for morphological components which do not border/touch the edges of an image?

Taking this doodle as an example:

enter image description here

We'd want to only select morphological components that stay at least some critical distance from the edges of the image.

share|improve this question
For example, after labeling all connected components you can check pixels on the border and mark objects that you meet as unlabeled – Eddy_Em Sep 2 '13 at 20:40
Are the morphological components you have in mind the same as the ones shown by MorphologicalComponents[yourimage] // Colorize? – Michael E2 Sep 2 '13 at 20:46
How about adding a border to the picture and then removing the border component (with everything attached to it)? – DavidC Sep 2 '13 at 20:48
How about DeleteBorderComponents[yourimage]? – Michael E2 Sep 2 '13 at 20:49
@MichaelE2 hit the nail on the head, imho. – DavidC Sep 2 '13 at 20:51
up vote 5 down vote accepted

DeleteBorderComponents does what you ask. You can crop or pad the border, I suppose, if you wish to delete components that are close to the border.

img = Import[""];

Mathematica graphics

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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