2015-07-11 54 views
0
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() 

上方我的代碼來檢測邊緣,然後在Python的類型錯誤:「NoneType」對象具有在圖像處理中沒有屬性「__getitem__」

我正在錯誤下面給出原始幀畫線在它:

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__' 
+0

東西沒有被正確由時間喲分配我們正在調用'CannyThreshold'函數,它試圖從'None'類型的未賦值變量中獲取屬性'__getitem__'。 – Leb

+0

在這個例子中:https://github.com/Itseez/opencv/blob/master/samples/python2/houghlines.py一個空數組作爲第5個參數傳入。文檔沒有說它是必需的,但是考慮到C++原型,我不會感到驚訝。 –

+0

你應該也可以提到你使用OpenCV的事實。這個事實在你的問題陳述中缺少,你也沒有在你的問題中標記OpenCV(我剛剛做到了這一點)。 'HoughLines'有可能返回'None',這意味着圖像中沒有檢測到行。下面給出的答案基本上描述了這個事實。 – rayryeng

回答

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

此線可以是無,然後行[0]中錯誤結果

+0

是的,我使用try和except解決了問題。行數組返回無類型.. –

相關問題