Search code examples
pythonopencvnumpychannelhough-transform

OpenCV Hough Circle Transform needs 8-bit image


I am working with Hough Circle Transform with my RaspberryPi and when I take a ROI to check for circle like this:

for (x,y,w,h) in trafficLights:
    cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)
    roi = image[y:y+h,x:x+w]
    roi = cv2.medianBlur(roi,5)

    circles = cv2.HoughCircles(roi,cv2.HOUGH_GRADIENT,1,20,
                       param1=50,param2=60,minRadius=0,maxRadius=0)
    circles = numpy.uint16(numpy.around(circles))

    for i in circles[0,:]:
        if i[2] < 100:
            cv2.circle(image,(i[0],i[1]),i[2],(0,255,0),2)
            cv2.circle(image,(i[0],i[1]),2,(0,0,255),3)
            if i[1] > 315:
                print "Green Light"
            else:
                print "Red Light"

I get this error

The source image must be 8-bit, single-channel in function cvHoughCircles

enter image description here How can I transform the ROI to become an 8-bit image or does the error mean something else

Thank you in Advance!

Edit:

enter image description here


Solution

  • Thank you Miki and bpachev for the help!

    The first error means that you need to convert it to grayscale like this

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
    

    And the NoneType error means that no circles were found so to advoid the error you can add this if statement

    if circles is not None:
        circles = numpy.round(circles[0, :]).astype("int")
    

    Then since no circles were found where I knew there were circles I had to play around with the settings of the detector.