File size: 4,412 Bytes
0103f17 | 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 | import numpy as np
import cv2
def resize_and_pad(image, box):
'''Fitting an image to the box region while keeping the aspect ratio.'''
y1,y2,x1,x2 = box
H,W = y2-y1, x2-x1
h,w = image.shape[0], image.shape[1]
r_box = W / H
r_image = w / h
if r_box >= r_image:
h_target = H
w_target = int(w * H / h)
image = cv2.resize(image, (w_target, h_target))
w1 = (W - w_target) // 2
w2 = W - w_target - w1
pad_param = ((0,0),(w1,w2),(0,0))
image = np.pad(image, pad_param, 'constant', constant_values=255)
else:
w_target = W
h_target = int(h * W / w)
image = cv2.resize(image, (w_target, h_target))
h1 = (H-h_target) // 2
h2 = H - h_target - h1
pad_param =((h1,h2),(0,0),(0,0))
image = np.pad(image, pad_param, 'constant', constant_values=255)
return image
def expand_image_mask(image, mask, ratio=1.4):
h,w = image.shape[0], image.shape[1]
H,W = int(h * ratio), int(w * ratio)
h1 = int((H - h) // 2)
h2 = H - h - h1
w1 = int((W -w) // 2)
w2 = W -w - w1
pad_param_image = ((h1,h2),(w1,w2),(0,0))
pad_param_mask = ((h1,h2),(w1,w2))
image = np.pad(image, pad_param_image, 'constant', constant_values=255)
mask = np.pad(mask, pad_param_mask, 'constant', constant_values=0)
return image, mask
def expand_image(image, ratio=1.4):
h,w = image.shape[0], image.shape[1]
H,W = int(h * ratio), int(w * ratio)
h1 = int((H - h) // 2)
h2 = H - h - h1
w1 = int((W -w) // 2)
w2 = W -w - w1
pad_param_image = ((h1,h2),(w1,w2),(0,0))
image = np.pad(image, pad_param_image, 'constant', constant_values=255)
return image
def expand_bbox(mask,yyxx,ratio=[1.2,2.0], min_crop=0):
y1,y2,x1,x2 = yyxx
ratio = np.random.randint( ratio[0] * 10, ratio[1] * 10 ) / 10
H,W = mask.shape[0], mask.shape[1]
xc, yc = 0.5 * (x1 + x2), 0.5 * (y1 + y2)
h = ratio * (y2-y1+1)
w = ratio * (x2-x1+1)
h = max(h,min_crop)
w = max(w,min_crop)
x1 = int(xc - w * 0.5)
x2 = int(xc + w * 0.5)
y1 = int(yc - h * 0.5)
y2 = int(yc + h * 0.5)
x1 = max(0,x1)
x2 = min(W,x2)
y1 = max(0,y1)
y2 = min(H,y2)
return (y1,y2,x1,x2)
def box2squre(image, box):
H,W = image.shape[0], image.shape[1]
y1,y2,x1,x2 = box
cx = (x1 + x2) // 2
cy = (y1 + y2) // 2
h,w = y2-y1, x2-x1
if h >= w:
x1 = cx - h//2
x2 = cx + h//2
else:
y1 = cy - w//2
y2 = cy + w//2
x1 = max(0,x1)
x2 = min(W,x2)
y1 = max(0,y1)
y2 = min(H,y2)
return (y1,y2,x1,x2)
def pad_to_square(image, pad_value = 255, random = False):
H,W = image.shape[0], image.shape[1]
if H == W:
return image
padd = abs(H - W)
if random:
padd_1 = int(np.random.randint(0,padd))
else:
padd_1 = int(padd / 2)
padd_2 = padd - padd_1
if H > W:
pad_param = ((0,0),(padd_1,padd_2),(0,0))
else:
pad_param = ((padd_1,padd_2),(0,0),(0,0))
image = np.pad(image, pad_param, 'constant', constant_values=pad_value)
return image
def get_bbox_from_mask(mask):
h,w = mask.shape[0],mask.shape[1]
if mask.sum() < 10:
return 0, h, 0, w
rows = np.any(mask, axis=1)
cols = np.any(mask, axis=0)
y1,y2 = np.where(rows)[0][[0, -1]]
x1,x2 = np.where(cols)[0][[0, -1]]
return (y1, y2, x1, x2)
def box_in_box(small_box, big_box):
y1, y2, x1, x2 = small_box
y1_b, _, x1_b, _ = big_box
y1, y2, x1, x2 = y1 - y1_b ,y2 - y1_b, x1 - x1_b, x2 - x1_b
return (y1, y2, x1, x2)
def crop_back(pred, tar_image, extra_sizes, tar_box_yyxx_crop, tar_box_yyxx_crop2, is_masked=False):
H1, W1, H2, W2 = extra_sizes
y1, x1, y2, x2 = tar_box_yyxx_crop
y1_, x1_, y2_, x2_ = tar_box_yyxx_crop2
m = 0 # maigin_pixel
if H1 < W1:
pad1 = int((W1 - H1) / 2)
pad2 = W1 - H1 - pad1
pred = pred[pad1: -pad2, :, :]
elif H1 > W1:
pad1 = int((H1 - W1) / 2)
pad2 = H1 - W1 - pad1
pred = pred[:,pad1: -pad2, :]
if is_masked:
gen_image = tar_image.copy()
gen_image[y1+m :y2-m, x1+m:x2-m, :] = pred[y1+m :y2-m, x1+m:x2-m, :]
gen_image[y1_+m :y2_-m, x1_+m:x2_-m, :] = pred[y1_+m :y2_-m, x1_+m:x2_-m, :]
else:
gen_image = pred
return gen_image |