2014-07-22 135 views
0

我正在嘗試使用opencv python編寫代碼,該代碼會自動獲取canny閾值而不是每次手動執行它們。在Canny邊緣檢測中出現錯誤

img= cv2.imread('micro.png',0) 
output = np.zeros(img.shape, img.dtype) 
# Otsu's thresholding 
ret2,highthresh = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 
lowthresh=0.1*highthres 
edges = cv2.Canny(img,output,lowthresh,highthresh) 
cv2.imshow('canny',edges) 

我正在此錯誤 「文件 「test2.py」,第14行,在 邊緣= cv2.Canny(IMG,輸出,lowthresh,highthresh) 類型錯誤:只有長度爲1的陣列可以是轉換到Python標量」

誰能幫助我弄清楚這個error.thankx提前

+0

我認爲它有PNG文件的問題,我也得到同樣的錯誤。嘗試將圖像轉換爲JPG – Akshay

回答

2

好像cv2.threshold返回檢測到的邊緣,並Canny其應用到圖像。下面的代碼爲我工作,給了我一些很好的檢測到我的圖像邊緣。

import cv2 

cv2.namedWindow('canny demo') 

img= cv2.imread('micro.png',0) 
ret2,detected_edges = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 
edges = cv2.Canny(detected_edges,0.1,1.0) 
dst = cv2.bitwise_and(img,img,mask = edges) 
cv2.imshow('canny',dst) 

if cv2.waitKey(0) == 27: 
    cv2.destroyAllWindows() 
+0

這不是給我的要求output.and我必須工作在一個以上的圖片,這就是爲什麼我不能設置閾值每次爲我從這裏得到的概念。 http://stackoverflow.com/questions/4292249/automatic-calculation-of-low-and-high-thresholds-for-the-canny-operation-in-open。但它不適合我 – user3778289

1

您正在運行:

cv2.Canny(img,output,lowthresh,highthresh) 

它正在尋找

cv2.Canny(img,lowthresh,highthresh,output) 

我覺得在一些版本改變了排序,因爲我已經看到這兩個引用。