私は瞳孔を検出できるように、目の画像に一連のスレッシュホールドを適用するこの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()
は、(元の入力画像を含む)コードのすべての出力のスクリーンショットである:
私は私のラズベリーパイでこの同じコードを実行しようとしました、私はちょうど入力画像のファイルパスと出力画像を格納する場所を変更しました。ここで
は、私は私のラズベリーパイに走ったコードです:
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と画像処理を一般的に使用するのはまだ新しいです。私はすでにそれをよく研究していますが、本当によく理解できません。
あなたが私を助けて正しい方向に向けることができれば、本当に感謝しています。また、あなたがエラーを引き起こすことなく、新たにしきい値の設定されたイメージを保存して再度ロードするのを避ける方法を提案することができれば(本当に非効率な方法です)、本当に感謝しています。
イメージを書き込まないでもう一度読み込んだときに発生するエラーを投稿できますか?また、最初のバイナリイメージがpiに正しく書き込まれていることを確認しましたか? –
これは私が上に投稿したのと同じエラーです。この問題は、逆バイナリしきい値を適用する行を常に指摘しています。はい。私がコードを実行することができたのは1回あり、最初の4つの出力イメージが生成されましたが、循環ハフ変換で出力は生成されませんでした。しかし、私は一度しかできませんでした。私は再びそれを複製することができませんでした。 – Swagayema