-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint_from_image.py
167 lines (132 loc) · 4.25 KB
/
point_from_image.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
#cython: language_level=3, boundscheck=False
"""
python37 .\tools\point_from_image.py .\pattern\2021.02.09_referans
Arguments:
1) Path of images to extract points
2) Preprocess Flag 0-1 for Off-On
"""
# importing the module
import sys
# sys.path.append("../tools/")
import libs
from stdo import stdo
from image_manipulation import canny_edge_detection
from image_tools import (
open_image,
show_image,
)
from tools import (
list_files,
save_to_json,
)
class image_point_picker:
last_coordinates = list()
is_threading_active = None
is_verbose = None
def __init__(self, is_verbose=False, is_threading_active=False):
self.is_threading_active = is_threading_active
self.is_verbose = is_verbose
def run(
self,
images_directory_path,
is_preprocess_on=False,
event_click=True,
event_motion=True,
necessary_events=["event_plot_dblclick"],
is_verbose=None,
):
sources_list = list()
if is_verbose is None:
is_verbose = self.is_verbose
if images_directory_path[-1] == "\\" or images_directory_path[-1] == "/":
images_directory_path = images_directory_path[:-1]
for images_path in list_files(images_directory_path):
current_image = open_image(images_path, option="cv2-rgb", is_numpy=True)
if is_preprocess_on:
current_image = image_point_picker.preprocess_sequence(current_image)
sources_list.append(current_image)
if is_preprocess_on:
cmap = "gray"
else:
cmap = None
if len(sources_list) > 1:
open_order = 2
else:
open_order = 1
click_coordinates = show_image(
sources_list,
title="Image Point Picker",
option="plot",
cmap=cmap,
open_order=open_order,
window=True,
event_click=event_click,
event_motion=event_motion,
is_verbose=is_verbose,
)
stdo(1, "Data of click_coordinates:\n\t{}".format(click_coordinates))
json_data = dict()
for key, value in click_coordinates.items():
if key in necessary_events:
json_data[key] = value
stdo(1, "Json Data is {}".format(json_data))
save_to_json(json_data, "coordinates.json", dict_format=True)
@staticmethod
def preprocess_sequence(image):
configs = []
# configs = [0, 0]
# cany_edge = canny_edge_detection(image, configs)
configs = []
preprocessed_image = canny_edge_detection(image, configs)
return preprocessed_image
"""
image = cv2.imread(image_one_path, 1)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image2 = cv2.imread(image_two_path, 1)
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
fig = plt.figure()
a1 = fig.add_subplot(1, 2, 1)
a2 = fig.add_subplot(1, 2, 2)
a1.imshow(image)
a2.imshow(image2)
coords = []
coords = fig.canvas.mpl_connect("button_press_event", onclick)
plt.show()
print("coords: ", coords)
def onclick(self, event, is_verbose=True):
if event.dblclick:
ix, iy = event.xdata, event.ydata
if self.is_verbose:
print("Choosen: x = %d, y = %d" % (ix, iy))
self.coords.append((round(ix), round(iy)))
"""
# driver function
if __name__ == "__main__":
images_directory_path = "images/"
is_preprocess_on = 0
if len(sys.argv) == 3:
images_directory_path = sys.argv[1]
is_preprocess_on = int(sys.argv[2])
elif len(sys.argv) == 2:
images_directory_path = sys.argv[1]
"""
# Events List
necessary_events = [
"event_plot_click",
"event_nonplot_click",
"event_plot_dblclick",
"event_nonplot_dblclick",
"event_plot_motion",
"event_nonplot_motion",
]
"""
necessary_events = ["event_plot_dblclick"]
ipp = image_point_picker()
ipp.run(
images_directory_path,
is_preprocess_on=is_preprocess_on,
event_click=True,
event_motion=False,
necessary_events=necessary_events,
is_verbose=True,
)