forked from WCHN/CTseg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spm_CTseg_util.m
53 lines (46 loc) · 1.79 KB
/
spm_CTseg_util.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
function varargout = spm_CTseg_util(varargin)
% CTseg utility functions.
%
% FORMAT y = spm_CTseg_util('affine',d,Mat)
% FORMAT id = spm_CTseg_util('identity',d)
% FORMAT spm_CTseg_util('write_nii',pth,dat,M,descrip,typ)
%__________________________________________________________________________
[varargout{1:nargout}] = spm_subfun(localfunctions,varargin{:});
%==========================================================================
%==========================================================================
function y = affine(d,Mat)
id = identity(d);
y = reshape(reshape(id,[prod(d) 3])*Mat(1:3,1:3)' + Mat(1:3,4)',[d 3]);
if d(3) == 1, y(:,:,:,3) = 1; end
%==========================================================================
%==========================================================================
function id = identity(d)
id = zeros([d(:)',3],'single');
[id(:,:,:,1),id(:,:,:,2),id(:,:,:,3)] = ndgrid(single(1:d(1)),single(1:d(2)),single(1:d(3)));
%==========================================================================
%==========================================================================
function write_nii(pth,dat,M,descrip,typ)
if nargin<5, typ = 'float32'; end
spm_unlink(pth);
switch typ
case 'float32'
fa = file_array(pth,size(dat),typ,0);
case 'uint8'
mx = max(dat(isfinite(dat(:))));
fa = file_array(pth,size(dat),typ,0,mx/255,0);
case 'int16'
mx = max(dat(isfinite(dat(:))));
mn = min(dat(isfinite(dat(:))));
s = max(mx/32767,-mn/32768);
fa = file_array(pth,size(dat),typ,0,s,0);
otherwise
error('Can''t do datatype "%s"', typ);
end
Nii = nifti;
Nii.dat = fa;
Nii.mat = M;
Nii.mat0 = M;
Nii.descrip = descrip;
create(Nii);
Nii.dat(:,:,:,:,:,:) = dat;
%==========================================================================