-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathompdenoise3.m
201 lines (151 loc) · 3.93 KB
/
ompdenoise3.m
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
function [y,nz] = ompdenoise3(params,msgdelta)
%OMPDENOISE3 OMP denoising of 3-D signals.
% OMPDENOISE3 denoises a 3-dimensional signal using OMP denoising. The
% function syntax is identical to OMPDENOISE, but it runs significantly
% faster on 3-D signals. OMPDENOISE3 requires somewhat more memory than
% OMPDENOISE (approximately the size of the input signal), so if memory is
% limited, OMPDENOISE can be used instead.
%
% See also OMPDENOISE.
% Ron Rubinstein
% Computer Science Department
% Technion, Haifa 32000 Israel
% ronrubin@cs
%
% August 2009
% parse input arguments %
x = params.x;
D = params.dict;
blocksize = params.blocksize;
% blocksize %
if (numel(blocksize)==1)
blocksize = ones(1,3)*blocksize;
end
% maxval %
if (isfield(params,'maxval'))
maxval = params.maxval;
else
maxval = 1;
end
% gain %
if (isfield(params,'gain'))
gain = params.gain;
else
gain = 1.15;
end
% maxatoms %
if (isfield(params,'maxatoms'))
maxatoms = params.maxatoms;
else
maxatoms = floor(prod(blocksize)/2);
end
% stepsize %
if (isfield(params,'stepsize'))
stepsize = params.stepsize;
if (numel(stepsize)==1)
stepsize = ones(1,3)*stepsize;
end
else
stepsize = ones(1,3);
end
if (any(stepsize<1))
error('Invalid step size.');
end
% noise mode %
if (isfield(params,'noisemode'))
switch lower(params.noisemode)
case 'psnr'
sigma = maxval / 10^(params.psnr/20);
case 'sigma'
sigma = params.sigma;
otherwise
error('Invalid noise mode specified');
end
elseif (isfield(params,'sigma'))
sigma = params.sigma;
elseif (isfield(params,'psnr'))
sigma = maxval / 10^(params.psnr/20);
else
error('Noise strength not specified');
end
% lambda %
if (isfield(params,'lambda'))
lambda = params.lambda;
else
lambda = maxval/(10*sigma);
end
% msgdelta %
if (nargin <2)
msgdelta = 5;
end
if (msgdelta<=0)
msgdelta = -1;
end
epsilon = sqrt(prod(blocksize)) * sigma * gain; % target error for omp
MEM_LOW = 1;
MEM_NORMAL = 2;
MEM_HIGH = 3;
if (isfield(params,'memusage'))
switch lower(params.memusage)
case 'low'
memusage = MEM_LOW;
case 'normal'
memusage = MEM_NORMAL;
case 'high'
memusage = MEM_HIGH;
otherwise
error('Invalid memory usage mode');
end
else
memusage = MEM_NORMAL;
end
% compute G %
G = [];
if (memusage >= MEM_NORMAL)
G = D'*D;
end
% verify dictionary normalization %
if (isempty(G))
atomnorms = sum(D.*D);
else
atomnorms = diag(G);
end
if (any(abs(atomnorms-1) > 1e-2))
error('Dictionary columns must be normalized to unit length');
end
% denoise the signal %
nz = 0; % count non-zeros in block representations
% the denoised signal
y = zeros(size(x));
blocknum = prod(floor((size(x)-blocksize)./stepsize) + 1);
processedblocks = 0;
tid = timerinit('ompdenoise', blocknum);
for k = 1:stepsize(3):size(y,3)-blocksize(3)+1
for j = 1:stepsize(2):size(y,2)-blocksize(2)+1
% the current batch of blocks
blocks = im2colstep(x(:,j:j+blocksize(2)-1,k:k+blocksize(3)-1),blocksize,stepsize);
% remove DC
[blocks, dc] = remove_dc(blocks,'columns');
% denoise the blocks
if (memusage == MEM_LOW)
gamma = omp2(D,blocks,[],epsilon,'maxatoms',maxatoms,'checkdict','off');
else
gamma = omp2(D'*blocks,sum(blocks.*blocks),G,epsilon,'maxatoms',maxatoms,'checkdict','off');
end
nz = nz + nnz(gamma);
cleanblocks = add_dc(D*gamma, dc, 'columns');
cleanvol = col2imstep(cleanblocks,[size(y,1) blocksize(2:3)],blocksize,stepsize);
y(:,j:j+blocksize(2)-1,k:k+blocksize(3)-1) = y(:,j:j+blocksize(2)-1,k:k+blocksize(3)-1) + cleanvol;
if (msgdelta>0)
processedblocks = processedblocks + size(blocks,2);
timereta(tid, processedblocks, msgdelta);
end
end
end
if (msgdelta>0)
timereta(tid, blocknum);
end
nz = nz/blocknum; % average number of non-zeros
% average the denoised and noisy signals
cnt = countcover(size(x),blocksize,stepsize);
y = (y+lambda*x)./(cnt + lambda);