-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_threshold.py
More file actions
97 lines (75 loc) · 4.23 KB
/
interactive_threshold.py
File metadata and controls
97 lines (75 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import cv2
import numpy as np
# Callback function for trackbars (it does nothing but is required by createTrackbar)
def nothing(x):
pass
# Function to display the filtered image based on current parameters
def interactive_circle_detection(image_path):
# Load the image
image = cv2.imread(image_path)
if image is None:
print(f"Error: Unable to load image {image_path}")
return
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Create a window to display the image
cv2.namedWindow('Interactive Circle Detection')
# Set a fixed resolution for the window
fixed_width = 2560 # Desired width
fixed_height = 1440 # Desired height
# Create trackbars for all adjustable parameters
cv2.createTrackbar('Threshold', 'Interactive Circle Detection', 180, 255, nothing) # Binary Threshold
cv2.createTrackbar('Gaussian Blur', 'Interactive Circle Detection', 9, 50, nothing) # Gaussian blur kernel size
cv2.createTrackbar('Canny Min', 'Interactive Circle Detection', 50, 255, nothing) # Canny min threshold
cv2.createTrackbar('Canny Max', 'Interactive Circle Detection', 150, 255, nothing) # Canny max threshold
cv2.createTrackbar('Min Circularity', 'Interactive Circle Detection', 70, 100, nothing) # Min Circularity (0.70 to 1.30)
cv2.createTrackbar('Max Circularity', 'Interactive Circle Detection', 130, 150, nothing) # Max Circularity
# Loop to update the image based on the trackbar positions
while True:
# Get the current positions of the trackbars
threshold_value = cv2.getTrackbarPos('Threshold', 'Interactive Circle Detection')
blur_value = cv2.getTrackbarPos('Gaussian Blur', 'Interactive Circle Detection')
canny_min = cv2.getTrackbarPos('Canny Min', 'Interactive Circle Detection')
canny_max = cv2.getTrackbarPos('Canny Max', 'Interactive Circle Detection')
min_circularity = cv2.getTrackbarPos('Min Circularity', 'Interactive Circle Detection') / 100.0
max_circularity = cv2.getTrackbarPos('Max Circularity', 'Interactive Circle Detection') / 100.0
# Ensure blur_value is odd (Gaussian blur kernel size must be odd)
if blur_value % 2 == 0:
blur_value += 1
# Apply Gaussian blur
blurred = cv2.GaussianBlur(gray, (blur_value, blur_value), 0)
# Apply binary thresholding
_, thresh = cv2.threshold(blurred, threshold_value, 255, cv2.THRESH_BINARY)
# Apply Canny edge detection
edges = cv2.Canny(thresh, canny_min, canny_max)
# Find contours
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Create a copy of the image to draw on
output_image = image.copy()
# Loop over the contours and calculate circularity
for contour in contours:
area = cv2.contourArea(contour)
perimeter = cv2.arcLength(contour, True)
if perimeter == 0:
continue
# Calculate circularity
circularity = 4 * np.pi * (area / (perimeter ** 2))
# Check if the contour is circular within the selected range
if min_circularity < circularity < max_circularity:
(x, y), radius = cv2.minEnclosingCircle(contour)
# Draw the circle on the output image
center = (int(x), int(y))
radius = int(radius)
cv2.circle(output_image, center, radius, (0, 255, 0), 2) # Green circle
cv2.circle(output_image, center, 5, (0, 0, 255), -1) # Red center
# Resize the output image to the fixed resolution
output_image_resized = cv2.resize(output_image, (fixed_width, fixed_height))
# Display the resized image with the detected circle(s)
cv2.imshow('Interactive Circle Detection', output_image_resized)
# Press 'q' to exit the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Close all windows
cv2.destroyAllWindows()
if __name__ == "__main__":
img_path = r'C:\Users\thepr\yolo\datasets\For Test\2024_09_19_06_36_30_166.tiff' # Path to your image
interactive_circle_detection(img_path)