5

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.

Community
  • 1
  • 1
Dan
  • 797
  • 1
  • 7
  • 14

3 Answers3

1

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.

ptomato
  • 56,175
  • 13
  • 112
  • 165
0

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

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • I checked all the arguments; they seem to be right. I'm checking with the C code at http://www.seas.upenn.edu/~bensapp/opencvdocs/ref/opencvref_cv.htm#cv_imgproc – Dan May 29 '10 at 01:21
0

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)
JMax
  • 26,109
  • 12
  • 69
  • 88
  • 1
    No, `storage` is actually where the list of circles is stored. So `cv.CreateMat(1, max_circles, cv.CV_32FC3)` is correct. – ptomato Mar 30 '11 at 14:38