For example in the MATLAB Image Processing Toolbox we have imfill
function. How can I do this in Python?
So I am trying to segment various shapes and remove the interior blacks. I know if I get the borders, segmentation is still easy but I want to detect the shapes in perfect as well. In short, I want to remove the black covered region in my image as shown.
I have tried using bitwise operations, morphological transforms and floodfill. I am new to floodfill and don't properly know how it works.
image=cv2.imread('rec2.jpg')
kernel = np.ones((5,5),np.uint8)
w=int(image.shape[1]*0.3)
h=int(image.shape[0]*0.5)
resized = cv2.resize(image,(w, h), interpolation = cv2.INTER_CUBIC)
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY_INV)[1]
thresh2 = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print(len(cnts[1])) #To verify number of counts
cv2.imshow("Result",np.hstack((thresh,thresh2)))
This is the segmented image so far. But I want those interior black zones to be filled with white too, so that it could be identified as a solid shape.
You can draw filled contours by defining thickness=cv2.FILLED
:
cv2.drawContours(thresh, cnts, -1, (255, 255, 255), thickness=cv2.FILLED)