I have a series of images in which the pixels take integer values corresponding to a classification. I'd like to try some morphological transformations (e.g. erosion, dilation) to remove noise and smooth across pixels that are classified differently than its neighbors.

However the only morphological transformation functions I know in Python operate on continuous values. This is obviously a problem for discrete values.

I'm sure other people have wanted to apply the techniques to images with classification values. Does anyone know of functions that exist for this sort of thing?

  • $\begingroup$ How many classes do you have? $\endgroup$ Dec 1, 2015 at 16:42
  • $\begingroup$ 70 classes but only a few (3-5) classes are usually present in a single image. So image 1 might have classes 3, 17, 44, and 65; image 2 might have classes 2, 17, 19, and 30. $\endgroup$ Dec 1, 2015 at 16:52

1 Answer 1


Morphological operators are primarily defined over binary images where values 0,1 could correspond to areas identified as foreground and background respectively.

In your case, you can create a binary image of the region you want to apply the operator on by assigning the "islands" of noisy classified regions to the "foreground" (e.g 4 now becomes the value 1) and the broad area around them that is consistently classified in a stable way to the "foreground" (e.g value 3). Apply erosion to this image and then substitute the pixels back from the new binary image to the segmentation mask.

Alternatively, if you want to eliminate spurious classifications so that you end up with consistent areas, you could apply nonlinear filters such as median, max and min over some sliding m by n window.

For more information, please see this link and this link

As far as Python is concerned please see this or this and this. Another excellent python module for this job is this one.

(Also, please note that you could apply any kind of filter that could even produce a Real type output (e.g 2.43, 1.76, etc) but either round or truncate its final output to make it to an integer. Not sure these operators would be very helpful though in this specific case.)

Hope this helps.

  • $\begingroup$ This is helpful. I'm a little surprised how mean, min, max, median are all easily implemented but mode is not. To your last point, the classes are not ordinal and dissimilar classes can be located close to each other. So averaging classes 5 and 25, for example, would give me class 15 which is neither. $\endgroup$ Dec 2, 2015 at 15:35
  • $\begingroup$ Yes, a modal mean would also help in "patching up" holes. Indeed, regarding your observation. I had in mind the smoothing effect of a mean which would decrease/eliminate differences between integers and can use rounding. $\endgroup$
    – A_A
    Dec 2, 2015 at 15:41

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.