HoughCircles(image, method, dp, minDist)
HoughCircles(image, method, dp, minDist, param1=?, param2=?, minRadius=?, maxRadius=?)
With the arguments:
image: Input image (grayscale).
method: Define the detection method. Currently "HOUGH_GRADIENT" is the only one available in OpenCV.
dp = 1: The inverse ratio of resolution.
min_dist: Minimum distance between detected centers.
param_1: Upper threshold for the internal Canny edge detector.
param_2: Threshold for center detection.
min_radius: Minimum radius to be detected. If unknown, put zero as default.
max_radius: Maximum radius to be detected. If unknown, put zero as default.
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Loads an image
src = cv2.imread("images/smarties.png")
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
rows = gray.shape[0]
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8, param1=100, param2=30)
if circles is not None:
circles = np.uint16(np.around(circles))
for c in circles[0, :]:
center_x,center_y, radius = c
center = (center_x,center_y)
cv2.circle(src, center, radius, (255, 0, 255), 3) #outline
plt.imshow(src[...,::-1])
<matplotlib.image.AxesImage at 0x19de0b98d30>
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Loads an image
src = cv2.imread("images/old-iranian-coins.jpg")
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
rows = gray.shape[0]
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8,
param1=100, param2=30)
if circles is not None:
print("Number of coins:",len(circles[0,:]))
circles = np.uint16(np.around(circles))
for c in circles[0, :]:
center_x,center_y, radius = c
center = (center_x,center_y)
cv2.circle(src, center, 1, (0, 100, 100), 3) #center
cv2.circle(src, center, radius, (255, 0, 255), 3) #outline
plt.imshow(src[...,::-1]);
Number of coins: 7
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Loads an image
src = cv2.imread("images/iranian-coin.jpg")
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
rows = gray.shape[0]
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8,
param1=100, param2=30,
minRadius=50, maxRadius=100)
if circles is not None:
circles = np.uint16(np.around(circles))
for c in circles[0, :]:
center_x,center_y, radius = c
center = (center_x,center_y)
cv2.circle(src, center, 1, (0, 100, 100), 3) #center
cv2.circle(src, center, radius, (255, 0, 255), 3) #outline
plt.imshow(src[...,::-1])
print("Number of coins:",len(circles[0,:]))
Number of coins: 24