简体   繁体   中英

Python TypeError: 'NoneType' object has no attribute '__getitem__' in image processing

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__'
lines = cv2.HoughLines(detected_edges,1,np.pi/180,190)

此行可以为None,然后lines [0]会导致错误

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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