-
Notifications
You must be signed in to change notification settings - Fork 1
/
image-clipper.py
217 lines (177 loc) · 8.04 KB
/
image-clipper.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
import os
import sys
import numpy as np
import cv2
try:
__import__("imp").find_module("dicompylercore")
from dicompylercore import dicomparser
dicom_support = True
except ImportError:
dicom_support = False
print("\"dicompylercore\" library was not found. \"image-clipper\" works without dicom support.")
# noinspection PyUnresolvedReferences
def on_mouse(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN and not on_mouse.start:
on_mouse.first_point = (x, y)
on_mouse.start = True
elif event == cv2.EVENT_MOUSEMOVE and on_mouse.start:
on_mouse.move = True
on_mouse.second_point = (x, y)
canvas = on_mouse.image.copy()
cv2.rectangle(canvas, on_mouse.first_point, on_mouse.second_point, (255, 255, 255), 2)
cv2.imshow("image-clipper", canvas)
elif event == cv2.EVENT_LBUTTONUP and on_mouse.start:
if on_mouse.move:
on_mouse.move = False
x = on_mouse.first_point[0]
y = on_mouse.first_point[1]
width = on_mouse.second_point[0]
height = on_mouse.second_point[1]
if width < x:
x, width = width, x
if height < y:
y, height = height, y
scale_factor = on_mouse.original.shape[0] / on_mouse.image.shape[0]
x_scaled = int(x * scale_factor)
y_scaled = int(y * scale_factor)
width_scaled = int(width * scale_factor)
height_scaled = int(height * scale_factor)
rectangle = on_mouse.image[y:height, x:width]
rectangle_original = on_mouse.original[y_scaled:height_scaled, x_scaled:width_scaled]
if on_mouse.final_width > 0 and on_mouse.final_height > 0:
rectangle_original = cv2.resize(rectangle_original, (on_mouse.final_width, on_mouse.final_height))
elif on_mouse.final_width <= 0 < on_mouse.final_height:
rectangle_original = cv2.resize(rectangle_original,
(rectangle_original.shape[1], on_mouse.final_height))
elif on_mouse.final_width > 0 >= on_mouse.final_height:
rectangle_original = cv2.resize(rectangle_original, (on_mouse.final_width, rectangle_original.shape[0]))
cv2.imwrite(on_mouse.path_for_save + os.sep + str(on_mouse.number) + ".png",
rectangle_original)
on_mouse.number += 1
on_mouse.canceled = False
labeled_image = on_mouse.image.copy()
cv2.putText(labeled_image, "Saved. Z - cancel.", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255),
1)
cv2.imshow("image-clipper", labeled_image)
if on_mouse.show_clipped:
cv2.imshow("clipped", rectangle)
on_mouse.start = False
def main(argv):
if len(argv) < 10:
print("Usage: \"path_to_folder\" \"path_for_save\" \"position_to_start\""
"\"save_number\" \"show_clipped\" \"open_grayscale\""
"\"histogram_equalization\" \"final_width\" \"final_height\"")
exit(1)
path_to_folder = argv[1]
if not os.path.exists(path_to_folder):
print("Path does not exist: " + path_to_folder + ". Check path and try again.")
exit(1)
path_for_save = argv[2]
if not os.path.exists(path_for_save):
print("Path does not exist: " + path_for_save + ". Check path and try again.")
exit(1)
position_to_start = int(argv[3])
save_number = int(argv[4])
if argv[5].lower() in ["true", "1"]:
show_clipped = True
else:
show_clipped = False
if argv[6].lower() in ["true", "1"]:
open_mode = 0
else:
open_mode = 1
if argv[7].lower() in ["true", "1"]:
histogram_equalization = True
else:
histogram_equalization = False
final_width = int(argv[8])
final_height = int(argv[9])
walks = list(os.walk(path_to_folder))
i = 0
k = 1
while i < len(walks):
path, directories, files = walks[i]
files = [file for file in files if not file[0] == "."]
j = 0
while j < len(files):
file = files[j]
if k < position_to_start:
k += 1
j += 1
else:
_, extension = os.path.splitext(file)
if extension == ".dcm" and dicom_support:
parsed = dicomparser.DicomParser(path + os.sep + file)
image = np.array(parsed.GetImage(), dtype=np.uint8)
if parsed.GetImageData()["photometricinterpretation"] == "MONOCHROME1":
image = 255 - image
if histogram_equalization:
image = cv2.equalizeHist(image)
image = cv2.medianBlur(image, 3)
elif extension in [".bmp", ".pbm", ".pgm", ".ppm", ".sr", ".ras", ".jpeg", ".jpg", ".jpe", ".png",
".tiff", ".tif"]:
image = cv2.imread(path + os.sep + file, open_mode)
if histogram_equalization:
image = cv2.equalizeHist(image)
else:
j += 1
continue
if image.shape[0] > image.shape[1]:
height = 512
width = int(height / image.shape[0] * image.shape[1])
else:
width = 512
height = int(width / image.shape[1] * image.shape[0])
scaled_image = cv2.resize(image, (width, height))
cv2.imshow("image-clipper", scaled_image)
cv2.setMouseCallback("image-clipper", on_mouse)
on_mouse.image = scaled_image
on_mouse.original = image
on_mouse.path_for_save = path_for_save
on_mouse.show_clipped = show_clipped
on_mouse.final_width = final_width
on_mouse.final_height = final_height
if not hasattr(on_mouse, "number"):
on_mouse.number = save_number
if not hasattr(on_mouse, "canceled"):
on_mouse.canceled = False
on_mouse.start = False
on_mouse.move = False
code = cv2.waitKey(0)
while code not in [2, 3, 27, 32, 127]:
if code in [90, 122]:
if not on_mouse.canceled:
on_mouse.number -= 1
os.remove(path_for_save + os.sep + str(on_mouse.number) + ".png")
labeled_image = on_mouse.image.copy()
cv2.putText(labeled_image, "Cancelled.", (20, 20), cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255), 1)
cv2.imshow("image-clipper", labeled_image)
on_mouse.canceled = True
else:
labeled_image = on_mouse.image.copy()
cv2.putText(labeled_image, "Error. Only one cancellation.", (20, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 1)
cv2.imshow("image-clipper", labeled_image)
code = cv2.waitKey(0)
if code == 27:
print("Next file number: " + str(j + 2) + ".")
print("Next saved number: " + str(on_mouse.number) + ".")
exit(0)
elif code in [3, 32]:
j += 1
elif code == 127:
os.remove(path + os.sep + file)
del files[j]
j += 1
else:
if j > 0:
j -= 1
else:
if i > 0:
i -= 2
i += 1
if __name__ == "__main__":
sys.exit(main(sys.argv))