Opening and closing images using morphological filters