【问题标题】:python TypeError: 'NoneType' object has no attribute '__getitem__'python TypeError:'NoneType'对象没有属性'__getitem__'
【发布时间】:2013-04-15 04:08:04
【问题描述】:

这一次我尝试来自Solem's blog 的另一个示例。它是一个使用霍夫变换检测图像中的线和圆的模块。 这是代码(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 函数返回None
  • @segfolt:是的,但documentation 似乎并不表示这是正常行为..
  • 嗯 - 如果你尝试 cv2.HoughLines(edges, 2, np.pi/90, 40, None) 会发生什么?
  • 如果我尝试您的解决方案,错误会“移动”到另一行。这次是:文件“houghlines.py”,第 25 行,在 plines = cv2.HoughLinesP(edges, 1, np.pi/180, 20, np.array([]), 10)[0] TypeError : 'NoneType' 对象没有属性 'getitem'
  • 意味着现在,HoughLinesP 返回 None。如果您不确定 cv2-functions 的输出,请尝试输入“空”图像时会发生什么,其中一张您确定它不包含任何行。

标签: python typeerror nonetype


【解决方案1】:

有时 HoughLines 和 HoughLinesP 不返回任何值。我认为这意味着“没有线”。令我惊讶的是,文档中的示例并未说明这一点。也许这是一个错误。

在任何情况下,您都可以使用简单的 if result != None: 或将 None 替换为默认列表(如 (HoughLinesP(... args ...) or [[]]))来阻止代码失败。这并不能解决您的线路未被检测到的事实,但它允许您在这种情况下做某事而不是失败。

【讨论】:

  • 我不会称其为错误,而只是缺少检查语句。我刚刚检查了 git repo 中 Hough 行的 cpp 示例等效项,并且进行了检查。那里(在 C++ 中)在堆栈上初始化行向量并传递给霍夫线函数。之后检查它是否包含任何内容。我会提交一个问题和一个补丁。
  • 一个小问题:if result is not None:if result != None: 更 Pythonic。
【解决方案2】:

我也在处理这个错误。当函数cv2.HoughCircles 没有检测到任何圆圈时,它将返回None。所以我有两种方法来解决它:

  • 使用 try/exception 包围您的代码块
 try:
    ...
 except Exception as e:
    print 'There is no circles to be detected!'
  • 使用if/else避免操作None对象
 if circles is not None:
   ...
 else:
   print 'There is no circles to be detected!'

【讨论】:

    【解决方案3】:

    这是因为lines = cv2.HoughLines(edges, 2, np.pi/90, 40)[0] 中的阈值,您传递的最后一个参数。它表示被视为一条线的最小长度,在您的情况下为 40 像素。尝试减少它。

    【讨论】:

      【解决方案4】:

      我用这段代码来克服这个问题:

          lines = []
          linesDetected = False
          while not linesDetected:
              lines = cv.HoughLinesP(edges, 1, np.pi/180, 30, minLineLength = 60, maxLineGap=300)
              try:
                  if len(lines) !=0:
                      linesDetected = True
              except:
                  linesDetected = False
      

      【讨论】:

        【解决方案5】:

        更改阈值将起作用

        canny=cv2.Canny(gray,50,150,apertureSize=3)

        lines=cv2.HoughLines(canny,1,numpy.pi/180,40)

        打印(行)

        【讨论】:

          猜你喜欢
          • 2018-04-13
          • 1970-01-01
          • 2017-01-08
          • 2017-08-17
          • 2012-12-04
          • 2014-07-03
          • 1970-01-01
          • 2014-08-14
          相关资源
          最近更新 更多