-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrop.py
150 lines (137 loc) · 7.58 KB
/
crop.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
# TODO:Crop big size images
import os
import cv2
import tifffile as tiff
def make_dataset(count, im, base_num, img_type, tif, overlap):
h, w = im.shape[:2]
stride = int(base_num * (1 - overlap))
if h < base_num or w < base_num:
print('Original image size is too small!')
quit()
else:
if (h - base_num) % stride == 0 and (w - base_num) % stride != 0:
for i in range((h - base_num) // stride + 1):
for j in range((w - base_num) // stride + 2):
if j == (w - base_num) // stride + 1 and i < (h - base_num) // stride + 1:
if len(im.shape) == 2:
new_img = im[stride * i:stride * i + base_num:, -base_num:]
else:
new_img = im[stride * i:stride * i + base_num:, -base_num:, :]
else:
if len(im.shape) == 2:
new_img = im[stride * i:stride * i + base_num, stride * j:stride * j + base_num]
else:
new_img = im[stride * i:stride * i + base_num, stride * j:stride * j + base_num, :]
if tif:
if img_type == 'tif':
tiff.imwrite(save_imgs_path + str(count).zfill(5) + ".tif", new_img)
else:
if len(im.shape) != 2:
b, g, r = cv2.split(new_img)
new_img = cv2.merge([r, g, b])
cv2.imwrite(save_imgs_path + str(count).zfill(5) + ".{}".format(img_type), new_img)
else:
cv2.imwrite(save_imgs_path + str(count).zfill(5) + ".{}".format(img_type), new_img)
count += 1
elif (w - base_num) % stride == 0 and (h - base_num) % stride != 0:
for i in range((h - base_num) // stride + 2):
for j in range((w - base_num) // stride + 1):
if i == (h - base_num) // stride + 1 and j < (w - base_num) // stride + 1:
if len(im.shape) == 2:
new_img = im[-base_num:, stride * j:stride * j + base_num]
else:
new_img = im[-base_num:, stride * j:stride * j + base_num, :]
else:
if len(im.shape) == 2:
new_img = im[stride * i:stride * i + base_num, stride * j:stride * j + base_num]
else:
new_img = im[stride * i:stride * i + base_num, stride * j:stride * j + base_num, :]
if tif:
if img_type == 'tif':
tiff.imwrite(save_imgs_path + str(count).zfill(5) + ".tif", new_img)
else:
if len(im.shape) != 2:
b, g, r = cv2.split(new_img)
new_img = cv2.merge([r, g, b])
cv2.imwrite(save_imgs_path + str(count).zfill(5) + ".{}".format(img_type), new_img)
else:
cv2.imwrite(save_imgs_path + str(count).zfill(5) + ".{}".format(img_type), new_img)
count += 1
elif (w - base_num) % stride == 0 and (h - base_num) % stride == 0:
for i in range((h - base_num) // stride + 1):
for j in range((w - base_num) // stride + 1):
if len(im.shape) == 2:
new_img = im[stride * i:stride * i + base_num, stride * j:stride * j + base_num]
else:
new_img = im[stride * i:stride * i + base_num, stride * j:stride * j + base_num, :]
if tif:
if img_type == 'tif':
tiff.imwrite(save_imgs_path + str(count).zfill(5) + ".tif", new_img)
else:
if len(im.shape) != 2:
b, g, r = cv2.split(new_img)
new_img = cv2.merge([r, g, b])
cv2.imwrite(save_imgs_path + str(count).zfill(5) + ".{}".format(img_type), new_img)
else:
cv2.imwrite(save_imgs_path + str(count).zfill(5) + ".{}".format(img_type), new_img)
count += 1
else:
for i in range((h - base_num) // stride + 2):
for j in range((w - base_num) // stride + 2):
if i == (h - base_num) // stride + 1 and j < (w - base_num) // stride + 1:
if len(im.shape) == 2:
new_img = im[-base_num:, stride * j:stride * j + base_num]
else:
new_img = im[-base_num:, stride * j:stride * j + base_num, :]
elif j == (w - base_num) // stride + 1 and i < (h - base_num) // stride + 1:
if len(im.shape) == 2:
new_img = im[stride * i:stride * i + base_num:, -base_num:]
else:
new_img = im[stride * i:stride * i + base_num:, -base_num:, :]
elif i == (h - base_num) // stride + 1 and j == (w - base_num) // stride + 1:
if len(im.shape) == 2:
new_img = im[-base_num:, -base_num:]
else:
new_img = im[-base_num:, -base_num:, :]
else:
if len(im.shape) == 2:
new_img = im[stride * i:stride * i + base_num, stride * j:stride * j + base_num]
else:
new_img = im[stride * i:stride * i + base_num, stride * j:stride * j + base_num, :]
if tif:
if img_type == 'tif':
tiff.imwrite(save_imgs_path + str(count).zfill(5) + ".tif", new_img)
else:
if len(im.shape) != 2:
b, g, r = cv2.split(new_img)
new_img = cv2.merge([r, g, b])
cv2.imwrite(save_imgs_path + str(count).zfill(5) + ".{}".format(img_type), new_img)
else:
cv2.imwrite(save_imgs_path + str(count).zfill(5) + ".{}".format(img_type), new_img)
count += 1
# args
imgs_path = r"" # dir containing large size images to be cropped
save_imgs_path = r"" # dir to save cropped images
is_graylabel = False # whether the path is grayscale label images
size = 512 # cropped size
img_type = 'tif' # the format in which the images are saved
overlap = 0 # the overlap ratio of crop patches
imgs_dir = os.listdir(imgs_path)
if len(os.listdir(save_imgs_path)) == 0:
count = 0
else:
count = int(os.listdir(save_imgs_path)[-1].split(".")[0]) + 1
begin_num = count
for i, j in enumerate(imgs_dir):
name = j.split(".")[1]
if name == 'tif':
im = tiff.imread(imgs_path + j)
tif = True
else:
im = cv2.imread(imgs_path + j, -1) if is_graylabel else cv2.imread(imgs_path + j)
tif = False
img_type = 'png' if is_graylabel else img_type # prevent labels being garbled after cropping
make_dataset(count, im, size, img_type, tif, overlap)
count = len(os.listdir(save_imgs_path))
print('No.{} image is cropped !'.format(i + 1))
print('All images have been cropped, the total of {} small size images have been obtained'.format(count - begin_num))