report-detect/scripts/debug_circle_detect.py

37 lines
1.1 KiB
Python
Raw Permalink Normal View History

2026-02-05 13:57:22 +08:00
import cv2
import numpy as np
import sys
def detect_circle(image_path, output_path):
img = cv2.imread(image_path)
if img is None:
print("Image not found")
sys.exit(1)
gray = cv2.cvtColor(img, 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=int(rows / 4), maxRadius=int(rows / 1.5))
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
center = (i[0], i[1])
# Circle center
cv2.circle(img, center, 1, (0, 100, 100), 3)
# Circle outline
radius = i[2]
cv2.circle(img, center, radius, (255, 0, 255), 3)
print(f"Detected circle: center={center}, radius={radius}")
cv2.imwrite(output_path, img)
print(f"Saved detection to {output_path}")
else:
print("No circles detected.")
if __name__ == "__main__":
detect_circle(sys.argv[1], sys.argv[2])