37 lines
No EOL
1.1 KiB
Python
37 lines
No EOL
1.1 KiB
Python
import cv2
|
|
import numpy as np
|
|
|
|
# Load the image
|
|
image = cv2.imread('dirty.jpeg')
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
# Apply Gaussian blur to reduce noise
|
|
gray = cv2.GaussianBlur(gray, (9, 9), 2)
|
|
|
|
# Detect circles using the Hough Circles method
|
|
circles = cv2.HoughCircles(
|
|
gray,
|
|
cv2.HOUGH_GRADIENT,
|
|
dp=1, # Inverse ratio of accumulator resolution to image resolution
|
|
minDist=20, # Minimum distance between the centers of detected circles
|
|
param1=50, # Higher threshold for edge detection
|
|
param2=30, # Accumulator threshold for circle detection
|
|
minRadius=10, # Minimum radius
|
|
maxRadius=100 # Maximum radius
|
|
)
|
|
|
|
if circles is not None:
|
|
circles = np.uint16(np.around(circles))
|
|
|
|
for circle in circles[0, :]:
|
|
center = (circle[0], circle[1])
|
|
radius = circle[2]
|
|
|
|
# Draw the circle and its center
|
|
cv2.circle(image, center, radius, (0, 255, 0), 2)
|
|
cv2.circle(image, center, 2, (0, 0, 255), 3)
|
|
|
|
# Display the result
|
|
cv2.imshow('Hough Circles', image)
|
|
cv2.waitKey(0)
|
|
cv2.destroyAllWindows() |