我有這個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()
這裏是代碼的所有輸出的屏幕截圖(包括原始輸入圖像):
我試着在我的覆盆子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和圖像處理有所瞭解,而且我真的不明白這個概念真的很好(即使我已經研究過它)。
如果你們可以幫助我並指出我朝着正確的方向,我會非常感激。而且,如果你們可以建議我如何避免存儲新的閾值圖像並重新加載(這實際上是一種低效率的方法)而不會造成任何錯誤,我真的非常感謝它。
你可以發佈你遇到的錯誤,當你*不*寫入圖像,然後再次讀取它們?另外,你是否證實第一個二進制圖像正在你的pi上正確寫入? –
這是我上面發佈的錯誤。問題總是指出我應用逆二進制閾值的路線。是。有一次,我能夠運行代碼,並且它生成了前4個輸出圖像,但是它沒有通過循環hough變換生成輸出。但我只能做到一次。我無法再複製它。 – Swagayema