0

I am trying to make a program that will accept an image of a pool table and detect balls with OpenCV and Python. My algorithm right now is basically: 1) grayscaling the image; 2) applying median blur; 3) applying Gaussian blur; 4) applying cv2.HoughCircles() to detect circles.

Unfortunately, cv2.HoughCircles() detects random circles in the image:

Pool Table with Poor Circles Detection

When I called cv2.Canny() to see the edges used in cv2.HoughCircles, the results included a lot of noise (due to the color/texture of the carpet):

Canny Edge Detection on Pool Table

How should I remove the false detected circles? Changing the values of "param2" to increase the accumulator threshold will cause the black eight ball to not be detected. I'm thinking of either 1) applying a mask to filter out everything but the pool table (and then determining a ROI from the mask); 2) applying a rectangle detection algorithm to detect the pool table; 3) applying fiducials to try an determine an ROI from the image.

Here is the original image.

The code is below:

import cv2

img = cv2.imread("Assets/Setup.jpg", 1)
grayscale_frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
median_blur_frame = cv2.medianBlur(grayscale_frame, 5)
gaussian_blur_frame = cv2.GaussianBlur(median_blur_frame, (5, 5), 0)
circles = cv2.HoughCircles(gaussian_blur_frame, cv2.HOUGH_GRADIENT, dp=1, minDist=45, param1=75, param2=14,
                           minRadius=40, maxRadius=45)
for circle in circles[0]:
    print(circle)
    cv2.circle(img, (int(circle[0]), int(circle[1])), int(circle[2]), (255, 255, 255), 5)
cv2.imshow("Frame", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

0 Answers0