-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
56 lines (41 loc) · 1.87 KB
/
common.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
import numpy as np
from scipy.signal import convolve2d
models = ['natural', 'secret']
epsilons = [round(e, 2) for e in np.arange(0.01, 0.51, 0.01)]
kernel = np.array([[-0.25, 0.5, -0.25],
[0.5, 0, 0.5],
[-0.25, 0.5, -0.25]])
mean_kernel = np.array([[0, 0.25, 0],
[0.25, 0, 0.25],
[0, 0.25, 0]])
large_mean_kernel = np.array([[0.125, 0.125, 0.125],
[0.125, 0, 0.125],
[0.125, 0.125, 0.125]])
def kernelResize(sample, kernel):
offsetx, offsety = getKernelOffset(kernel)
return sample[offsetx:sample.shape[0] - offsetx, offsety:sample.shape[1] - offsety]
def getKernelOffset(kernel):
offsetx = int(kernel.shape[0] / 2)
offsety = int(kernel.shape[1] / 2)
return offsetx, offsety
def diffToEstimate(sample, kernel, weights=None):
offsetx, offsety = getKernelOffset(kernel)
estimated = convolve2d(sample, kernel, mode='valid')
resized = kernelResize(sample, kernel)
diff = resized - estimated
if weights is None:
weights = np.ones_like(resized) / np.prod(resized.shape)
return np.mean(np.abs(np.multiply(diff, weights)))
def getWeightMatrix(sample):
offsetx, offsety = getKernelOffset(large_mean_kernel)
m = convolve2d(sample, mean_kernel, mode='valid')
weights = np.zeros_like(kernelResize(sample, kernel))
for x in range(weights.shape[0]):
for y in range(weights.shape[1]):
outerx = x + offsetx
outery = y + offsety
weights[x, y] = ((sample[outerx - 1, outery] - m[x, y]) ** 2 + (sample[outerx + 1, outery] - m[x, y]) ** 2
+ (sample[outerx, outery - 1] - m[x, y]) ** 2 + (sample[outerx, outery + 1] - m[x, y]) ** 2) / 3
weights = np.divide(1., weights + 5)
weights = weights / weights.sum()
return weights