Trying to detect all the circles with HoughCircles in openCV (python)

  computer-vision, detection, opencv, python

I am following this tutorial: https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/

I was playing around with the parameters of HoughCircles and it seems very innacurate, in my project, the disks you see on the picture will be placed on random spots and i need to be able to detect them and their color.

Currently i am only able to detect few circles, and sometimes some random circles are drawn where there is no circles so i am a bit confused.

Is this the best way to do circle detection with openCV or is there a more accurate way of doing it ?

Initial board : https://imgur.com/BrPB5Ox

Circle drawn : https://imgur.com/dT7k29E

My code :

import cv2
import numpy as np

img = cv2.imread('Photos/board.jpg')
output = img.copy()

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)
# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
    # show the output image
    cv2.imshow("output", np.hstack([img, output]))
    cv2.waitKey(0)

Thanks a lot.

Source: Python Questions

LEAVE A COMMENT