-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint_picker.py
242 lines (210 loc) · 9.94 KB
/
point_picker.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import cv2
from macro import *
class PointsPicker:
def __init__(self):
self.image_to_display = None
self.image_to_display_copy = None
self.window_size = None
self.raw_image = None
self.zoomed_image = None
self.points_to_display = []
self.window = "set anchor"
self.window_position = [0, 0] # pixel coordinate of window w.r.t reshaped image
self.window_position_copy = self.window_position.copy()
# self.window_position_in_screen =
# TODO: 尝试 本地化到方法内部
# pointer position w.r.t window (origin)
self.leftbutton_down_position = [0, 0]
self.leftbutton_pushing_position = [0, 0]
self.zoom_ratio = 1
self.zoom_stride = 0.1
# self.exit_flag = False
def mouse_callback(self, event, x, y, flags, param):
if event == cv2.EVENT_RBUTTONDOWN:
self.leftbutton_down_position = [x, y]
# note that deep copy needed
self.window_position_copy = self.window_position.copy()
self.display()
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_RBUTTON):
pointer_position = [x, y]
self.move(pointer_position)
self.display()
elif event == cv2.EVENT_MOUSEWHEEL:
former_zoom_ratio = self.zoom_ratio
self.update_zoom_ratio(flags)
self.zoom(x, y, former_zoom_ratio)
self.display()
elif event == cv2.EVENT_LBUTTONDOWN:
rightbutton_down_position = [x, y]
corresponding_point = [((x + self.window_position[0]) / self.zoom_ratio),
((y + self.window_position[1]) / self.zoom_ratio)]
quantified_point = [int(corresponding_point[0]),
int(corresponding_point[1])]
self.points_to_display.append(corresponding_point)
param[0].append(quantified_point)
self.display()
pass
else:
cv2.imshow(self.window, self.image_to_display)
pass
# if len(param[0]) == 4:
# self.exit_flag = True
def display(self):
# TODO: 此处为了方便而冗余,可以优化
# self.image_to_display = self.image_to_display_copy.copy()
for point in self.points_to_display:
point_on_display = (int(point[0] * self.zoom_ratio - self.window_position[0]),
int(point[1] * self.zoom_ratio - self.window_position[1]))
# print(point_on_display)
msg = '(x:' + str(int(point[0])) + ',y:' + str(int(point[1])) + ')'
cv2.circle(self.image_to_display,
point_on_display,
1,
(0, 0, 255),
thickness=-1)
cv2.putText(self.image_to_display,
msg,
point_on_display,
cv2.FONT_HERSHEY_PLAIN,
1.0,
(0, 0, 255),
thickness=1)
# self.points_to_display.clear()
cv2.imshow(self.window, self.image_to_display)
# 矫正窗口在图片中的位置
def check_location(self, image_size):
"""for i in range(2):
if image_size < self.window_size:
self.window_position[i] = 0
elif image_size[i] < self.window_position[i] + self.window_size[i]:
self.window_position[i] = image_size[i] - self.window_size[i]
else:
self.window_position[i] = image_size[i] - self.window_size[i]
if self.window_position[i] < 0:
self.window_position[i] = 0"""
for i in range(2):
if self.window_position[i] + self.window_size[i] > image_size[i] > self.window_size[i]:
self.window_position[i] = image_size[i] - self.window_size[i]
elif self.window_position[i] + self.window_size[i] > image_size[i] and image_size[i] < self.window_size[i]:
self.window_position[i] = 0
if self.window_position[i] < 0:
self.window_position[i] = 0
# flag: up/down, stride: , ratio:
def update_zoom_ratio(self, flag):
# up: zoom in
if flag > 0:
temp = self.zoom_ratio + self.zoom_stride
if temp > 4: # 1 + self.zoom_stride * 20: smooth
self.zoom_ratio = 4 # 1 + self.zoom_stride * 20
else:
self.zoom_ratio = temp
# down: zoom out
else:
temp = self.zoom_ratio - self.zoom_stride
if temp < 1:
self.zoom_ratio = 1
else:
self.zoom_ratio = temp
self.zoom_ratio = round(self.zoom_ratio, 2)
def zoom(self, x, y, former_zoom_ratio):
zoomed_image_size = MySize(int(self.raw_image.shape[1] * self.zoom_ratio),
int(self.raw_image.shape[0] * self.zoom_ratio))
self.zoomed_image = cv2.resize(self.raw_image,
(zoomed_image_size.w, zoomed_image_size.h),
interpolation=cv2.INTER_AREA)
new_size = None
if zoomed_image_size <= self.window_size:
new_size = zoomed_image_size.copy()
elif zoomed_image_size > self.window_size:
new_size = self.window_size.copy()
cv2.resizeWindow(self.window, new_size.w, new_size.h)
self.window_position = [int((self.window_position[0] + x) * self.zoom_ratio / former_zoom_ratio - x),
int((self.window_position[1] + y) * self.zoom_ratio / former_zoom_ratio - y)]
self.check_location(zoomed_image_size)
self.image_to_display = self.zoomed_image[self.window_position[1]:self.window_position[1] + new_size.h,
self.window_position[0]:self.window_position[0] + new_size.w]
self.image_to_display_copy = self.image_to_display.copy()
def move(self, pointer_position):
zoomed_h, zoomed_w = self.zoomed_image.shape[0:2]
window_w, window_h = self.window_size.get()
show_size = None
if zoomed_w <= window_w and zoomed_h <= window_h:
show_size = MySize(zoomed_w, zoomed_h)
self.window_position = [0, 0]
elif zoomed_w > window_w and zoomed_h < window_h:
show_size = MySize(window_w, zoomed_h)
self.window_position[0] = self.window_position_copy[0] + self.leftbutton_down_position[0] - \
pointer_position[0]
elif zoomed_w < window_w and zoomed_h > window_h:
show_size = MySize(zoomed_w, window_h)
self.window_position[1] = self.window_position_copy[1] + self.leftbutton_down_position[1] - \
pointer_position[1]
else:
show_size = MySize(window_w, window_h)
self.window_position[0] = self.window_position_copy[0] + self.leftbutton_down_position[0] - \
pointer_position[0]
self.window_position[1] = self.window_position_copy[1] + self.leftbutton_down_position[1] - \
pointer_position[1]
self.check_location(MySize(zoomed_w, zoomed_h))
self.image_to_display = self.zoomed_image[self.window_position[1]:self.window_position[1] + show_size.h,
self.window_position[0]:self.window_position[0] + show_size.w]
self.image_to_display_copy = self.image_to_display.copy()
def init_frame(self, image):
self.raw_image = image
print(self.raw_image.shape)
self.window_size = MySize(self.raw_image.shape[1], self.raw_image.shape[0])
self.zoomed_image = self.raw_image.copy()
self.image_to_display = self.raw_image[self.window_position[1]:self.window_position[1] + self.window_size.h,
self.window_position[0]:self.window_position[0] + self.window_size.w]
self.image_to_display_copy = self.image_to_display.copy()
def caller(self, image, anchor):
self.init_frame(image)
cv2.namedWindow(self.window, cv2.WINDOW_NORMAL)
cv2.resizeWindow(self.window, self.window_size.w, self.window_size.h)
cv2.moveWindow(self.window, 200, 100)
cv2.setMouseCallback(self.window, self.mouse_callback, (anchor, ))
# note that waitKey is needed
while True:
key = cv2.waitKey(400)
print(key)
if key == 100 or key == 68: # d
anchor.pop()
self.points_to_display.pop()
self.image_to_display = self.image_to_display_copy.copy()
self.display()
print('deleted point')
if key == 113 or key == 81: # q
break
cv2.destroyAllWindows()
def resume(self, anchor):
cv2.namedWindow(self.window, cv2.WINDOW_NORMAL)
cv2.resizeWindow(self.window, self.window_size.w, self.window_size.h)
cv2.moveWindow(self.window, 200, 100)
cv2.setMouseCallback(self.window, self.mouse_callback, (anchor,))
# note that waitKey is needed
# cv2.waitKey()
while True:
key = cv2.waitKey(400)
print(key)
if key == 100 or key == 68:
anchor.pop()
self.points_to_display.pop()
self.image_to_display = self.image_to_display_copy.copy()
self.display()
print('deleted point')
if key == 113 or key == 81:
break
cv2.destroyAllWindows()
if __name__ == '__main__':
from anchor import Anchor
_image = cv2.imread('./0.jpg')
_anchor = Anchor()
pp = PointsPicker()
pp.caller(_image, _anchor)
while len(_anchor) < 4:
pp.resume(_anchor)
cv2.waitKey(1000)
print('final')
print(_anchor.vertexes)
# 删除
# 恢复