File size: 23,476 Bytes
c509e76 |
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
import os
from os.path import join as pjoin
import collections
import json
from numpy.lib.histograms import histogram_bin_edges
import torch
import numpy as np
import cv2
import random
import torch.nn.functional as F
from torch.utils import data
import glob
class DocResTrainDataset(data.Dataset):
def __init__(self, dataset={}, img_size=512,):
json_paths = dataset['json_paths']
self.task = dataset['task']
self.size = img_size
self.im_path = dataset['im_path']
self.datas = []
for json_path in json_paths:
with open(json_path,'r') as f:
data = json.load(f)
self.datas += data
self.background_paths = glob.glob('/data2/jiaxin/Training_Data/dewarping/doc_3d/background/*/*/*')
self.shadow_paths = glob.glob('/data2/jiaxin/Training_Data/illumination/doc3dshadow/new_shadow/*/*')
def __len__(self):
return len(self.datas)
def __getitem__(self, index):
data = self.datas[index]
in_im,gt_im,dtsprompt = self.data_processing(self.task,data)
return torch.cat((in_im,dtsprompt),0), gt_im
def data_processing(self,task,data):
if task=='deblurring':
## image prepare
in_im = cv2.imread(os.path.join(self.im_path,data['in_path']))
gt_im = cv2.imread(os.path.join(self.im_path,data['gt_path']))
dtsprompt = self.deblur_dtsprompt(in_im)
## get prompt
in_im, gt_im,dtsprompt = self.randomcrop([in_im,gt_im,dtsprompt])
in_im = self.rgbim_transform(in_im)
gt_im = self.rgbim_transform(gt_im)
dtsprompt = self.rgbim_transform(dtsprompt)
elif task =='dewarping':
## image prepare
in_im = cv2.imread(os.path.join(self.im_path,data['in_path']))
mask = cv2.imread(os.path.join(self.im_path,data['mask_path']))[:,:,0]
bm = np.load(os.path.join(self.im_path,data['gt_path'])).astype(np.float) #-> 0-448
bm = cv2.resize(bm,(448,448))
## add background
background = cv2.imread(random.choice(self.background_paths))
min_length = min(background.shape[:2])
crop_size = random.randint(int(min_length*0.5),min_length-1)
shift_y = np.random.randint(0,background.shape[1]-crop_size)
shift_x = np.random.randint(0,background.shape[0]-crop_size)
background = background[shift_x:shift_x+crop_size,shift_y:shift_y+crop_size,:]
background = cv2.resize(background,(448,448))
if np.mean(in_im[mask==0])<10:
in_im[mask==0]=background[mask==0]
## random crop and get prompt
in_im,mask,bm = self.random_margin_bm(in_im,mask,bm) # bm-> 0-1
in_im = cv2.resize(in_im,(self.size,self.size))
mask = cv2.resize(mask,(self.size,self.size))
mask_aug = self.mask_augment(mask)
in_im[mask_aug==0]=0
bm = cv2.resize(bm,(self.size,self.size)) # bm-> 0-1
bm_shift = (bm*self.size - self.getBasecoord(self.size,self.size))/self.size
base_coord = self.getBasecoord(self.size,self.size)/self.size
in_im = self.rgbim_transform(in_im)
base_coord = base_coord.transpose(2, 0, 1)
base_coord = torch.from_numpy(base_coord)
bm_shift = bm_shift.transpose(2, 0, 1)
bm_shift = torch.from_numpy(bm_shift)
mask[mask>155] = 255
mask[mask<=155] = 0
mask = mask/255
mask = np.expand_dims(mask,-1)
mask = mask.transpose(2, 0, 1)
mask = torch.from_numpy(mask)
mask_aug[mask_aug>155] = 255
mask_aug[mask_aug<=155] = 0
mask_aug = mask_aug/255
mask_aug = np.expand_dims(mask_aug,-1)
mask_aug = mask_aug.transpose(2, 0, 1)
mask_aug = torch.from_numpy(mask_aug)
in_im = in_im
gt_im = torch.cat((bm_shift,mask),0)
dtsprompt = torch.cat((base_coord,mask_aug),0)
elif task == 'binarization':
## image prepare
in_im = cv2.imread(os.path.join(self.im_path,data['in_path']))
gt_im = cv2.imread(os.path.join(self.im_path,data['gt_path']))
## get prompt
thr = cv2.imread(os.path.join(self.im_path,data['thr_path']))
bin_map = cv2.imread(os.path.join(self.im_path,data['bin_path']))
gradient = cv2.imread(os.path.join(self.im_path,data['gradient_path']))
bin_map[bin_map>155]=255
bin_map[bin_map<=155]=0
in_im, gt_im,thr,bin_map,gradient = self.randomcrop([in_im,gt_im,thr,bin_map,gradient])
in_im = self.randomAugment_binarization(in_im)
gt_im[gt_im>155]=255
gt_im[gt_im<=155]=0
gt_im = gt_im[:,:,0]
## transform
in_im = self.rgbim_transform(in_im)
thr = self.rgbim_transform(thr)
gradient = self.rgbim_transform(gradient)
bin_map = self.rgbim_transform(bin_map)
gt_im = gt_im.astype(np.float)/255.
gt_im = torch.from_numpy(gt_im)
gt_im = gt_im.unsqueeze(0)
dtsprompt = torch.cat((thr[0].unsqueeze(0),gradient[0].unsqueeze(0),bin_map[0].unsqueeze(0)),0)
elif task == 'deshadowing':
in_im = cv2.imread(os.path.join(self.im_path,data['in_path']))
gt_im = cv2.imread(os.path.join(self.im_path,data['gt_path']))
shadow_im = self.deshadow_dtsprompt(in_im)
if 'fsdsrd' in data['in_path']:
in_im = cv2.resize(in_im,(512,512))
gt_im = cv2.resize(gt_im,(512,512))
shadow_im = cv2.resize(shadow_im,(512,512))
in_im, gt_im,shadow_im = self.randomcrop([in_im,gt_im,shadow_im])
else:
in_im, gt_im,shadow_im = self.randomcrop([in_im,gt_im,shadow_im])
in_im = self.rgbim_transform(in_im)
gt_im = self.rgbim_transform(gt_im)
shadow_im = self.rgbim_transform(shadow_im)
dtsprompt = shadow_im
elif task == 'appearance':
if 'in_path' in data.keys():
cap_im = cv2.imread(os.path.join(self.im_path,data['in_path']))
gt_im = cv2.imread(os.path.join(self.im_path,data['gt_path']))
gt_im,cap_im = self.randomcrop_realdae(gt_im,cap_im)
cap_im = self.appearance_randomAugmentv1(cap_im)
enhance_result = self.appearance_dtsprompt(cap_im)
else:
gt_im = cv2.imread(os.path.join(self.im_path,data['gt_path']))
bleed_im = cv2.imread(os.path.join(self.im_path,random.choice(self.datas)['gt_path']))
bleed_im = cv2.resize(bleed_im,gt_im.shape[:2][::-1])
gt_im = self.randomcrop([gt_im])[0]
bleed_im = self.randomcrop([bleed_im])[0]
cap_im = self.bleed_trough(gt_im,bleed_im)
shadow_path = random.choice(self.shadow_paths)
shadow_im = cv2.imread(shadow_path)
cap_im = self.appearance_randomAugmentv2(cap_im,shadow_im)
enhance_result = self.appearance_dtsprompt(cap_im)
in_im = self.rgbim_transform(cap_im)
gt_im = self.rgbim_transform(gt_im)
dtsprompt = self.rgbim_transform(enhance_result)
return in_im, gt_im,dtsprompt
def randomcrop(self,im_list):
im_num = len(im_list)
## random scale rotate
if random.uniform(0,1) <= 0.8:
y,x = im_list[0].shape[:2]
angle = random.uniform(-180,180)
scale = random.uniform(0.7,1.5)
M = cv2.getRotationMatrix2D((int(x/2),int(y/2)),angle,scale)
for i in range(im_num):
im_list[i] = cv2.warpAffine(im_list[i],M,(x,y),borderValue=(255,255,255))
## random crop
crop_size = self.size
for i in range(im_num):
h,w = im_list[i].shape[:2]
h = max(h,crop_size)
w = max(w,crop_size)
im_list[i] = cv2.resize(im_list[i],(w,h))
if h==crop_size:
shift_y=0
else:
shift_y = np.random.randint(0,h-crop_size)
if w==crop_size:
shift_x=0
else:
shift_x = np.random.randint(0,w-crop_size)
for i in range(im_num):
im_list[i] = im_list[i][shift_y:shift_y+crop_size,shift_x:shift_x+crop_size,:]
return im_list
def deblur_dtsprompt(self,img):
x = cv2.Sobel(img,cv2.CV_16S,1,0)
y = cv2.Sobel(img,cv2.CV_16S,0,1)
absX = cv2.convertScaleAbs(x) # 转回uint8
absY = cv2.convertScaleAbs(y)
high_frequency = cv2.addWeighted(absX,0.5,absY,0.5,0)
high_frequency = cv2.cvtColor(high_frequency,cv2.COLOR_BGR2GRAY)
high_frequency = cv2.cvtColor(high_frequency,cv2.COLOR_GRAY2BGR)
return high_frequency
def appearance_dtsprompt(self,img):
h,w = img.shape[:2]
img = cv2.resize(img,(1024,1024))
rgb_planes = cv2.split(img)
result_planes = []
result_norm_planes = []
for plane in rgb_planes:
dilated_img = cv2.dilate(plane, np.ones((7,7), np.uint8))
bg_img = cv2.medianBlur(dilated_img, 21)
diff_img = 255 - cv2.absdiff(plane, bg_img)
norm_img = cv2.normalize(diff_img,None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
result_planes.append(diff_img)
result_norm_planes.append(norm_img)
result_norm = cv2.merge(result_norm_planes)
result_norm = cv2.resize(result_norm,(w,h))
return result_norm
def rgbim_transform(self,im):
im = im.astype(np.float)/255.
im = im.transpose(2, 0, 1)
im = torch.from_numpy(im)
return im
def random_margin_bm(self,in_im,msk,bm):
size = in_im.shape[:2]
[y, x] = (msk).nonzero()
minx = min(x)
maxx = max(x)
miny = min(y)
maxy = max(y)
s = 20
s = int(20*size[0]/128)
difference = int(5*size[0]/128)
cx1 = random.randint(0, s - difference)
cx2 = random.randint(0, s - difference) + 1
cy1 = random.randint(0, s - difference)
cy2 = random.randint(0, s - difference) + 1
t = miny-s+cy1
b = size[0]-maxy-s+cy2
l = minx-s+cx1
r = size[1]-maxx-s+cx2
t = max(0,t)
b = max(0,b)
l = max(0,l)
r = max(0,r)
in_im = in_im[t:size[0]-b,l:size[1]-r]
msk = msk[t:size[0]-b,l:size[1]-r]
bm[:,:,1]=bm[:,:,1]-t
bm[:,:,0]=bm[:,:,0]-l
bm=bm/np.array([448-l-r, 448-t-b])
return in_im,msk,bm
def mask_augment(self,mask):
if random.uniform(0,1) <= 0.6:
if random.uniform(0,1) <= 0.5:
mask = cv2.resize(mask,(64,64))
else:
mask = cv2.resize(mask,(128,128))
mask = cv2.resize(mask,(256,256))
mask[mask>155] = 255
mask[mask<=155] = 0
return mask
def bleed_trough(self, in_im, bleed_im):
if random.uniform(0,1) <= 0.5:
if random.uniform(0,1) <= 0.8:
ksize = np.random.randint(1,2)*2 + 1
bleed_im = cv2.blur(bleed_im,(ksize,ksize))
bleed_im = cv2.flip(bleed_im,1)
alpha = random.uniform(0.75,1)
in_im = cv2.addWeighted(in_im,alpha,bleed_im,1-alpha,0)
return in_im
def getBasecoord(self,h,w):
base_coord0 = np.tile(np.arange(h).reshape(h,1),(1,w)).astype(np.float32)
base_coord1 = np.tile(np.arange(w).reshape(1,w),(h,1)).astype(np.float32)
base_coord = np.concatenate((np.expand_dims(base_coord1,-1),np.expand_dims(base_coord0,-1)),-1)
return base_coord
def randomcrop_realdae(self,gt_im,cap_im):
if random.uniform(0,1) <= 0.5:
y,x = gt_im.shape[:2]
angle = random.uniform(-30,30)
scale = random.uniform(0.8,1.5)
M = cv2.getRotationMatrix2D((int(x/2),int(y/2)),angle,scale)
gt_im = cv2.warpAffine(gt_im,M,(x,y),borderValue=(255,255,255))
cap_im = cv2.warpAffine(cap_im,M,(x,y),borderValue=(255,255,255))
crop_size = self.size
if gt_im.shape[0] <= crop_size:
gt_im = cv2.copyMakeBorder(gt_im,crop_size-gt_im.shape[0]+1,0,0,0,borderType=cv2.BORDER_CONSTANT,value=(255,255,255))
cap_im = cv2.copyMakeBorder(cap_im,crop_size-cap_im.shape[0]+1,0,0,0,borderType=cv2.BORDER_CONSTANT,value=(255,255,255))
if gt_im.shape[1] <= crop_size:
gt_im = cv2.copyMakeBorder(gt_im,0,0,crop_size-gt_im.shape[1]+1,0,borderType=cv2.BORDER_CONSTANT,value=(255,255,255))
cap_im = cv2.copyMakeBorder(cap_im,0,0,crop_size-cap_im.shape[1]+1,0,borderType=cv2.BORDER_CONSTANT,value=(255,255,255))
shift_y = np.random.randint(0,gt_im.shape[1]-crop_size)
shift_x = np.random.randint(0,gt_im.shape[0]-crop_size)
gt_im = gt_im[shift_x:shift_x+crop_size,shift_y:shift_y+crop_size,:]
cap_im = cap_im[shift_x:shift_x+crop_size,shift_y:shift_y+crop_size,:]
return gt_im,cap_im
def randomAugment_binarization(self,in_img):
h,w = in_img.shape[:2]
## brightness
if random.uniform(0,1) <= 0.5:
high = 1.3
low = 0.8
ratio = np.random.uniform(low,high)
in_img = in_img.astype(np.float64)*ratio
in_img = np.clip(in_img,0,255).astype(np.uint8)
## contrast
if random.uniform(0,1) <= 0.5:
high = 1.3
low = 0.8
ratio = np.random.uniform(low,high)
gray = cv2.cvtColor(in_img,cv2.COLOR_BGR2GRAY)
mean = np.mean(gray)
mean_array = np.ones_like(in_img).astype(np.float64)*mean
in_img = in_img.astype(np.float64)*ratio + mean_array*(1-ratio)
in_img = np.clip(in_img,0,255).astype(np.uint8)
## color
if random.uniform(0,1) <= 0.5:
high = 0.2
low = 0.1
ratio = np.random.uniform(0.1,0.3)
random_color = np.random.randint(50,200,3).reshape(1,1,3)
random_color = (random_color*ratio).astype(np.uint8)
random_color = np.tile(random_color,(self.size,self.size,1))
in_img = in_img.astype(np.float64)*(1-ratio) + random_color
in_img = np.clip(in_img,0,255).astype(np.uint8)
return in_img
def deshadow_dtsprompt(self,img):
h,w = img.shape[:2]
img = cv2.resize(img,(1024,1024))
rgb_planes = cv2.split(img)
result_planes = []
result_norm_planes = []
bg_imgs = []
for plane in rgb_planes:
dilated_img = cv2.dilate(plane, np.ones((7,7), np.uint8))
bg_img = cv2.medianBlur(dilated_img, 21)
bg_imgs.append(bg_img)
diff_img = 255 - cv2.absdiff(plane, bg_img)
norm_img = cv2.normalize(diff_img,None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
result_planes.append(diff_img)
result_norm_planes.append(norm_img)
result_norm = cv2.merge(result_norm_planes)
bg_imgs = cv2.merge(bg_imgs)
bg_imgs = cv2.resize(bg_imgs,(w,h))
return bg_imgs
def randomAugment(self,in_img,gt_img,shadow_img):
h,w = in_img.shape[:2]
# random crop
crop_size = random.randint(128,1024)
if shadow_img.shape[0] <= crop_size:
shadow_img = cv2.copyMakeBorder(shadow_img,crop_size-shadow_img.shape[0]+1,0,0,0,borderType=cv2.BORDER_CONSTANT,value=(128,128,128))
if shadow_img.shape[1] <= crop_size:
shadow_img = cv2.copyMakeBorder(shadow_img,0,0,crop_size-shadow_img.shape[1]+1,0,borderType=cv2.BORDER_CONSTANT,value=(128,128,128))
shift_y = np.random.randint(0,shadow_img.shape[1]-crop_size)
shift_x = np.random.randint(0,shadow_img.shape[0]-crop_size)
shadow_img = shadow_img[shift_x:shift_x+crop_size,shift_y:shift_y+crop_size,:]
shadow_img = cv2.resize(shadow_img,(w,h))
in_img = in_img.astype(np.float64)*(shadow_img.astype(np.float64)+1)/255
in_img = np.clip(in_img,0,255).astype(np.uint8)
## brightness
if random.uniform(0,1) <= 0.5:
high = 1.3
low = 0.8
ratio = np.random.uniform(low,high)
in_img = in_img.astype(np.float64)*ratio
in_img = np.clip(in_img,0,255).astype(np.uint8)
## contrast
if random.uniform(0,1) <= 0.5:
high = 1.3
low = 0.8
ratio = np.random.uniform(low,high)
gray = cv2.cvtColor(in_img,cv2.COLOR_BGR2GRAY)
mean = np.mean(gray)
mean_array = np.ones_like(in_img).astype(np.float64)*mean
in_img = in_img.astype(np.float64)*ratio + mean_array*(1-ratio)
in_img = np.clip(in_img,0,255).astype(np.uint8)
## color
if random.uniform(0,1) <= 0.5:
high = 0.2
low = 0.1
ratio = np.random.uniform(0.1,0.3)
random_color = np.random.randint(50,200,3).reshape(1,1,3)
random_color = (random_color*ratio).astype(np.uint8)
random_color = np.tile(random_color,(self.img_size[0],self.img_size[1],1))
in_img = in_img.astype(np.float64)*(1-ratio) + random_color
in_img = np.clip(in_img,0,255).astype(np.uint8)
## scale and rotate
if random.uniform(0,1) <= 0:
y,x = self.img_size
angle = random.uniform(-180,180)
scale = random.uniform(0.5,1.5)
M = cv2.getRotationMatrix2D((int(x/2),int(y/2)),angle,scale)
in_img = cv2.warpAffine(in_img,M,(x,y),borderValue=0)
gt_img = cv2.warpAffine(gt_img,M,(x,y),borderValue=0)
# add noise
## jpegcompression
quanlity_high = 95
quanlity_low = 45
quanlity = int(np.random.randint(quanlity_low,quanlity_high))
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY),quanlity]
result, encimg = cv2.imencode('.jpg',in_img,encode_param)
in_img = cv2.imdecode(encimg,1).astype(np.uint8)
## gaussiannoise
mean = 0
sigma = 0.02
noise_ratio = 0.004
num_noise = int(np.ceil(noise_ratio*w))
coords = [np.random.randint(0,i-1,int(num_noise)) for i in [h,w]]
gauss = np.random.normal(mean,sigma,num_noise*3)*255
guass = np.reshape(gauss,(-1,3))
in_img = in_img.astype(np.float64)
in_img[tuple(coords)] += guass
in_img = np.clip(in_img,0,255).astype(np.uint8)
## blur
ksize = np.random.randint(1,2)*2 + 1
in_img = cv2.blur(in_img,(ksize,ksize))
## erase
if random.uniform(0,1) <= 0.7:
for i in range(100):
area = int(np.random.uniform(0.01,0.05)*h*w)
ration = np.random.uniform(0.3,1/0.3)
h_shift = int(np.sqrt(area*ration))
w_shift = int(np.sqrt(area/ration))
if (h_shift<h) and (w_shift<w):
break
h_start = np.random.randint(0,h-h_shift)
w_start = np.random.randint(0,w-w_shift)
randm_area = np.random.randint(low=0,high=255,size=(h_shift,w_shift,3))
in_img[h_start:h_start+h_shift,w_start:w_start+w_shift,:] = randm_area
return in_img, gt_img
def appearance_randomAugmentv1(self,in_img):
## brightness
if random.uniform(0,1) <= 0.8:
high = 1.3
low = 0.5
ratio = np.random.uniform(low,high)
in_img = in_img.astype(np.float64)*ratio
in_img = np.clip(in_img,0,255).astype(np.uint8)
## contrast
if random.uniform(0,1) <= 0.8:
high = 1.3
low = 0.5
ratio = np.random.uniform(low,high)
gray = cv2.cvtColor(in_img,cv2.COLOR_BGR2GRAY)
mean = np.mean(gray)
mean_array = np.ones_like(in_img).astype(np.float64)*mean
in_img = in_img.astype(np.float64)*ratio + mean_array*(1-ratio)
in_img = np.clip(in_img,0,255).astype(np.uint8)
## color
if random.uniform(0,1) <= 0.8:
high = 0.2
low = 0.1
ratio = np.random.uniform(0.1,0.3)
random_color = np.random.randint(50,200,3).reshape(1,1,3)
random_color = (random_color*ratio).astype(np.uint8)
random_color = np.tile(random_color,(self.size,self.size,1))
in_img = in_img.astype(np.float64)*(1-ratio) + random_color
in_img = np.clip(in_img,0,255).astype(np.uint8)
return in_img
def appearance_randomAugmentv2(self,in_img,shadow_img):
h,w = in_img.shape[:2]
# random crop
crop_size = random.randint(96,1024)
if shadow_img.shape[0] <= crop_size:
shadow_img = cv2.resize(shadow_img,(crop_size+1,crop_size+1))
if shadow_img.shape[1] <= crop_size:
shadow_img = cv2.resize(shadow_img,(crop_size+1,crop_size+1))
shift_y = np.random.randint(0,shadow_img.shape[1]-crop_size)
shift_x = np.random.randint(0,shadow_img.shape[0]-crop_size)
shadow_img = shadow_img[shift_x:shift_x+crop_size,shift_y:shift_y+crop_size,:]
shadow_img = cv2.resize(shadow_img,(w,h))
in_img = in_img.astype(np.float64)*(shadow_img.astype(np.float64)+1)/255
in_img = np.clip(in_img,0,255).astype(np.uint8)
## brightness
if random.uniform(0,1) <= 0.8:
high = 1.3
low = 0.5
ratio = np.random.uniform(low,high)
in_img = in_img.astype(np.float64)*ratio
in_img = np.clip(in_img,0,255).astype(np.uint8)
## contrast
if random.uniform(0,1) <= 0.8:
high = 1.3
low = 0.5
ratio = np.random.uniform(low,high)
gray = cv2.cvtColor(in_img,cv2.COLOR_BGR2GRAY)
mean = np.mean(gray)
mean_array = np.ones_like(in_img).astype(np.float64)*mean
in_img = in_img.astype(np.float64)*ratio + mean_array*(1-ratio)
in_img = np.clip(in_img,0,255).astype(np.uint8)
## color
if random.uniform(0,1) <= 0.8:
high = 0.2
low = 0.1
ratio = np.random.uniform(0.1,0.3)
random_color = np.random.randint(50,200,3).reshape(1,1,3)
random_color = (random_color*ratio).astype(np.uint8)
random_color = np.tile(random_color,(h,w,1))
in_img = in_img.astype(np.float64)*(1-ratio) + random_color
in_img = np.clip(in_img,0,255).astype(np.uint8)
if random.uniform(0,1) <= 0.8:
quanlity_high = 95
quanlity_low = 45
quanlity = int(np.random.randint(quanlity_low,quanlity_high))
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY),quanlity]
result, encimg = cv2.imencode('.jpg',in_img,encode_param)
in_img = cv2.imdecode(encimg,1).astype(np.uint8)
return in_img |