opencv python中封闭区域的大小

时间:2017-12-21 07:23:03

标签: python python-3.x opencv opencv-contour watershed

尊重所有人,

我在附图中使用分水岭算法,现在我想找到封闭边界图像的大小(图像中的不规则对象)。

附有图片: enter image description here

1 个答案:

答案 0 :(得分:2)

更新:

在OpenCV 3.4中,cv2.findContours返回image, contours, hierarchy

cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy

enter image description here

在OpenCV 4.0中,cv2.findContours返回contours, hierarchy

cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy

enter image description here

因此,要在脱粒二进制图像中查找轮廓并计算区域,请使用:

cnts= cv.findContours(threshed, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)[-2]
for cnt in cnts:
    area = cv2.contourArea(cnt)
    print(area)