'Filling contour won't work using drawContours() thickness=-1 [duplicate]
I am trying to fill a contour which was obtained by separately thresholding 3 color channels.
image_original = cv2.imread(original_image_path)
image_contours = np.zeros((image_original.shape[0], image_original.shape[1], 1), dtype=np.uint8)
image_contour = np.zeros((image_original.shape[0], image_original.shape[1], 1), dtype=np.uint8)
image_binary = np.zeros((image_original.shape[0], image_original.shape[1], 1), dtype=np.uint8)
image_area = image_original.shape[0] * image_original.shape[1]
for channel in range(image_original.shape[2]):
ret, image_thresh = cv2.threshold(image_original[:, :, channel], 120, 255, cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(image_thresh, 1, 1)
for index, contour in enumerate(contours):
if( cv2.contourArea( contour ) > image_area * background_remove_offset ):
del contours[index]
cv2.drawContours(image_contours, contours, -1, (255,255,255), 3)
_, contours, hierarchy = cv2.findContours(image_contours, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image_contour, max(contours, key = cv2.contourArea), -1, (255, 255, 255), 1)
cv2.imwrite(output_contour_image_path, image_contour)
cv2.drawContours(image_binary, max(contours, key = cv2.contourArea), -1, (255, 255, 255), thickness=-1)
cv2.imwrite(output_binary_image_path, image_binary)
cv2.imshow("binary", image_binary)
which is supposed to work by setting the thickness=-1 but it only draws up the contour with 1 thickness same as that of thickness=1 specifically in the following line.
cv2.drawContours(image_binary, max(contours, key = cv2.contourArea), -1, (255, 255, 255), thickness=-1)
Results are as follows,
Which should come up with a binary filled image other than a one just with a contour of thickness=1
Solution 1:[1]
well, solved it it seems the cv2.drawContours()
function need contours as list type, just changing the line
cv2.drawContours(image_binary, max(contours, key = cv2.contourArea), -1, 255, thickness=-1)
to
cv2.drawContours(image_binary, [max(contours, key = cv2.contourArea)], -1, 255, thickness=-1)
Solves it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jeru Luke |