OpenCV Python HoughCircles 错误

发布于 2024-09-03 18:24:21 字数 1090 浏览 12 评论 0原文

我正在开发一个检测图像中圆形形状的程序。我认为霍夫变换是最好的,并且我在 OpenCV 库中找到了一个。问题是,当我尝试使用它时,我收到一个错误,我不知道如何修复。 OpenCV for Python 还没有完全实现吗?程序运行所需的库是否有修复?

这是代码:

import cv

#cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)

while True:
    img = cv.QueryFrame(capture)
    gray = cv.CreateImage(cv.GetSize(img), 8, 1)
    edges = cv.CreateImage(cv.GetSize(img), 8, 1)

    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
    cv.Canny(gray, edges, 50, 200, 3)
    cv.Smooth(gray, gray, cv.CV_GAUSSIAN, 9, 9)

    storage = cv.CreateMat(1, 2, cv.CV_32FC3)

    #This is the line that throws the error
    cv.HoughCircles(edges, storage, cv.CV_HOUGH_GRADIENT, 2, gray.height/4, 200, 100)

    #cv.ShowImage("camera", img)
    if cv.WaitKey(10) == 27:
         break

这是我收到的错误:

OpenCV 错误:未知函数中的 Null pinter (), 文件..\..\..\..\ocv\openc\src\cxcore\cxdatastructs.cpp,第 408 行 回溯(最近一次调用最后一次): 文件“ellipse-detect-webcam.py”,第 20 行,位于 cv.HoughCircles(边缘, 存储, cv.CV_HOUGH_GRADIENT, 2, grey.height/4, 200, 100) 简历错误

预先感谢您的帮助。

I'm working on a program that detects circular shapes in images. I decided a Hough Transform would be the best, and I found one in the OpenCV library. The problem is that when I try to use it I get an error that I have no idea how to fix. Is OpenCV for Python not fully implemented? Is there a fix to the library I need for the program to work?

Here's the code:

import cv

#cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)

while True:
    img = cv.QueryFrame(capture)
    gray = cv.CreateImage(cv.GetSize(img), 8, 1)
    edges = cv.CreateImage(cv.GetSize(img), 8, 1)

    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
    cv.Canny(gray, edges, 50, 200, 3)
    cv.Smooth(gray, gray, cv.CV_GAUSSIAN, 9, 9)

    storage = cv.CreateMat(1, 2, cv.CV_32FC3)

    #This is the line that throws the error
    cv.HoughCircles(edges, storage, cv.CV_HOUGH_GRADIENT, 2, gray.height/4, 200, 100)

    #cv.ShowImage("camera", img)
    if cv.WaitKey(10) == 27:
         break

And here is the error I'm getting:

OpenCV Error: Null pinter () in unknown function,
file ..\..\..\..\ocv\openc\src\cxcore\cxdatastructs.cpp, line 408
Traceback (most recent call last):
File "ellipse-detect-webcam.py", line 20, in
cv.HoughCircles(edges, storage, cv.CV_HOUGH_GRADIENT, 2, gray.height/4, 200, 100)
cv.error

Thanks in advance for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

别想她 2024-09-10 18:24:22

存储必须更大,我认为 cvMat 不是动态分配的,因此您必须将行:更改

storage = cv.CreateMat(1, 2, cv.CV_32FC3)

为:

storage = cv.CreateMat(1, img.rows * img.cols, cv.CV_32FC3)

The storage must to be bigger, I thought that cvMat isn't dinamically allocated so you have to for example change the line:

storage = cv.CreateMat(1, 2, cv.CV_32FC3)

to:

storage = cv.CreateMat(1, img.rows * img.cols, cv.CV_32FC3)
末骤雨初歇 2024-09-10 18:24:21

无论如何,我发现如果 cv.HoughCircles 无法检测到图像中的圆形形状,它就会中止,而不是优雅地返回一个空列表。

For what it's worth, I've found that cv.HoughCircles aborts if it can't detect a circular shape in the image, instead of gracefully returning an empty list.

木槿暧夏七纪年 2024-09-10 18:24:21

图片有效吗?
你能显示它们(原始的和灰度的)吗?

否则你确定函数的参数是正确的吗?您是否正确传递指针或引用

Are the images valid?
Can you display them (the original and the grayscaled)

Otherwise are you sure the args to the function are correct? Are you passing pointers or references correctly

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