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

时间:2021-11-22 08:07:57
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()

above is my code to detect edges and then draw lines over it in original frame

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

the error I am getting is given below:

我得到的错误如下:

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 个解决方案

#1


0  

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

This lines can be None, then lines[0] results in error

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

#1


0  

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

This lines can be None, then lines[0] results in error

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