Giovanni Luro
Giovanni Luro

Reputation: 17

HoughCircles can't detect this circle

I'm using openCV to detect some coins, first I used some functions to fill the coin area so I can make a solid white circle where the coin is, then im trying to use houghCircles to detect the white circle so I can crop it to send to a neural network. But the houghCircle is not detecting anything, any tips on this?

Here is the code:

import numpy as np
import cv2


gray = cv2.imread('coin25a2.jpg',0)

color = cv2.imread('coin25a2.jpg',1)

gray_blur = cv2.GaussianBlur(gray, (15,15), 0)
thresh = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11,1)

kernel = np.ones((3, 3), np.uint8)
closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=7)

circles = cv2.HoughCircles(closing,cv2.HOUGH_GRADIENT,1, 200, 20,30,30, 0)
circles = np.uint16(np.around(circles))

print(circles)
cv2.imshow("a", closing)
cv2.waitKey(0)

The circles variable is not returning any valid (x,y,r).

closing image

Upvotes: 0

Views: 355

Answers (1)

circles = cv2.HoughCircles(closing,cv2.HOUGH_GRADIENT,1, 200, 20,30,30, 0) The last parameter is the maximum radius of the circle that you want to find. I think you need to put a large value there, instead of 0.

A better plan is to go with only the default parameters and adjust later.

cv2.HoughCircles(image, method, dp, minDist)

, which is the same as

cv2.HoughCircles(closing,cv2.HOUGH_GRADIENT,1, 200)

Upvotes: 3

Related Questions