2017-09-05 8 views
1

私は瞳孔を検出できるように、目の画像に一連のスレッシュホールドを適用するこのPythonコードを持っています。私はpython 2.7をWindows 10で使ってこのコードを書いた。私が望む出力を得ることができたので、実際にはうまくいった。ここpython2.7で作業中のOpencvスレッショルドコード(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:

私は私のラズベリーパイでこの同じコードを実行しようとしました、私はちょうど入力画像のファイルパスと出力画像を格納する場所を変更しました。ここで

は、私は私のラズベリーパイに走ったコードです:

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() 

私は次のエラーを取得しかし:

トレースバック(最新の呼び出しの最後): ファイル「/ホーム/パイを(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

これは私が上に投稿したのと同じエラーです。この問題は、逆バイナリしきい値を適用する行を常に指摘しています。はい。私がコードを実行することができたのは1回あり、最初の4つの出力イメージが生成されましたが、循環ハフ変換で出力は生成されませんでした。しかし、私は一度しかできませんでした。私は再びそれを複製することができませんでした。 – 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でそれを保存しますイメージ(double)ですが、しきい値はのみはCV_8UまたはCV_32Fです。ここでは2つのオプションがあります.1つは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を使用して出力イメージを書き込んで読み込まずにコードを編集することもできました。このコードは私のラズベリーパイで正しく実行されました(しかし、私は現在循環ハフ変換部分を適用することに問題がありますが、属性 'HOUGH_GRADIENT'を持たないオブジェクトについてはエラーが発生します)。しかし、CHT部分、私の元の出力に比べて少し異なっていますが、しきい値処理は正しく機能します。 – Swagayema

関連する問題