51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import cv2
|
|
import numpy as np
|
|
import os
|
|
|
|
img_path = "seal_cropped.png"
|
|
img = cv2.imread(img_path)
|
|
if img is None:
|
|
print(f"Error: {img_path} not found")
|
|
exit(1)
|
|
|
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
gray = cv2.medianBlur(gray, 5)
|
|
|
|
# Try HoughCircles
|
|
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, gray.shape[0] / 8,
|
|
param1=100, param2=30,
|
|
minRadius=int(gray.shape[0] / 4), maxRadius=int(gray.shape[0] / 1.5))
|
|
|
|
if circles is not None:
|
|
circles = np.uint16(np.around(circles))
|
|
for i in circles[0, :]:
|
|
# Draw center
|
|
cv2.circle(img, (i[0], i[1]), 2, (0, 255, 0), 3)
|
|
# Draw circumference
|
|
cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)
|
|
print(f"Found {len(circles[0])} circles. Best: C={circles[0][0][0]},{circles[0][0][1]} R={circles[0][0][2]}")
|
|
else:
|
|
print("No circles detected via HoughCircles")
|
|
|
|
# Fallback: Red Centroid
|
|
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
|
lower_red1 = np.array([0, 50, 50])
|
|
upper_red1 = np.array([10, 255, 255])
|
|
lower_red2 = np.array([160, 50, 50])
|
|
upper_red2 = np.array([180, 255, 255])
|
|
mask1 = cv2.inRange(hsv, lower_red1, upper_red1)
|
|
mask2 = cv2.inRange(hsv, lower_red2, upper_red2)
|
|
mask = cv2.bitwise_or(mask1, mask2)
|
|
|
|
M = cv2.moments(mask)
|
|
if M["m00"] != 0:
|
|
cX = int(M["m10"] / M["m00"])
|
|
cY = int(M["m01"] / M["m00"])
|
|
cv2.circle(img, (cX, cY), 5, (255, 0, 0), -1)
|
|
print(f"Red Centroid: {cX}, {cY}")
|
|
else:
|
|
print("No red detected")
|
|
|
|
cv2.imwrite("debug_circle_detect_result.png", img)
|
|
print("Saved debug_circle_detect_result.png")
|