-
Notifications
You must be signed in to change notification settings - Fork 13
/
MPdenoising.m
217 lines (184 loc) · 7.98 KB
/
MPdenoising.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
function [Signal, Sigma] = MPdenoising(data, mask, kernel, sampling, centering)
%
% "MPPCA": 4d image denoising and noise map estimation by exploiting data redundancy in the PCA domain using universal properties of the eigenspectrum of
% random covariance matrices, i.e. Marchenko Pastur distribution
%
% [Signal, Sigma] = MPdenoising(data, mask, kernel, sampling)
% output:
% - Signal: [x, y, z, M] denoised data matrix
% - Sigma: [x, y, z] noise map
% input:
% - data: [x, y, z, M] data matrix
% - mask: (optional) region-of-interest [boolean]
% - kernel: (optional) window size, typically in order of [5 x 5 x 5]
% - sampling:
% 1. full: sliding window (default for noise map estimation, i.e. [Signal, Sigma] = MPdenoising(...) )
% 2. fast: block processing (default for denoising, i.e. [Signal] = MPdenoising(...))
%
% Authors: Jelle Veraart (jelle.veraart@nyumc.org)
% Copyright (c) 2016 New York Universit and University of Antwerp
%
% Permission is hereby granted, free of charge, to any non-commercial entity
% ('Recipient') obtaining a copy of this software and associated
% documentation files (the 'Software'), to the Software solely for
% non-commercial research, including the rights to use, copy and modify the
% Software, subject to the following conditions:
%
% 1. The above copyright notice and this permission notice shall be
% included by Recipient in all copies or substantial portions of the
% Software.
%
% 2. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIESOF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
% NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BELIABLE FOR ANY CLAIM,
% DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
% OTHERWISE, ARISING FROM, OUT OF ORIN CONNECTION WITH THE SOFTWARE OR THE
% USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% 3. In no event shall NYU be liable for direct, indirect, special,
% incidental or consequential damages in connection with the Software.
% Recipient will defend, indemnify and hold NYU harmless from any claims or
% liability resulting from the use of the Software by recipient.
%
% 4. Neither anything contained herein nor the delivery of the Software to
% recipient shall be deemed to grant the Recipient any right or licenses
% under any patents or patent application owned by NYU.
%
% 5. The Software may only be used for non-commercial research and may not
% be used for clinical care.
%
% 6. Any publication by Recipient of research involving the Software shall
% cite the references listed below.
%
% REFERENCES
% Veraart, J.; Fieremans, E. & Novikov, D.S. Diffusion MRI noise mapping
% using random matrix theory Magn. Res. Med., 2016, early view, doi:
% 10.1002/mrm.26059
if isa(data,'integer')
data = single(data);
end
[sx, sy, sz, M] = size(data);
if ~exist('mask', 'var') || isempty(mask)
mask = true([sx, sy, sz]);
end
if ~isa(mask,'boolean')
mask = mask>0;
end
if ~exist('kernel', 'var') || isempty(kernel)
kernel = [5 5 5];
end
if isscalar(kernel)
kernel = [kernel, kernel, kernel];
end
kernel = kernel + (mod(kernel, 2)-1); % needs to be odd.
k = (kernel-1)/2; kx = k(1); ky = k(2); kz = k(3);
N = prod(kernel);
if ~exist('sampling', 'var') || isempty(sampling)
if nargout > 1
sampling = 'full';
else
sampling = 'fast';
end
end
% create mask
if ~exist('mask', 'var') || isempty(mask)
mask = true(sx, sy, sz);
end
if ~exist('centering', 'var') || isempty(centering)
centering = false;
end
if strcmp(sampling, 'fast')
if nargout>1
warning('undersampled noise map will be returned')
end
% compute center points of patches
stats = regionprops(mask, 'BoundingBox');
n = ceil(stats.BoundingBox(4:6) ./ kernel);
x = linspace(ceil(stats.BoundingBox(1))+k(1), floor(stats.BoundingBox(1))-k(1) + stats.BoundingBox(4), n(1)); x = round(x);
y = linspace(ceil(stats.BoundingBox(2))+k(2), floor(stats.BoundingBox(2))-k(2) + stats.BoundingBox(5), n(2)); y = round(y);
z = linspace(ceil(stats.BoundingBox(3))+k(3), floor(stats.BoundingBox(3))-k(3) + stats.BoundingBox(6), n(3)); z = round(z);
[y, x, z] = meshgrid(x, y, z); x = x(:); y = y(:); z = z(:);
end
if strcmp(sampling, 'full')
warning('image boundaries are not processed.')
mask(1:k(1), :, :) = 0;
mask(sx-k(1)+1:sx, :, :) = 0;
mask(:, 1:k(2), :) = 0;
mask(:, sy-k(2)+1:sy, :, :) = 0;
mask(:,:,1:k(3)) = 0;
mask(:,:,sz-k(3)+1:sz) = 0;
x = []; y = []; z = [];
for i = k(3)+1:sz-k(3)
[x_, y_] = find(mask(:,:,i) == 1);
x = [x; x_]; y = [y; y_]; z = [z; i*ones(size(y_))];
end
x = x(:); y = y(:); z = z(:);
end
% Declare variables:
sigma = zeros(1, numel(x), 'like', data);
npars = zeros(1, numel(x), 'like', data);
signal = zeros(M, prod(kernel), numel(x), 'like', data);
Sigma = zeros(sx, sy, sz, 'like', data);
Npars = zeros(sx, sy, sz, 'like', data);
Signal = zeros(sx, sy, sz, M, 'like', data);
% compute scaling factor for in case N<M
R = min(M, N);
scaling = (max(M, N) - (0:R-centering-1)) / N;
scaling = scaling(:);
% start denoising
for nn = 1:numel(x)
% create data matrix
X = data(x(nn)-kx:x(nn)+kx, y(nn)-ky:y(nn)+ky, z(nn)-kz:z(nn)+kz, :);
X = reshape(X, N, M); X = X';
if centering
colmean = mean(X, 1);
X = X - repmat(colmean, [M, 1]);
end
% compute PCA eigenvalues
[u, vals, v] = svd(X, 'econ');
vals = diag(vals).^2 / N;
% First estimation of Sigma^2; Eq 1 from ISMRM presentation
csum = cumsum(vals(R-centering:-1:1)); cmean = csum(R-centering:-1:1)./(R-centering:-1:1)'; sigmasq_1 = cmean./scaling;
% Second estimation of Sigma^2; Eq 2 from ISMRM presentation
gamma = (M - (0:R-centering-1)) / N;
rangeMP = 4*sqrt(gamma(:));
rangeData = vals(1:R-centering) - vals(R-centering);
sigmasq_2 = rangeData./rangeMP;
% sigmasq_2 > sigma_sq1 if signal-components are represented in the
% eigenvalues
t = find(sigmasq_2 < sigmasq_1, 1);
if isempty(t)
sigma(nn) = NaN;
signal(:, :, nn) = X;
t = R+1;
else
sigma(nn) = sqrt(sigmasq_1(t));
vals(t:R) = 0;
s = u*diag(sqrt(N*vals))*v';
if centering
s = s + repmat(colmean, [M, 1]);
end
signal(:, :, nn) = s;
end
npars(nn) = t-1;
end
for nn = 1:numel(x)
Sigma(x(nn), y(nn), z(nn)) = sigma(nn);
Npars(x(nn), y(nn), z(nn)) = npars(nn);
if strcmp(sampling, 'fast')
Signal(x(nn)-k(1):x(nn)+k(1),y(nn)-k(2):y(nn)+k(2),z(nn)-k(3):z(nn)+k(3), :) = unpatch(signal(:,:,nn), k);
elseif strcmp(sampling, 'full')
Signal(x(nn), y(nn),z(nn), :) = signal(:,ceil(prod(kernel)/ 2),nn);
end
end
end
function data = unpatch(X, k)
kernel=k+k+1;
data = zeros([kernel, size(X, 1)]);
tmp = zeros(kernel);
for i = 1:size(X, 1);
tmp(:) = X(i, :);
data(:,:,:,i) = tmp;
end
end