如何在 Python 中访问 OpenCV HoughCircles 结果?
我正在开发一种用于虹膜相关生物识别技术的概念验证算法。我希望能够在一系列图像上测试它,但为了做到这一点,我需要知道虹膜边界。按照此处使用的技术,我对图像进行了过滤和智能阈值处理(Otsu 的方法),这使得我只是瞳孔的黑眼圈。我尝试使用 OpenCV 的 HoughCircles 方法,但非 C(++) 文档很少。这是迄今为止我在本节中的代码:
# Convert PIL to openCV type
cvImage = cv.CreateImageHeader(inputImage.size, cv.IPL_DEPTH_8U, 1)
cv.SetData(cvImage, inputImage.tostring())
self.cvSize = cv.GetSize(cvImage)
# Create storage for circles (there should only be one)
storage = cv.CreateMat(50, 1, cv.CV_32FC3)
# Get circles (why doesn't this work?)
circles = cv.HoughCircles(cvImage,storage,cv.CV_HOUGH_GRADIENT,2,(self.cvSize[0])/4,200,100);
最后一行是有问题的行。我关注了互联网上流传的几篇帖子(其中大部分是针对 C/C++ 或 5 岁以上的帖子),并设法想出了这句话。它不会返回任何错误。我如何访问结果?我是否访问圈子
或存储
。我如何访问它们?我已经尝试过
顺便说一句,我如何优化参数以始终在合理的时间内返回学生的拟合圆?
I'm developing a proof of concept algorithm for iris-related biometrics. I'd like to be able to test it on a series of images, but in order to do so, I need to know the iris boundaries. Following the techniques used here, I've filtered and intelligently thresholded the image (Otsu's method), which leaves me just the dark circle of the pupil. I've attempted to use OpenCV's HoughCircles
method, but the non-C(++) documentation is sparse. Here is my code so far for this section.:
# Convert PIL to openCV type
cvImage = cv.CreateImageHeader(inputImage.size, cv.IPL_DEPTH_8U, 1)
cv.SetData(cvImage, inputImage.tostring())
self.cvSize = cv.GetSize(cvImage)
# Create storage for circles (there should only be one)
storage = cv.CreateMat(50, 1, cv.CV_32FC3)
# Get circles (why doesn't this work?)
circles = cv.HoughCircles(cvImage,storage,cv.CV_HOUGH_GRADIENT,2,(self.cvSize[0])/4,200,100);
The final line is the line in question. I followed several posts spread across the internet (most of which are for C/C++ or 5+ years old) and managed to come up with that line. It doesn't return any errors. How do I access the results? Do I access circles
or storage
. How do I access them? I've tried suggestions from this question, but, as the asker said, the cvMat
type isn't iterable, so it won't work. It seems like it would be less work to write my own Circular Hough Transform rather than deal with the sparse documentation of this library, but I think its something simple that I'm missing.
On a side note, how might I optimize the parameters to always return a fitting circle for a pupil in a reasonable amount of time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
cv.HoughLines2
的示例完全不同,因为它的内存存储是这样的:这不适用于
HoughCircles
。HoughCircles
只能处理cvMat
存储,例如:重要的是,它只有 1 行,并且表示应该是具有 3 个通道的 32 位浮点。
例如,您有以下代码行:
由于
cvMat
函数的返回是NULL
,这意味着在 Python 中它是 None (这在 C 中是类似的,有当存储为cvMat
时,没有返回)。这意味着,唯一的输出是存储。
您可以使用 numpy 解码该结构:
这是我用中心点和半径绘制圆的代码,这是
HoughCircles
的结果。只有一个问题我还无法解决:
中心点和半径的X、Y坐标完全不准确,我不知道它们出了什么问题,也许你可以找出来。
The example of
cv.HoughLines2
is totally different, because its memory storage is something like that:That won't work with
HoughCircles
.HoughCircles
can handle onlycvMat
storages, for example:It is important, that it has only 1 row, and the represenation should be 32bit floating point with 3 channels.
For example, you have the following line of code:
Because of
cvMat
the return of the function isNULL
, that means its None in Python (this is similar in C, there is no return, when storage iscvMat
).That means, the only output is the storage.
You can decode the structure with the numpy:
Here is my code to draw circles with the centerpoint and radius, which is the result of
HoughCircles
.There is only one problem I couldn't solve yet:
The X,Y coordinates of centerpoint and the radius is totally inaccurate, I don't know whats wrong with them, maybe you can find out.
一些解决方案:
和:
Some solution:
and:
您使用的是 OpenCV 2.3.x 吗?它的文档似乎建议您执行以下操作:
注意: cv.HoughCircles 可能不会返回任何圆圈(由于限制性参数设置),因此您可以检查以确保圆圈不为空。
这里是一个使用 cv 的类似 python 示例。 HoughLines...
希望有帮助!
Are you using OpenCV 2.3.x? It's documentation seems to suggest you would do something as follows:
NOTE: cv.HoughCircles may not be returning any circles (due to restrictive parameter settings), so you might check to make sure circles is not empty.
Here is a similar python example using cv.HoughLines...
Hope that helps!