2017-09-05 27 views
1

我有這個python代碼,應用系列閾值的眼睛圖像,以便它將它能夠檢測到瞳孔。我在Windows 10中使用python 2.7編寫了這段代碼。它實際上運行得非常好,因爲我能夠獲得所需的輸出。Opencv閾值代碼工作在python2.7(Windows)但不工作樹莓派

這裏是我在窗10寫的代碼:

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 

img = cv2.imread('C:\Users\User\Documents\module4\input\left.jpg',0) 
image = cv2.medianBlur(img,5) 

#Apply Adaptive Threshold with Laplacian 
th = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C, 
cv2.THRESH_BINARY,11,2) 

laplacian = cv2.Laplacian(th,cv2.CV_64F) 

cv2.imwrite('C:\Users\User\Documents\module4\output\output1.jpg', laplacian) 

#Apply Inverse Binary Threshold 

binthresh = cv2.imread('C:\Users\User\Documents\module4\output\output1.jpg',0) 

ret,thresh2 = cv2.threshold(laplacian,127,255,cv2.THRESH_BINARY_INV) 

cv2.imwrite('C:\Users\User\Documents\module4\output\output2.jpg', thresh2) 

#Apply First Otsu's Threshold 

otsuthresh1 = cv2.imread('C:\Users\User\Documents\module4\output\output2.jpg',0) 

blur = cv2.GaussianBlur(otsuthresh1,(5,5),0) 

ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 

cv2.imwrite('C:\Users\User\Documents\module4\output\output3.jpg', th3) 

#Apply Gaussian Blur 
gaussblur = cv2.imread('C:\Users\User\Documents\module4\output\output3.jpg',0) 

blur2 = cv2.GaussianBlur(gaussblur,(5,5),0) 

cv2.imwrite('C:\Users\User\Documents\module4\output\output4.jpg', blur2) 
#Apply Second Otsu's Threshold 
otsuthresh2 = cv2.imread('C:\Users\User\Documents\module4\output\output4.jpg',0) 

blur3 = cv2.GaussianBlur(otsuthresh2,(5,5),0) 

ret4,th4 = cv2.threshold(blur3,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 


#Apply Circular Hough Transform 
circles = cv2.HoughCircles(th4,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0) 

circles = np.uint16(np.around(circles)) 
for i in circles[0,:]: 
    # draw the outer circle 
    cv2.circle(th4,(i[0],i[1]),i[2],(100,100,0),2) 
    # draw the center of the circle 
    cv2.circle(th4,(i[0],i[1]),2,(0,50,100),3) 

cv2.imshow('combined', th4) 
cv2.imwrite('C:\Users\User\Documents\module4\output\output5.jpg', th4) 

cv2.waitKey(0) 
cv2.destroyAllWindows() 

這裏是代碼的所有輸出的屏幕截圖(包括原始輸入圖像):

Here is a screenshot of the series of outputs of the code:

我試着在我的覆盆子pi中運行這個相同的代碼,我只是改變了輸入圖像的文件路徑以及存儲輸出圖像的位置。

下面是我在樹莓派跑代碼:

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 

img = cv2.imread('/home/pi/IPD/images/image1.jpg',0) 
image = cv2.medianBlur(img,5) 

#Apply Adaptive Threshold with Laplacian 
th = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C, 
cv2.THRESH_BINARY,11,2) 

laplacian = cv2.Laplacian(th,cv2.CV_64F) 


#Apply Inverse Binary Threshold 

binthresh = cv2.imread('/home/pi/IPD/temp/output1.jpg',0) 

ret,thresh2 = cv2.threshold(binthresh,127,255,cv2.THRESH_BINARY_INV) 

cv2.imwrite('/home/pi/IPD/temp/output2.jpg', thresh2) 

#Apply First Otsu's Threshold 

otsuthresh1 = cv2.imread('/home/pi/IPD/temp/output2.jpg',0) 

blur = cv2.GaussianBlur(otsuthresh1,(5,5),0) 

ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 

cv2.imwrite('/home/pi/IPD/temp/output3.jpg', th3) 

#Apply Gaussian Blur 
gaussblur = cv2.imread('/home/pi/IPD/temp/output3.jpg',0) 

blur2 = cv2.GaussianBlur(gaussblur,(5,5),0) 

cv2.imwrite('/home/pi/IPD/temp/output4.jpg', blur2) 
#Apply Second Otsu's Threshold 
otsuthresh2 = cv2.imread('C/home/pi/IPD/temp/output4.jpg',0) 

blur3 = cv2.GaussianBlur(otsuthresh2,(5,5),0) 

ret4,th4 = cv2.threshold(blur3,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 


#Apply Circular Hough Transform 
circles = cv2.HoughCircles(th4,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0) 

circles = np.uint16(np.around(circles)) 
for i in circles[0,:]: 
    # draw the outer circle 
    cv2.circle(th4,(i[0],i[1]),i[2],(100,100,0),2) 
    # draw the center of the circle 
    cv2.circle(th4,(i[0],i[1]),2,(0,50,100),3) 

cv2.imshow('combined', th4) 
cv2.imwrite('/home/pi/IPD/images/final.jpg', th4) 

cv2.waitKey(0) 
cv2.destroyAllWindows() 

不過,我得到以下錯誤:

回溯(最近通話最後一個): 文件「/家/ PI /IPD/mod4.py「,第18行,在 ret,thresh2 = cv2.threshold(binthresh,127,255,cv2.THRESH_BINARY_INV) 錯誤:/build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/core /src/matrix.cpp:269:錯誤:(-215)m.dims> = 2函數Mat

實際上,當我第一次在Windows 10中編寫代碼時,我也遇到了這個錯誤,但是我通過編寫新的閾值圖像並重新加載它來解決它(正如您在我的代碼中看到的那樣。我知道這是一種效率低下的方式),以便我可以對其應用新的閾值。我試圖尋找可能的解釋,爲什麼這可能是,我認爲它與我輸入的信號有多少個通道有關(我認爲)。但是,我仍然對使用opencv和圖像處理有所瞭解,而且我真的不明白這個概念真的很好(即使我已經研究過它)。

如果你們可以幫助我並指出我朝着正確的方向,我會非常感激。而且,如果你們可以建議我如何避免存儲新的閾值圖像並重新加載(這實際上是一種低效率的方法)而不會造成任何錯誤,我真的非常感謝它。

+0

你可以發佈你遇到的錯誤,當你*不*寫入圖像,然後再次讀取它們?另外,你是否證實第一個二進制圖像正在你的pi上正確寫入? –

+0

這是我上面發佈的錯誤。問題總是指出我應用逆二進制閾值的路線。是。有一次,我能夠運行代碼,並且它生成了前4個輸出圖像,但是它沒有通過循環hough變換生成輸出。但我只能做到一次。我無法再複製它。 – Swagayema

回答

1

我發現2個錯誤,我希望那些是唯一的2

1)在你的第一個代碼,您有:

laplacian = cv2.Laplacian(th,cv2.CV_64F) 

cv2.imwrite('C:\Users\User\Documents\module4\output\output1.jpg', laplacian) 

#Apply Inverse Binary Threshold 

binthresh = cv2.imread('C:\Users\User\Documents\module4\output\output1.jpg',0) 

ret,thresh2 = cv2.threshold(laplacian,127,255,cv2.THRESH_BINARY_INV) 

這裏你正在做的拉普拉斯算子並將其保存在CV_64F圖像(雙打),但閾值只有需要CV_8U或CV_32F。這裏有兩個選項,一個是將此64F更改爲32F或使用功能normalize並將其轉換爲8U圖像。喜歡的東西:

cv2.normalize(laplacian, output1, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) 

2)在第二個代碼你缺少:

cv2.imwrite('C:\Users\User\Documents\module4\output\output1.jpg', laplacian) 

那麼,你是不是保存這樣的圖像,因此您不會加載它要麼...沒有圖像,一個錯誤跳出。

一般建議,總是使用imshow來看看發生了什麼,直到什麼點。使用相對路徑來保存和加載臨時映像,這樣您只能更改輸入路徑。

+0

感謝您的回覆。我會嘗試你的建議。 – Swagayema

+0

@Swagayema如果你仍然有錯誤只是張貼,試圖找出別的東西:) – api55

+0

非常感謝。使用cv2.normalize做了訣竅。將64F更改爲32F未正確處理,只顯示空白圖像。我還能夠編輯我的代碼,而無需使用cv2.normalize編寫和加載輸出圖像。代碼在我的Raspberry Pi中也正確運行(儘管我現在在應用循環Hough變換部分時遇到了問題,但它仍然存在關於沒有屬性'HOUGH_GRADIENT'的對象的錯誤)但是當我取出CHT部分,閾值工作正常,儘管與我的原始輸出相比有點不同。 – Swagayema

相關問題