python TypeError:’NoneType’对象没有属性’__getitem__’
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python TypeError:’NoneType’对象没有属性’__getitem__’,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2039字,纯文字阅读大概需要3分钟。
内容图文
![python TypeError:’NoneType’对象没有属性’__getitem__’](/upload/InfoBanner/zyjiaocheng/749/28c29e17ae2a46b89c6e2dded57a6ac6.jpg)
这次我尝试从Solem’s blog开始的另一个例子.它是一个通过使用Hough变换检测图像中的线条和圆圈的模块.
这是代码(houghlines.py):
import numpy as np
import cv2
"""
Script using OpenCV's Hough transforms for reading images of
simple dials.
"""
# load grayscale image
im = cv2.imread("house2.jpg")
gray_im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
# create version to draw on and blurred version
draw_im = cv2.cvtColor(gray_im, cv2.COLOR_GRAY2BGR)
blur = cv2.GaussianBlur(gray_im, (0,0), 5)
m,n = gray_im.shape
# Hough transform for circles
circles = cv2.HoughCircles(gray_im, cv2.cv.CV_HOUGH_GRADIENT, 2, 10, np.array([]), 20, 60, m/10)[0]
# Hough transform for lines (regular and probabilistic)
edges = cv2.Canny(blur, 20, 60)
lines = cv2.HoughLines(edges, 2, np.pi/90, 40)[0]
plines = cv2.HoughLinesP(edges, 1, np.pi/180, 20, np.array([]), 10)[0]
# draw
for c in circles[:3]:
# green for circles (only draw the 3 strongest)
cv2.circle(draw_im, (c[0],c[1]), c[2], (0,255,0), 2)
for (rho, theta) in lines[:5]:
# blue for infinite lines (only draw the 5 strongest)
x0 = np.cos(theta)*rho
y0 = np.sin(theta)*rho
pt1 = ( int(x0 + (m+n)*(-np.sin(theta))), int(y0 + (m+n)*np.cos(theta)) )
pt2 = ( int(x0 - (m+n)*(-np.sin(theta))), int(y0 - (m+n)*np.cos(theta)) )
cv2.line(draw_im, pt1, pt2, (255,0,0), 2)
for l in plines:
# red for line segments
cv2.line(draw_im, (l[0],l[1]), (l[2],l[3]), (0,0,255), 2)
cv2.imshow("circles",draw_im)
cv2.waitKey()
# save the resulting image
cv2.imwrite("res.jpg",draw_im)
当我在python中执行文件时:
execfile('houghlines.py')
出现以下错误:
File "houghlines.py", line 24, in <module>
lines = cv2.HoughLines(edges, 2, np.pi/90, 40)[0]
TypeError: 'NoneType' object has no attribute '__getitem__'
你们有任何想法如何解决它?
提前致谢.
解决方法:
有时HoughLines和HoughLinesP没有返回.我认为这意味着“没有台词”.我真的很惊讶文档中的示例没有考虑到它.也许这是一个错误.
在任何情况下,您都可以使用简单的if结果来阻止代码失败!=无:或用默认列表替换None(如HoughLinesP(… args …)或[[]]).这不会解决您的线路未被检测到的事实,但它允许您在该情况下执行某些操作而不是失败.
内容总结
以上是互联网集市为您收集整理的python TypeError:’NoneType’对象没有属性’__getitem__’全部内容,希望文章能够帮你解决python TypeError:’NoneType’对象没有属性’__getitem__’所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。