Similar to what @WhiteboxDev suggested, another filter type that could be used is a sieve filter
. Instead of looking at whether the 0s or 1s "win" for a given region, it looks at the number of neighbours a given pixel has, that matches its starting value, and chains those neighbours together.
For example, the following case:
0 0 0
1 1 1
0 0 0
With a majority filter, this would end up as all 0
, while a sieve filter
, with threshold of 3 would keep the area as is. The strength of a sieve filter
is that you can keep long narrow features, which may be desirable, for some applications - for example a binary classification of flooded areas, where a majority filter could end up removing well defined streams.