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.

  • $\begingroup$ For example, after labeling all connected components you can check pixels on the border and mark objects that you meet as unlabeled $\endgroup$
    – Eddy_Em
    Commented Sep 2, 2013 at 20:40
  • $\begingroup$ Are the morphological components you have in mind the same as the ones shown by MorphologicalComponents[yourimage] // Colorize? $\endgroup$
    – Michael E2
    Commented Sep 2, 2013 at 20:46
  • 1
    $\begingroup$ How about adding a border to the picture and then removing the border component (with everything attached to it)? $\endgroup$
    – DavidC
    Commented Sep 2, 2013 at 20:48
  • 3
    $\begingroup$ How about DeleteBorderComponents[yourimage]? $\endgroup$
    – Michael E2
    Commented Sep 2, 2013 at 20:49
  • 1
    $\begingroup$ @MichaelE2 hit the nail on the head, imho. $\endgroup$
    – DavidC
    Commented Sep 2, 2013 at 20:51

1 Answer 1


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["https://i.sstatic.net/2fQ3j.png"];

Mathematica graphics


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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