I'm not sure how imfill works. This is true:
import numpy as np import scipy.ndimage.morphology as morphology bw = np.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) print(morphology.binary_fill_holes(bw).astype('int'))
gives
[[0 0 0 0 0 0 0 0] [0 1 1 1 1 1 0 0] [0 1 1 1 1 1 0 0] [0 1 1 1 1 1 0 0] [0 1 1 1 1 1 0 0] [0 1 1 1 1 0 0 0] [0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0]]
If so, you can check out the scipy morphology package .