Thank you !
I actually created a black image (image2) and filled it with the blue contours calculated in white. Then I found all white pixels in the picture and used these coordinates to apply cv2.fillPoly on the original image.
Here is the code :
import cv2
from google.colab.patches import cv2_imshow
from google.colab import drive
import numpy as np
filePath = '/gdrive/My Drive/teste17.png'
image = cv2.imread(filePath)
height,width,channel = image.shape
print(channel)
#image = cv2.rotate(image, cv2.cv2.ROTATE_90_CLOCKWISE)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2_imshow(image)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contour_area = []
for c in contours:
print('area:', cv2.contourArea(c))
contour_area.append((cv2.contourArea(c), c))
print('--- contour_area ---')
for item in contour_area:
print('contour_area:', item[0])
# sort list with `(area, contour)` using only `area`
contour_area = sorted(contour_area, key=lambda x:x[0], reverse=True)
print('--- contour_area - sorted ---')
for item in contour_area:
print('contour_area:', item[0])
# get two the biggest contours
print('--- two the biggest contours ---')
print('p0:', contour_area[0][0])
print('p1:', contour_area[1][0])
image2 = np.zeros((height, width, 3), dtype = "uint8")
# draw them
coords1 = np.vstack([contour_area[0][1], contour_area[1][1]])
cv2.fillPoly(image2, [coords1], (255, 255, 255))
gray = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
coords = np.column_stack(np.where(thresh1 > 0))
c2=np.stack((coords[:,1], coords[:,0]), axis=-1)
cv2.fillPoly(image, [c2], (255, 255, 255))
cv2_imshow(image)
And I got the right output :
Once again, thank you for your help !