I have some troubles with part of my code. I would like to find contours after cv.Watershed
algorithm in Python. To be honest, I don't know how to do it.
This is my code:
kernel = np.ones((3, 3), np.uint8)
# sure background area
sure_bg = cv2.dilate(image, kernel, iterations=5)
opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel, iterations=2)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 3)
ret, sure_fg = cv2.threshold(dist_transform, 0.4 * dist_transform.max(), 255, 0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
cv.imshow('mark ', sure_fg)
cv.waitKey(0)
# sure_fg = cv2.erode(sure_fg,kernel,iterations=3)
unknown = cv2.subtract(sure_bg, sure_fg)
# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers + 1
# Now, mark the region of unknown with zero
markers[unknown == 255] = 0
markers = cv2.watershed(img, markers)
m = cv2.convertScaleAbs(markers)
m = cv2.threshold(m, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
img[markers == -1] = [255, 255, 255]
_, contours, _ = cv2.findContours(img[markers == -1], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Marks with img[markers == -1] = [255, 255, 255]
are done perfectly, but how to convert it into contours?
Thanks!