您好,我正在关注下一个文档“ https://pysource.com/2018/09/25/simple-shape-detection-opencv-with-python-3/”,但遇到下一个错误:
Traceback (most recent call last):
File "e:/Proyectos/Detecciondeobjetos/Untitled-1.py",line 9,in <module>
for cnt in contours:
TypeError: 'NoneType' object is not iterable
这是我的代码:
import cv2
import numpy as np
font = cv2.FONT_HERSHEY_COMPLEX
img = cv2.imread("123.png",cv2.IMREAD_GRAYSCALE)
_,threshold = cv2.threshold(img,240,255,cv2.THRESH_BINARY)
_,contours = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
cv2.drawContours(img,[approx],(0),5)
x = approx.ravel()[0]
y = approx.ravel()[1]
if len(approx) == 3:
cv2.putText(img,"Triangle",(x,y),font,1,(0))
elif len(approx) == 4:
cv2.putText(img,"Rectangle",(0))
elif len(approx) == 5:
cv2.putText(img,"Pentagon",(0))
elif 6 < len(approx) < 15:
cv2.putText(img,"Ellipse",(0))
else:
cv2.putText(img,"Circle",(0))
cv2.imshow("shapes",img)
cv2.imshow("Threshold",threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()
this is the image i am using