-
Notifications
You must be signed in to change notification settings - Fork 1
/
noiselevel.py
executable file
·209 lines (156 loc) · 7.88 KB
/
noiselevel.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
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
import numpy as np
from scipy.stats import gamma
from scipy.ndimage import correlate
from skimage.util import view_as_windows
class NoiseLevelEstimation:
def __init__(self, img, patchsize=7, decim=0, conf=1-1E-6, itr=3):
"""
Estimates noise level of input single noisy image assuming additive white Gaussian noise.
Parameters
----------
img: input single numpy image array
patchsize (optional): patch size (default: 7)
decim (optional): decimation factor. The larger the number, the faster the processing. (default: 0)
conf (optional): confidence interval to determine the threshold for the weak texture. This value is typically set very close to one. (default: 0.99)
itr (optional): number of iterations. (default: 3)
Attributes
----------
nlevel: estimated noise levels.
th: threshold to extract weak texture patches at the last iteration.
num: number of extracted weak texture patches at the last iteration.
mask: weak-texture mask. 0 and 1 represent non-weak-texture and weak-texture regions, respectively
Example
----------
estimate = NoiseLevelEstimation(noisy_image_array, patchsize=11, itr=10)
Notes
-----
Python Version: 20180718
Python Author: M. Kirk
Translated from Noise Level Estimation Matlab code: noiselevel.m
noiselevel.m Copyright (C) 2012-2015 Masayuki Tanaka; distributed under the BSD-2-Clause license
Reference
---------
Xinhao Liu, Masayuki Tanaka and Masatoshi Okutomi
Noise Level Estimation Using Weak Textured Patches of a Single Noisy Image
IEEE International Conference on Image Processing (ICIP), 2012.
Xinhao Liu, Masayuki Tanaka and Masatoshi Okutomi
Single-Image Noise Level Estimation for Blind Denoising Noisy Image
IEEE Transactions on Image Processing, Vol.22, No.12, pp.5226-5237, December, 2013.
"""
self.img = img
self.patchsize = patchsize
self.decim = decim
self.conf = conf
self.itr = itr
self.nlevel, self.th, self.num = self.noiselevel()
self.mask = self.weaktexturemask()
def noiselevel(self):
if len(self.img.shape) < 3:
self.img = np.expand_dims(self.img, 2)
nlevel = np.ndarray(self.img.shape[2])
th = np.ndarray(self.img.shape[2])
num = np.ndarray(self.img.shape[2])
kh = np.expand_dims(np.expand_dims(np.array([-0.5, 0, 0.5]), 0),2)
imgh = correlate(self.img, kh, mode='nearest')
imgh = imgh[:, 1: imgh.shape[1] - 1, :]
imgh = imgh * imgh
kv = np.expand_dims(np.vstack(np.array([-0.5, 0, 0.5])), 2)
imgv = correlate(self.img, kv, mode='nearest')
imgv = imgv[1: imgv.shape[0] - 1, :, :]
imgv = imgv * imgv
Dh = np.matrix(self.convmtx2(np.squeeze(kh,2), self.patchsize, self.patchsize))
Dv = np.matrix(self.convmtx2(np.squeeze(kv,2), self.patchsize, self.patchsize))
DD = Dh.getH() * Dh + Dv.getH() * Dv
r = np.double(np.linalg.matrix_rank(DD))
Dtr = np.trace(DD)
tau0 = gamma.ppf(self.conf, r / 2, scale=(2 * Dtr / r))
for cha in range(self.img.shape[2]):
X = view_as_windows(self.img[:, :, cha], (self.patchsize, self.patchsize))
X = X.reshape(np.int(X.size / self.patchsize ** 2), self.patchsize ** 2, order='F').transpose()
Xh = view_as_windows(imgh[:, :, cha], (self.patchsize, self.patchsize - 2))
Xh = Xh.reshape(np.int(Xh.size / ((self.patchsize - 2) * self.patchsize)),
((self.patchsize - 2) * self.patchsize), order='F').transpose()
Xv = view_as_windows(imgv[:, :, cha], (self.patchsize - 2, self.patchsize))
Xv = Xv.reshape(np.int(Xv.size / ((self.patchsize - 2) * self.patchsize)),
((self.patchsize - 2) * self.patchsize), order='F').transpose()
Xtr = np.expand_dims(np.sum(np.concatenate((Xh, Xv), axis=0), axis=0), 0)
if self.decim > 0:
XtrX = np.transpose(np.concatenate((Xtr, X), axis=0))
XtrX = np.transpose(XtrX[XtrX[:, 0].argsort(),])
p = np.floor(XtrX.shape[1] / (self.decim + 1))
p = np.expand_dims(np.arange(0, p) * (self.decim + 1), 0)
Xtr = XtrX[0, p.astype('int')]
X = np.squeeze(XtrX[1:XtrX.shape[1], p.astype('int')])
# noise level estimation
tau = np.inf
if X.shape[1] < X.shape[0]:
sig2 = 0
else:
cov = (np.asmatrix(X) @ np.asmatrix(X).getH()) / (X.shape[1] - 1)
d = np.flip(np.linalg.eig(cov)[0], axis=0)
sig2 = d[0]
for i in range(1, self.itr):
# weak texture selection
tau = sig2 * tau0
p = Xtr < tau
Xtr = Xtr[p]
X = X[:, np.squeeze(p)]
# noise level estimation
if X.shape[1] < X.shape[0]:
break
cov = (np.asmatrix(X) @ np.asmatrix(X).getH()) / (X.shape[1] - 1)
d = np.flip(np.linalg.eig(cov)[0], axis=0)
sig2 = d[0]
nlevel[cha] = np.sqrt(sig2)
th[cha] = tau
num[cha] = X.shape[1]
# clean up
self.img = np.squeeze(self.img)
return nlevel, th, num
def convmtx2(self, H, m, n):
# Specialized 2D convolution matrix generation
# H — Input matrix
# m — Rows in convolution matrix
# n — Columns in convolution matrix
s = np.shape(H)
T = np.zeros([(m - s[0] + 1) * (n - s[1] + 1), m * n])
k = 0
for i in range((m - s[0] + 1)):
for j in range((n - s[1] + 1)):
for p in range(s[0]):
T[k, (i + p) * n + j: (i + p) * n + j + 1 + s[1] - 1] = H[p, :]
k = k + 1
return T
def weaktexturemask(self):
if len(self.img.shape) < 3:
self.img = np.expand_dims(self.img, 2)
kh = np.expand_dims(np.transpose(np.vstack(np.array([-0.5, 0, 0.5]))), 2)
imgh = correlate(self.img, kh, mode='nearest')
imgh = imgh[:, 1: imgh.shape[1] - 1, :]
imgh = imgh * imgh
kv = np.expand_dims(np.vstack(np.array([-0.5, 0, 0.5])), 1)
imgv = correlate(self.img, kv, mode='nearest')
imgv = imgv[1: imgv.shape[0] - 1, :, :]
imgv = imgv * imgv
s = self.img.shape
msk = np.zeros_like(self.img)
for cha in range(s[2]):
m = view_as_windows(self.img[:, :, cha], (self.patchsize, self.patchsize))
m = np.zeros_like(m.reshape(np.int(m.size / self.patchsize ** 2), self.patchsize ** 2, order='F').transpose())
Xh = view_as_windows(imgh[:, :, cha], (self.patchsize, self.patchsize - 2))
Xh = Xh.reshape(np.int(Xh.size / ((self.patchsize - 2) * self.patchsize)),
((self.patchsize - 2) * self.patchsize), order='F').transpose()
Xv = view_as_windows(imgv[:, :, cha], (self.patchsize - 2, self.patchsize))
Xv = Xv.reshape(np.int(Xv.size / ((self.patchsize - 2) * self.patchsize)),
((self.patchsize - 2) * self.patchsize), order='F').transpose()
Xtr = np.expand_dims(np.sum(np.concatenate((Xh, Xv), axis=0), axis=0), 0)
p = Xtr < self.th[cha]
ind = 0
for col in range(0,s[1]-self.patchsize+1):
for row in range(0,s[0]-self.patchsize+1):
if p[:,ind]:
msk[row: row + self.patchsize - 1, col: col + self.patchsize - 1, cha] = 1
ind = ind + 1
# clean up
self.img = np.squeeze(self.img)
return np.squeeze(msk)