繁体   English   中英

typeError:'NoneType'对象没有属性'__getitem__'python

[英]typeError: 'NoneType' object has no attribute '__getitem__' python

我是OpenCV的新手,尝试自己学习,我运行此代码表单教程:

img=cv2.imread('whatever.jpg')  
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
edges=cv2.Canny(gray,50,150,apertureSize=3)  
lines=cv2.HoughLines(gray,1,np.pi/180,200)  
for rho,theta in lines[0]:  
    a=np.cos(theta)  
    b=np.sin(theta)  
    x0=a*rho  
    y0=b*rho  
    x1=int(x0+1000*(-b))  
    y1=int(y0+1000*(a))  
    x2=int(x0-1000*(-b))
    y2=int(y0-1000*(a))  
    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imwrite('somthing.jpg',img)

我收到此错误

Traceback (most recent call last):  
File "nothing", line 8, in <module>  
for rho,theta in lines[0]:  
TypeError: 'NoneType' object has no attribute '__getitem__'  

我正在寻找此类问题的答案,但没有得到分词lines[0]
感谢帮助

cv2库函数并不总是返回值。 您应该检查以下内容的返回值:

img=cv2.imread('whatever.jpg')  
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
edges=cv2.Canny(gray,50,150,apertureSize=3)  
lines=cv2.HoughLines(gray,1,np.pi/180,200) 

并且您应该添加try-catch行。 如果输入相同的错误,请尝试此操作。

img=cv2.imread(os.path.join(filePath,'image1.png'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM