-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata_tools.py
81 lines (73 loc) · 2.37 KB
/
data_tools.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
# -*- coding:utf-8 -*-
import cv2
import numpy as np
import random
def data_augmentation(image, mode):
'''
Performs dat augmentation of the input image
Input:
image: a cv2 (OpenCV) image
mode: int. Choice of transformation to apply to the image
0 - no transformation
1 - flip up and down
2 - rotate counterwise 90 degree
3 - rotate 90 degree and flip up and down
4 - rotate 180 degree
5 - rotate 180 degree and flip
6 - rotate 270 degree
7 - rotate 270 degree and flip
'''
if mode == 0:
# original
pass
elif mode == 1:
# flip up and down
out = np.flipud(image)
elif mode == 2:
# rotate counterwise 90 degree
out = np.rot90(image)
elif mode == 3:
# rotate 90 degree and flip up and down
out = np.rot90(image)
out = np.flipud(out)
elif mode == 4:
# rotate 180 degree
out = np.rot90(image, k=2)
elif mode == 5:
# rotate 180 degree and flip
out = np.rot90(image, k=2)
out = np.flipud(out)
elif mode == 6:
# rotate 270 degree
out = np.rot90(image, k=3)
elif mode == 7:
# rotate 270 degree and flip
out = np.rot90(image, k=3)
out = np.flipud(out)
else:
raise Exception('Invalid choice of image transformation')
return out
def sigma_estimate(im_noisy, im_gt, win, sigma_spatial):
noise2 = (im_noisy-im_gt)**2
sigma2_map_est = cv2.GaussianBlur(noise2, (win, win), sigma_spatial)
sigma2_map_est = sigma2_map_est.astype(np.float32)
sigma2_map_est = np.where(sigma2_map_est<1e-10, 1e-10, sigma2_map_est)
if sigma2_map_est.ndim == 2:
sigma2_map_est = sigma2_map_est[:, :, np.newaxis]
return sigma2_map_est
def random_augmentation(*args):
out = []
if random.randint(0,1) == 1:
flag_aug = random.randint(1,7)
for data in args:
out.append(data_augmentation(data, flag_aug).copy())
else:
for data in args:
out.append(data)
return out
def gaussian_kernel(H, W, center, scale):
centerH = center[0]
centerW = center[1]
XX, YY = np.meshgrid(np.arange(W), np.arange(H))
ZZ = 1./(2*np.pi*scale**2) * np.exp( (-(XX-centerH)**2-(YY-centerW)**2)/(2*scale**2) )
return ZZ