| 382 | |
| 383 | |
| 384 | def compute_crop_size(mask): |
| 385 | H,W = mask.shape[:2] |
| 386 | index = np.nonzero(mask) |
| 387 | left = np.min(index[1]) |
| 388 | right = np.max(index[1]) |
| 389 | top = np.min(index[0]) |
| 390 | bottom = np.max(index[0]) |
| 391 | horizontal = H - (right-left) |
| 392 | vertical = W - (bottom-top) |
| 393 | crop_size = max(horizontal,vertical) |
| 394 | crop_size = random.randint(0,crop_size) |
| 395 | ratio_left = left/(horizontal) |
| 396 | |
| 397 | ratio_top = top/(vertical) |
| 398 | |
| 399 | crop_left = int(ratio_left*crop_size) |
| 400 | crop_right = W - crop_size + crop_left |
| 401 | crop_top = int(ratio_top*crop_size) |
| 402 | |
| 403 | crop_bottom = H -crop_size + crop_top |
| 404 | return crop_left,crop_right,crop_top,crop_bottom |
| 405 | |
| 406 | |
| 407 | def dilate_erode_mask(mask): |