-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_frf.py
152 lines (121 loc) · 3.76 KB
/
utils_frf.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
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
import numpy as np
from scipy.ndimage.filters import uniform_filter,gaussian_filter
from scipy import signal
import warnings
from enum import Enum
from PIL import Image
class Filter(Enum):
UNIFORM = 0
GAUSSIAN = 1
def _initial_check(GT,P):
assert GT.shape == P.shape, "Supplied images have different sizes " + \
str(GT.shape) + " and " + str(P.shape)
if GT.dtype != P.dtype:
msg = "Supplied images have different dtypes " + \
str(GT.dtype) + " and " + str(P.dtype)
warnings.warn(msg)
if len(GT.shape) == 2:
GT = GT[:,:,np.newaxis]
P = P[:,:,np.newaxis]
return GT.astype(np.float64),P.astype(np.float64)
def _replace_value(array,value,replace_with):
array[array == value] = replace_with
return array
def _get_sums(GT,P,win,mode='same'):
mu1,mu2 = (filter2(GT,win,mode),filter2(P,win,mode))
return mu1*mu1, mu2*mu2, mu1*mu2
def _get_sigmas(GT,P,win,mode='same',**kwargs):
if 'sums' in kwargs:
GT_sum_sq,P_sum_sq,GT_P_sum_mul = kwargs['sums']
else:
GT_sum_sq,P_sum_sq,GT_P_sum_mul = _get_sums(GT,P,win,mode)
return filter2(GT*GT,win,mode) - GT_sum_sq,\
filter2(P*P,win,mode) - P_sum_sq, \
filter2(GT*P,win,mode) - GT_P_sum_mul
def fspecial(fltr,ws,**kwargs):
if fltr == Filter.UNIFORM:
return np.ones((ws,ws))/ ws**2
elif fltr == Filter.GAUSSIAN:
x, y = np.mgrid[-ws//2 + 1:ws//2 + 1, -ws//2 + 1:ws//2 + 1]
g = np.exp(-((x**2 + y**2)/(2.0*kwargs['sigma']**2)))
g[ g < np.finfo(g.dtype).eps*g.max() ] = 0
assert g.shape == (ws,ws)
den = g.sum()
if den !=0:
g/=den
return g
return None
def filter2(img,fltr,mode='same'):
return signal.convolve2d(img, np.rot90(fltr,2), mode=mode)
def _str_to_array(str):
pattern = r'''# Match (mandatory) whitespace between...
(?<=\]) # ] and
\s+
(?= \[) # [, or
|
(?<=[^\[\]\s])
\s+
(?= [^\[\]\s]) # two non-bracket non-whitespace characters
'''
return np.array(ast.literal_eval(str))
def _power_complex(a,b):
return a.astype('complex') ** b
def imresize(arr,size):
return np.array(Image.fromarray(arr).resize(size))
def _compute_bef(im, block_size=8):
"""Calculates Blocking Effect Factor (BEF) for a given grayscale/one channel image
C. Yim and A. C. Bovik, "Quality Assessment of Deblocked Images," in IEEE Transactions on Image Processing,
vol. 20, no. 1, pp. 88-98, Jan. 2011.
:param im: input image (numpy ndarray)
:param block_size: Size of the block over which DCT was performed during compression
:return: float -- bef.
"""
if len(im.shape) == 3:
height, width, channels = im.shape
elif len(im.shape) == 2:
height, width = im.shape
channels = 1
else:
raise ValueError("Not a 1-channel/3-channel grayscale image")
if channels > 1:
raise ValueError("Not for color images")
h = np.array(range(0, width - 1))
h_b = np.array(range(block_size - 1, width - 1, block_size))
h_bc = np.array(list(set(h).symmetric_difference(h_b)))
v = np.array(range(0, height - 1))
v_b = np.array(range(block_size - 1, height - 1, block_size))
v_bc = np.array(list(set(v).symmetric_difference(v_b)))
d_b = 0
d_bc = 0
# h_b for loop
for i in list(h_b):
diff = im[:, i] - im[:, i+1]
d_b += np.sum(np.square(diff))
# h_bc for loop
for i in list(h_bc):
diff = im[:, i] - im[:, i+1]
d_bc += np.sum(np.square(diff))
# v_b for loop
for j in list(v_b):
diff = im[j, :] - im[j+1, :]
d_b += np.sum(np.square(diff))
# V_bc for loop
for j in list(v_bc):
diff = im[j, :] - im[j+1, :]
d_bc += np.sum(np.square(diff))
# N code
n_hb = height * (width/block_size) - 1
n_hbc = (height * (width - 1)) - n_hb
n_vb = width * (height/block_size) - 1
n_vbc = (width * (height - 1)) - n_vb
# D code
d_b /= (n_hb + n_vb)
d_bc /= (n_hbc + n_vbc)
# Log
if d_b > d_bc:
t = log2(block_size)/log2(min(height, width))
else:
t = 0
# BEF
bef = t*(d_b - d_bc)
return bef