問題描述
我一直在閱讀有關該主題的文章,但無法以簡單的英語"了解 HoughCircles
的用法和參數(特別是 CV_HOUGH_GRADIENT
之后的那些).
I've been reading about the subject but cannot get the idea in "plain English" about the usage and parameters for HoughCircles
(specially the ones after CV_HOUGH_GRADIENT
).
什么是累加器閾值?100票"是正確的值嗎?
What's an accumulator threshold? Are 100 "votes" a right value?
我可以找到并掩蓋"瞳孔,并通過 Canny
函數工作,但我正在努力超越,我的問題是 HoughCircles
函數.似乎無法找到 Iris 的圈子,我不知道為什么.
I could find and "mask" the pupil, and worked my way through the Canny
function, but I'm struggling beyond that and my problem is the HoughCircles
function. There seems to be failing at finding the Iris' circle and I don't know why.
這是我正在處理的功能:
And this is the function I'm working on:
def getRadius(area):
r = 1.0
r = math.sqrt(area/3.14)
return (r)
def getIris(frame):
grayImg = cv.CreateImage(cv.GetSize(frame), 8, 1)
cv.CvtColor(frame,grayImg,cv.CV_BGR2GRAY)
cv.Smooth(grayImg,grayImg,cv.CV_GAUSSIAN,9,9)
cv.Canny(grayImg, grayImg, 32, 2)
storage = cv.CreateMat(grayImg.width, 1, cv.CV_32FC3)
minRad = int(getRadius(pupilArea))
circles = cv.HoughCircles(grayImg, storage, cv.CV_HOUGH_GRADIENT, 2, 10,32,200,minRad, minRad*2)
cv.ShowImage("output", grayImg)
while circles:
cv.DrawContours(frame, circles, (0,0,0), (0,0,0), 2)
# this message is never shown, therefore I'm not detecting circles
print "circle!"
circles = circles.h_next()
return (frame)
推薦答案
HoughCircles
可能有點棘手,我建議查看 這個線程.包括我在內的一群人;),討論如何使用它.關鍵參數是param2
,即所謂的accumulator threshold
.基本上,它越高,你得到的圈子就越少.而且這些圓圈有更高的正確概率.每個圖像的最佳價值都不同.我認為最好的方法是在 param2
上使用參數搜索.IE.繼續嘗試值,直到滿足您的條件(例如:有 2 個圓圈,或不重疊的最大圓圈數等).我有一些對param2"進行二進制搜索的代碼,所以它很快就滿足了條件.
HoughCircles
can be kind of tricky, I suggest looking through this thread. Where a bunch of people, including me ;), discuss how to use it. The key parameter is param2
, the so-called accumulator threshold
. Basically, the higher it is the less circles you get. And these circles have a higher probability of being correct. The best value is different for every image. I think the best approach is to use a parameter search on param2
. Ie. keep on trying values until your criteria is met (such as: there are 2 circles, or max. number of circles that are non-overlapping, etc.). I have some code that does a binary search on 'param2', so it meet the criteria quickly.
另一個關鍵因素是預處理,盡量減少噪點并簡化圖像.模糊/閾值/canny的某種組合對此有好處.
The other crucial factor is pre-processing, try to reduce noise, and simplify the image. Some combination of blurring/thresholding/canny is good for this.
無論如何,我明白了:
從您上傳的圖片中,使用以下代碼:
From your uploded image, using this code:
import cv
import numpy as np
def draw_circles(storage, output):
circles = np.asarray(storage)
for circle in circles:
Radius, x, y = int(circle[0][3]), int(circle[0][0]), int(circle[0][4])
cv.Circle(output, (x, y), 1, cv.CV_RGB(0, 255, 0), -1, 8, 0)
cv.Circle(output, (x, y), Radius, cv.CV_RGB(255, 0, 0), 3, 8, 0)
orig = cv.LoadImage('eyez.png')
processed = cv.LoadImage('eyez.png',cv.CV_LOAD_IMAGE_GRAYSCALE)
storage = cv.CreateMat(orig.width, 1, cv.CV_32FC3)
#use canny, as HoughCircles seems to prefer ring like circles to filled ones.
cv.Canny(processed, processed, 5, 70, 3)
#smooth to reduce noise a bit more
cv.Smooth(processed, processed, cv.CV_GAUSSIAN, 7, 7)
cv.HoughCircles(processed, storage, cv.CV_HOUGH_GRADIENT, 2, 32.0, 30, 550)
draw_circles(storage, orig)
cv.ShowImage("original with circles", orig)
cv.WaitKey(0)
更新
我意識到我有點看錯了你的問題!您實際上想要找到 iris 邊緣.他們沒有像學生那樣明確定義.所以我們需要盡可能地幫助HoughCircles
.我們可以這樣做:
I realise I somewhat miss-read your question! You actually want to find the iris edges. They are not so clearly defined, as the pupils. So we need to help HoughCircles
as much as possible. We can do this, by:
- 指定虹膜的大小范圍(我們可以根據瞳孔大小計算出一個合理的范圍).
- 增加圓心之間的最小距離(我們知道兩個虹膜永遠不會重疊,因此我們可以安全地將其設置為我們的最小虹膜尺寸)
然后我們需要再次對 param2
進行參數搜索.將上述代碼中的HoughCircles"行替換為:
And then we need to do a param search on param2
again. Replacing the 'HoughCircles' line in the above code with this:
cv.HoughCircles(processed, storage, cv.CV_HOUGH_GRADIENT, 2, 100.0, 30, 150,100,140)
告訴我們這個:
這還不錯.
這篇關于OpenCV 中用于虹膜檢測的 HoughCircles 的正確用法/參數值是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!