forked from TemugeB/python_stereo_camera_calibrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_dots.py
180 lines (138 loc) · 5.48 KB
/
find_dots.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import cv2
import numpy as np
from matplotlib import pyplot as plt
def mirror_dots(image, pattern_size, area):
"""
Finds the dot patterns on the mirror.
input:
- Image with dot patterns (image)
- The size of the pattern (pattern_size)
- The limits of the are of the blob (area)
output:
- Image with recognized patterns (image)
- Centers of the dots (centers)
"""
# get shape of the image
shape = image.shape
# Set filtering parameters
# Initialize parameter setting using cv2.SimpleBlobDetector
params = cv2.SimpleBlobDetector_Params()
params.minArea = area[0]
params.maxArea = area[1]
params.minCircularity = 0.1
params.minThreshold = 0
params.filterByConvexity = True
params.minConvexity = 0.9
params.filterByInertia = False
# create blob detector
detector = cv2.SimpleBlobDetector_create(params)
# find the dots on of the mirror for camera 1 and camera 2
upper_image = image[0:int(shape[0]/2), :]
lower_image = image[int(shape[0]/2):int(shape[0]), :]
# find circle grids in the upper and lower part of the image
upper_ret, upper_centers = cv2.findCirclesGrid(upper_image, pattern_size, flags=(cv2.CALIB_CB_SYMMETRIC_GRID+cv2.CALIB_CB_CLUSTERING), blobDetector=detector)
lower_ret, lower_centers = cv2.findCirclesGrid(lower_image, pattern_size, flags=(cv2.CALIB_CB_SYMMETRIC_GRID+cv2.CALIB_CB_CLUSTERING), blobDetector=detector)
for center in lower_centers[:,0]:
center[1] = center[1]+shape[0]/2
count = 0
centers = np.zeros((pattern_size[0]*pattern_size[1]*2, 2))
for center in upper_centers[:,0]:
centers[count,:] = center
count += 1
for center in lower_centers[:,0]:
centers[count,:] = center
count += 1
# Put number of the pattern on the image
image = cv2.putText(image, str(1), (int(upper_centers[0,0][0]), int(upper_centers[0,0][1])), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 4, (255,0,0), 5,
cv2.LINE_AA)
image = cv2.putText(image, str(2), (int(lower_centers[0,0][0]), int(lower_centers[0,0][1])), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 4, (255,0,0), 5,
cv2.LINE_AA)
# draw chessboard on image
image = cv2.drawChessboardCorners(image, pattern_size, upper_centers, upper_ret)
image = cv2.drawChessboardCorners(image, pattern_size, lower_centers, lower_ret)
return image, centers
def screen_dots(image, pattern_size, area):
"""
Finds the dot pattern on the screen.
input:
- Image with dot patterns (image)
- Pattern size of the dots (pattern_size)
- Limits of the area of the dots (area)
output:
- Image with the recognized dots (image)
- Centers of the dots (centers)
"""
# get shape of the image
shape = image.shape
# Set filtering parameters
# Initialize parameter setting using cv2.SimpleBlobDetector
params = cv2.SimpleBlobDetector_Params()
params.minArea = area[0]
params.maxArea = area[1]
params.minCircularity = 0.1
params.minThreshold = 0
params.filterByConvexity = False
params.filterByInertia = False
detector = cv2.SimpleBlobDetector_create(params)
centers = []
ret, centers = cv2.findCirclesGrid(image, pattern_size, flags=(cv2.CALIB_CB_SYMMETRIC_GRID+cv2.CALIB_CB_CLUSTERING), blobDetector=detector)
image = cv2.drawChessboardCorners(image, pattern_size, centers, ret)
return image, centers
def led_dots(image, area):
"""
Finds the dots of the LED light sources.
input:
- Image with the LED dots (image)
- Limits of the area of the dots (area)
output:
- Image with the recognized dots (image)
- Coodinates of the LED in the images (led_locations)
"""
# Set filtering parameters
# Initialize parameter setting using cv2.SimpleBlobDetector
params = cv2.SimpleBlobDetector_Params()
params.minArea = area[0]
params.maxArea = area[1]
params.minCircularity = 0.9
params.minThreshold = 20
params.maxThreshold = 255
params.thresholdStep = 5
params.filterByConvexity = False
params.filterByInertia = False
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(image)
temp_image = cv2.drawKeypoints(image, keypoints, np.array([]), (255,0,0), flags=cv2.DrawMatchesFlags_DRAW_RICH_KEYPOINTS)
plt.subplot(),plt.imshow(temp_image,'gray')
plt.get_current_fig_manager().window.showMaximized()
plt.xticks([]),plt.yticks([])
plt.title("Select LED dot")
point = plt.ginput(1, show_clicks=True)
plt.show()
dist = [np.linalg.norm(np.array(keypoint.pt)-np.array(point)) for keypoint in keypoints]
led_location = keypoints[np.argmin(dist)].pt
image = cv2.drawMarker(image, (int(led_location[0]), int(led_location[1])), (0,255,0), markerType=cv2.MARKER_TILTED_CROSS, thickness=5)
return image, led_location
def show_images(images):
"""
Plots images.
input:
- Images to plot (images)
"""
# create titles of images and put them in an array
titles = ['Camera 1', 'Camera 2']
for i in range(len(images)):
plt.subplot(1,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
if __name__ == "__main__":
# read the images
img1 = cv2.imread("images/led/camera1/image0.png")
img2 = cv2.imread("images/led/camera2/image0.png")
image1, mirror_dots1 = mirror_dots(img1, (4,2), (150, 1000))
image2, mirror_dots2 = mirror_dots(img2, (4,2), (150, 1800))
# image1, screen_dots1 = screen_dots(img1, (7,5), (1000,10000))
# image2, screen_dots2 = screen_dots(img2, (7,5), (1000,10000))
image1, location1 = led_dots(img1, (100,10000))
image2, location2 = led_dots(img2, (100,10000))
show_images([image1, image2])