Find Contours after Watershed opencv

人走茶凉 提交于 2020-05-13 14:03:12

问题


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!


回答1:


You cannot find contours on img but you can using markers.

Now the array markers contains values of -1 which is a signed integer. I converted it to an array containing signed integers markers1 = markers.astype(np.uint8), where values with -1 will be replaced by values of 255. Then applying Otsu threshold on the result I then found contours.

Here is the extra code that you have to add to the existing one:

Code:

img2 = img.copy()
markers1 = markers.astype(np.uint8)
ret, m2 = cv2.threshold(markers1, 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
cv2.imshow('m2', m2)
_, contours, hierarchy = cv2.findContours(m2, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)    
for c in contours:
#    img2 = img.copy()
#    cv2.waitKey(0)
    cv2.drawContours(img2, c, -1, (0, 255, 0), 2)

#cv2.imshow('markers1', markers1)
cv2.imshow('contours', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:



来源:https://stackoverflow.com/questions/50882663/find-contours-after-watershed-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!