简体   繁体   中英

OpenCV Python HoughCircles error

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.

对于它的价值,我发现如果cv.HoughCircles无法检测到图像中的圆形形状而不是优雅地返回空列表, cv.HoughCircles中止。

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM