March 2020
Intermediate to advanced
366 pages
9h 8m
English
A common problem with segmentation is that a hard threshold typically results in small imperfections (that is, holes, as in the preceding image) in the segmented region. These holes can be alleviated by using morphological opening and closing. When it is opened, it removes small objects from the foreground (assuming that the objects are bright on a dark foreground), whereas closing removes small holes (dark regions).
This means that we can get rid of the small black regions in our mask by applying morphological closing (dilation followed by erosion) with a small 3 x 3-pixel kernel, as follows:
kernel = np.ones((3, 3), np.uint8)frame = cv2.morphologyEx(frame, cv2.MORPH_CLOSE, kernel)
The result ...