如何在 Python 中访问 OpenCV HoughCircles 结果?

发布于 2024-12-10 06:13:32 字数 867 浏览 0 评论 0原文

我正在开发一种用于虹膜相关生物识别技术的概念验证算法。我希望能够在一系列图像上测试它,但为了做到这一点,我需要知道虹膜边界。按照此处使用的技术,我对图像进行了过滤和智能阈值处理(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇纵 2024-12-17 06:13:32

cv.HoughLines2 的示例完全不同,因为它的内存存储是这样的:

storage2 = cv.CreateMemStorage(0)

这不适用于 HoughCirclesHoughCircles 只能处理 cvMat 存储,例如:

storage = cv.CreateMat(image.width, 1, cv.CV_32FC3)

重要的是,它只有 1 行,并且表示应该是具有 3 个通道的 32 位浮点。

例如,您有以下代码行:

circles=cv.HoughCircles(image,storage, cv.CV_HOUGH_GRADIENT, 100, 300,100,50)

由于 cvMat 函数的返回是 NULL,这意味着在 Python 中它是 None (这在 C 中是类似的,有当存储为cvMat时,没有返回)。

这意味着,唯一的输出是存储。
您可以使用 numpy 解码该结构:

np.asarray(storage)

这是我用中心点和半径绘制圆的代码,这是 HoughCircles 的结果。

for i in range(0,len(np.asarray(storage))):
        cv.Circle(image, ( int(np.asarray(storage)[i][0][0]), int(np.asarray(storage)[i][0][1]) ), int(np.asarray(storage)[i][0][2]), cv.CV_RGB(255, 0, 0), 2, 8, 0 )

只有一个问题我还无法解决:
中心点和半径的X、Y坐标完全不准确,我不知道它们出了什么问题,也许你可以找出来。

The example of cv.HoughLines2 is totally different, because its memory storage is something like that:

storage2 = cv.CreateMemStorage(0)

That won't work with HoughCircles. HoughCircles can handle only cvMat storages, for example:

storage = cv.CreateMat(image.width, 1, cv.CV_32FC3)

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:

circles=cv.HoughCircles(image,storage, cv.CV_HOUGH_GRADIENT, 100, 300,100,50)

Because of cvMat the return of the function is NULL, that means its None in Python (this is similar in C, there is no return, when storage is cvMat).

That means, the only output is the storage.
You can decode the structure with the numpy:

np.asarray(storage)

Here is my code to draw circles with the centerpoint and radius, which is the result of HoughCircles.

for i in range(0,len(np.asarray(storage))):
        cv.Circle(image, ( int(np.asarray(storage)[i][0][0]), int(np.asarray(storage)[i][0][1]) ), int(np.asarray(storage)[i][0][2]), cv.CV_RGB(255, 0, 0), 2, 8, 0 )

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.

野却迷人 2024-12-17 06:13:32

一些解决方案:

grayImage = cv2.cvtColor(circleImage, cv.CV_BGR2GRAY)
circles = cv2.HoughCircles(grayImage, cv.CV_HOUGH_GRADIENT, 2, 80, None, 100, 100, 50, 150)

和:

all_circles = circles[0]
    for circle in all_circles:
        #circle[0] - x
        #circle[1] - y
        #circle[2] - radius

Some solution:

grayImage = cv2.cvtColor(circleImage, cv.CV_BGR2GRAY)
circles = cv2.HoughCircles(grayImage, cv.CV_HOUGH_GRADIENT, 2, 80, None, 100, 100, 50, 150)

and:

all_circles = circles[0]
    for circle in all_circles:
        #circle[0] - x
        #circle[1] - y
        #circle[2] - radius
星星的軌跡 2024-12-17 06:13:32

您使用的是 OpenCV 2.3.x 吗?它的文档似乎建议您执行以下操作:

circles = cv2.HoughCircles(circleImage, cv.CV_HOUGH_GRADIENT, 2, 32, 200, 100);
for (x, y, radius) in circles:
    # do something with circle

注意: cv.HoughCircles 可能不会返回任何圆圈(由于限制性参数设置),因此您可以检查以确保圆圈不为空。

这里是一个使用 cv 的类似 python 示例。 HoughLines...

希望有帮助!

Are you using OpenCV 2.3.x? It's documentation seems to suggest you would do something as follows:

circles = cv2.HoughCircles(circleImage, cv.CV_HOUGH_GRADIENT, 2, 32, 200, 100);
for (x, y, radius) in circles:
    # do something with circle

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文