In this recipe, you will learn how to use morphological filters to count objects in a binary image. Often, the objects (blobs) in a binary image are overlapping and, before counting them, we need to do some necessary preprocessing, such as blob separation and detection. Morphological erosion can be quite useful in these cases. Then, contour-finding can be used to count the separated objects. You can use morphological watershed segmentation, too, to separate the blobs and then count them.
Counting objects
Getting ready
Let's import the packages and modules required to start with:
import cv2
import numpy as np
import matplotlib.pylab as plt