MCPcopy Create free account
hub / github.com/DingLei14/WiCoNet / create_crops

Function create_crops

utils/transform.py:166–207  ·  view source on GitHub ↗
(ims, labels, size)

Source from the content-addressed store, hash-verified

164 return img_rotate, label_rotate, x_s_rotate
165
166def create_crops(ims, labels, size):
167 crop_imgs = []
168 crop_labels = []
169 label_dims = len(labels[0].shape)
170 for img, label, in zip(ims, labels):
171 h = img.shape[0]
172 w = img.shape[1]
173 c_h = size[0]
174 c_w = size[1]
175 if h < c_h or w < c_w:
176 print("Cannot crop area {} from image with size ({}, {})".format(str(size), h, w))
177 crop_imgs.append(img)
178 crop_labels.append(label)
179 continue
180 h_rate = h/c_h
181 w_rate = w/c_w
182 h_times = math.ceil(h_rate)
183 w_times = math.ceil(w_rate)
184 if h_times==1: stride_h=0
185 else:
186 stride_h = math.ceil(c_h*(h_times-h_rate)/(h_times-1))
187 if w_times==1: stride_w=0
188 else:
189 stride_w = math.ceil(c_w*(w_times-w_rate)/(w_times-1))
190 for j in range(h_times):
191 for i in range(w_times):
192 s_h = int(j*c_h - j*stride_h)
193 if(j==(h_times-1)): s_h = h - c_h
194 e_h = s_h + c_h
195 s_w = int(i*c_w - i*stride_w)
196 if(i==(w_times-1)): s_w = w - c_w
197 e_w = s_w + c_w
198 # print('%d %d %d %d'%(s_h, e_h, s_w, e_w))
199 # print('%d %d %d %d'%(s_h_s, e_h_s, s_w_s, e_w_s))
200 crop_imgs.append(img[s_h:e_h, s_w:e_w, :])
201 if label_dims==2:
202 crop_labels.append(label[s_h:e_h, s_w:e_w])
203 else:
204 crop_labels.append(label[s_h:e_h, s_w:e_w, :])
205
206 print('Sliding crop finished. %d images created.' %len(crop_imgs))
207 return crop_imgs, crop_labels
208
209def create_crops_onlyimgs(ims, size):
210 crop_imgs = []

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected