在进行图像分割的过程中,由于算法的不稳定或者图像质量的问题,会造成图像孔洞出现,这个时候就需要对图像中的孔洞进行填充,具体函数如下
def fillHole(im_in):
im_floodfill = im_in.copy()
# Mask used to flood filling.
# Notice the size needs to be 2 pixels than the image.
h, w = im_in.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
# Floodfill from point (0, 0)
cv2.floodFill(im_floodfill, mask, (0,0), 255);
# Invert floodfilled image
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
# Combine the two images to get the foreground.
im_out = im_in | im_floodfill_inv
return im_out
这里调用了 opencv 库中的泛洪函数。我们将图像输入就可以获得整幅图像填充后的结果。