-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibmarker.py
103 lines (72 loc) · 3.17 KB
/
calibmarker.py
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
98
99
100
101
102
103
import cv2
# Define the marker size (in pixels)
marker_size = 1
numbersize = 0.01
# List to store the pixel coordinates and assigned numbers of markers for each image
uv = []
# Variables to track the marker number and mouse callback position
marker_number = 1
current_position = None
# Mouse callback function to handle mouse events
def mouse_callback(event, x, y, flags, param):
global marker_number, current_position, image_uv
if event == cv2.EVENT_LBUTTONDOWN:
# Store the current position when the left mouse button is clicked
current_position = (x, y)
# Draw a rectangle around the marker (in red color)
cv2.rectangle(image_uv, (x - marker_size // 2, y - marker_size // 2),
(x + marker_size // 2, y + marker_size // 2), (0, 0, 255), 2)
# Put the marker number on the marker
cv2.putText(image_uv, str(marker_number), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
numbersize, (0, 0, 255), 2)
# Store the pixel coordinates and assigned number in the list for the current image
uv[-1].append((x, y))
# Increment the marker number
marker_number += 1
# Refresh the display
cv2.imshow("Marker Detection", image_uv)
# Function to process an image and detect markers
def process_image(image_path):
global image_uv, marker_number
# Load the image
image = cv2.imread(image_path)
# Reset the marker number for each new image
marker_number = 1
# Create a copy of the image for drawing markers
image_uv = image.copy()
# Marker detection parameters
marker_threshold = 255 # Adjust this threshold based on marker visibility
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding to isolate the markers
_, thresholded = cv2.threshold(
gray, marker_threshold, 255, cv2.THRESH_BINARY)
# Find contours of the markers
contours, _ = cv2.findContours(
thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Iterate through the detected contours
for contour in contours:
# Calculate the contour area to filter out small detections
area = cv2.contourArea(contour)
if area > 100: # Adjust the minimum area threshold based on marker size
# Get the bounding box of the contour
x, y, w, h = cv2.boundingRect(contour)
# Draw a rectangle around the marker (in green color)
cv2.rectangle(image_uv, (x, y), (x + w, y + h), (0, 0, 255), 2)
# Display the image with marker squares
cv2.namedWindow("Marker Detection", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Marker Detection", 800, 600)
cv2.imshow("Marker Detection", image_uv)
cv2.setMouseCallback("Marker Detection", mouse_callback)
# Wait for a key press to close the image window
cv2.waitKey(0)
def returnUV(image_paths):
# Process each image and detect markers
for image_path in image_paths:
# Create a new list for the current image
uv.append([])
# Process the image and detect markers
process_image(image_path)
# Close all open windows
cv2.destroyAllWindows()
return uv