Python TypeError:'NoneType'对象在图像处理中没有属性'__getitem__'

时间:2015-07-11 17:47:38

标签: python opencv image-processing hough-transform

import cv2
import numpy as np
import matplotlib.pyplot as plt
def CannyThreshold(lowThreshold):
            lowThreshold = cv2.getTrackbarPos('Min threshold','canny demo')
            detected_edges = cv2.medianBlur(gray,5)
            detected_edges=cv2.Canny(detected_edges,lowThreshold,lowThreshold*ratio,apertureSize = kernel_size)
lines = cv2.HoughLines(detected_edges,1,np.pi/180,190)
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))
    #print 'rho=',rho,' theta=',theta
    cv2.line(frame,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imshow('canny demo',detected_edges)
cv2.imshow('original',frame)

lowThreshold = 23
max_lowThreshold = 50
ratio = 3
kernel_size = 3
cap = cv2.VideoCapture(0)
cv2.namedWindow('canny demo')
cv2.createTrackbar('Min threshold','canny demo',lowThreshold,max_lowThreshold, CannyThreshold)

while(1):
  ret, frame = cap.read()
  gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
  CannyThreshold(lowThreshold)  # initialization
if cv2.waitKey(1) == 27:
    cap.release()
    cv2.destroyAllWindows()

上面是我的代码,用于检测边缘,然后在原始帧中在其上绘制线条

我得到的错误如下:

Traceback (most recent call last):
  File "D:\python_program\hough transform\canny_camera.py", line 37, in <module>
    CannyThreshold(lowThreshold)  # initialization
  File "D:\python_program\hough transform\canny_camera.py", line 11, in CannyThreshold
    for rho,theta in lines[0]:
TypeError: 'NoneType' object has no attribute '__getitem__'

1 个答案:

答案 0 :(得分:0)

lines = cv2.HoughLines(detected_edges,1,np.pi/180,190)

这些行可以是None,然后行[0]导致错误