Morphological operations — these are a few simple operations based on the shape of the image. This is usually done on binary images. The two main morphological operators — it is erosion and dilatation. For a basic understanding of expansion and erosion, refer to this
This output shows that the image is transformed using a thresholding operation when the foreground still contains some noise.
Now we need to remove any small white noise from the image, i.e. in the foreground. For this we can use morphological closure. To remove any small holes in the foreground object, we can use morphological closure. To get the background, we expand the image. The extension expands the object’s border to the background.
Let’s see the code:
From the output image we we can infer that small holes are removed by the close operation, and make sure that any area in the foreground in the result is indeed the foreground.