我在图像中找到轮廓。对于我找到的每个轮廓,我打印出它的 bouding rect 和 area,然后将其绘制到图像上。有趣的是,我发现已经绘制了 5 个轮廓,而只打印了 4 个轮廓。有谁知道这里发生了什么?
>>contour 1
>>(0, 0, 314, 326)
>>101538.5
>>contour 2
>>(75, 117, 60, 4)
>>172.0
>>contour 3
>>(216, 106, 3, 64)
>>124.0
>>contour 4
>>(62, 18, 138, 9)
>>383.5
import cv2
import numpy as np
img = cv2.imread('1.png')
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
_, contours, hier = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
for i,c in enumerate(contours):
rect = cv2.boundingRect(c)
area = cv2.contourArea(c)
print("contour " + str(i+1))
print(rect)
print(area)
cv2.drawContours(img, contours, -1, (0,255,0), 1)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
撒科打诨
相关分类