Like its opposite, the closing operator can be derived from erosion and dilation operations. In this case, the operation performs a dilation followed by an erosion. A dilation operation is commonly used to fill small holes in images. However, the dilation operation will also make small groups of undesirable pixels bigger. By applying the erosion operation on the image after the dilation, some of this effect will be reduced. You can see the details of this operation in the following code:
closing = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)