saviopalmieri / ctypes-opencv

Automatically exported from code.google.com/p/ctypes-opencv
0 stars 0 forks source link

CvMat __iter__ error/correct return procedure for cvHoughCircle? #36

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I'm attempting to get the coordinates of matched circles, but I can't get
CvMat to iterate, and CvPoint returns huge x and y values:

from opencv import *

C = {} # dict of open camera captures

# returns an image from cam
def get_frame(cam):
    if not cam in C.keys():
        capture = cvCreateCameraCapture(cam)
        if capture:
            C.update({cam: capture})
        else:
            print "Capture failed on camera", cam
            return None
    return cvQueryFrame(C[cam])

def match_circles(img):
    grayscale = cvCreateImage(cvGetSize(img), 8, 1)
    storage = cvCreateMemStorage(0)

    cvConvertImage(img, grayscale)
    cvSmooth(grayscale, grayscale, CV_GAUSSIAN, 9, 9)
    circles = cvHoughCircles(grayscale, storage, CV_HOUGH_GRADIENT, 2,
grayscale.height, 200, 100)
    return circles

if __name__ == '__main__':
    c = 0
    cvNamedWindow("Raw Input", 1)
    while c != 27:
        image = get_frame(0)
        circles = match_circles(image)
        for l in circles.asarray(CvMat):
            for p in l:
                print p

        c = cvWaitKey(10)
        cvShowImage("Raw Input", image)
    cvDestroyAllWindows()

-----
Traceback (most recent call last):
  File "/home/liveuser/Desktop/roboticsai/vision/__init__.py", line 45, in
<module>
    for p in l:
  File "/usr/lib/python2.5/opencv/cxcore.py", line 827, in __iter__
    yield cvGetRows(self, i, i+1)
TypeError: cvGetRows() takes at least 4 arguments (3 given)
-----

cvHoughCircle is supposed to return either a CvSeq of circles, or a CvMat
of circle locations. Replacing circles.asarray(CvMat) with
circles.asarray(CvPoint) gives outputs like:

CvPoint(x=1135017984,y=1133838336)

Does anyone know how to get position data out of the returned value from
cvHoughCircle?

Original issue reported on code.google.com by ian.wetherbee on 9 May 2009 at 5:22

GoogleCodeExporter commented 8 years ago
Actually, based on the C example in the OpenCV's reference, cvHoughCircle 
returns a
sequence of pointers. Each pointer points to an array of 3 float32 numbers: the 
first
two are the coordinates of the circle's center, and the third one is the 
circle's
radius. I tried to use circles.asarrayptr(ctypes.c_float_p) to convert the 
resulting
CvSeq into a python list, and it worked.

Please verify on your end and let me know if you have resolved the problem. 
Thanks.

Original comment by pmtri80@gmail.com on 10 May 2009 at 3:22

GoogleCodeExporter commented 8 years ago
Ah I see now, your solution works perfectly. Thanks!

Original comment by ian.wetherbee on 10 May 2009 at 12:46

GoogleCodeExporter commented 8 years ago
No problem. I'm closing the issue.

Original comment by pmtri80@gmail.com on 10 May 2009 at 2:17