-
Notifications
You must be signed in to change notification settings - Fork 7
/
swe_thresholdImage.m
64 lines (54 loc) · 2.25 KB
/
swe_thresholdImage.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
function swe_thresholdImage(threshold, minimumClusterSize)
% Threshold an image, specified by selection window.
% =========================================================================
% FORMAT: swe_thresholdImage(threshold, minimumClusterSize)
% -------------------------------------------------------------------------
% Inputs:
%
% - threshold - minimum value (inclusive) of the surviving voxels
% - minimumClusterSize - the minimum size ofthe surviving clusters
% =========================================================================
% Version Info: $Format:%ci$ $Format:%h$
inputImageName = spm_select(1, 'image');
[pth, bnm, ext] = spm_fileparts(inputImageName);
VI = swe_data_hdr_read(inputImageName);
[Z, XYZ] = swe_data_read(VI);
XYZ = inv(VI.mat) * [XYZ; ones(1,VI.dim(1)*VI.dim(2)*VI.dim(3))];
XYZ = round(XYZ(1:3,:));
Z = swe_data_read(inputImageName, 'xyz', XYZ);
VI.fname = fullfile(pth, [bnm '_thresholded' ext]);
VI.descrip = [VI.descrip sprintf(' thresholdValue: %fminimumClusterSize: %i', threshold, minimumClusterSize)];
VI = swe_data_hdr_write(VI);
%-Calculate height threshold filtering
%--------------------------------------------------------------------------
Q = find(Z >= threshold);
%-Apply height threshold
%--------------------------------------------------------------------------
Z = Z(:,Q);
XYZ = XYZ(:,Q);
if isempty(Q)
fprintf('\n'); %-#
warning('SwE:NoVoxels','No voxels survive the thresholding');
else
%-Calculate extent threshold filtering
%----------------------------------------------------------------------
A = spm_clusters(XYZ);
Q = [];
for i = 1:max(A)
j = find(A == i);
if length(j) >= minimumClusterSize; Q = [Q j]; end
end
% ...eliminate voxels
%----------------------------------------------------------------------
Z = Z(:,Q);
XYZ = XYZ(:,Q);
if isempty(Q)
fprintf('\n'); %-#
warning('SwE:NoVoxels','No voxels survive the thresholding');
end
end
tmp= zeros(VI.dim);
Q = cumprod([1,VI.dim(1:2)])*XYZ - ...
sum(cumprod(VI.dim(1:2)));
tmp(Q) = Z;
swe_data_write(VI, tmp);