#tested ok khwong 25 June 2011
#from http://stackoverflow.com/questions/2933284/opencv-python-houghcircles-error
# still need to make houghcircle work
import cv

#cv.NamedWindow("camera", 1)
cv.NamedWindow("Hough_lines", 1)
cv.NamedWindow("lines", 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)
    color_dst = cv.CreateImage( cv.GetSize(img), 8, 3 )
    storage = cv.CreateMemStorage(0)    

    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
    cv.Canny(gray, edges, 50, 200, 3)
    lines = cv.HoughLines2( edges, storage, cv.CV_HOUGH_PROBABILISTIC, 1, 3.14/180, 50, 50, 10 )
    for line in lines:
        cv.Line(color_dst, line[0], line[1], cv.CV_RGB(255, 0, 0), 3, 8)
    

    cv.ShowImage("Hough_lines", edges)
    cv.ShowImage("lines", color_dst );
    
    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:
        cv.DestroyWindow("Hough_lines")
        break
