Skip to content

Commit 08161dc

Browse files
committed
FEAT: utility function for image resampling
1 parent 7e171a6 commit 08161dc

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

spm_CTseg_util.m

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,85 @@
44
% FORMAT y = spm_CTseg_util('affine',d,Mat)
55
% FORMAT id = spm_CTseg_util('identity',d)
66
% FORMAT spm_CTseg_util('write_nii',pth,dat,M,descrip,typ)
7+
% FORMAT pth_out = spm_CTseg_util('modify_img_vx',pth_in,vx,odir,deg,bc)
78
%__________________________________________________________________________
89

910
[varargout{1:nargout}] = spm_subfun(localfunctions,varargin{:});
1011
%==========================================================================
1112

13+
%==========================================================================
14+
function pth_out = modify_img_vx(pth_in,vx,odir,deg,bc)
15+
% Modify image voxel size.
16+
%
17+
% PARAMETERS
18+
% ----------
19+
% pth_in : char
20+
% Image input path
21+
% vx : float|[float,float,float]
22+
% Output image voxel size
23+
% odir : char, default=[same directory as input image, prefixed 'r']
24+
% Directory where to write output image
25+
% deg : int, default=1
26+
% Interpolation order
27+
% bc : int, default=0
28+
% Interpolation boundary condition
29+
%
30+
% RETURNS
31+
% ----------
32+
% pth_out : char
33+
% Modifed image output path
34+
%
35+
%_______________________________________________________________________
36+
if nargin < 3, odir = ''; end
37+
if nargin < 4, deg = 1; end
38+
if nargin < 5, bc = 0; end
39+
40+
if numel(vx) == 1, vx = vx*ones([1 3]); end
41+
if numel(deg) == 1, deg = deg*ones([1 3]); end
42+
if numel(bc) == 1, bc = bc*ones([1 3]); end
43+
44+
vx(vx == 0) = 1;
45+
46+
% Input image properties
47+
Nii = nifti(pth_in);
48+
img = Nii.dat(:,:,:);
49+
mat0 = Nii.mat;
50+
vx0 = sqrt(sum(mat0(1:3,1:3).^2));
51+
dm0 = size(img);
52+
mn = min(img(:));
53+
mx = max(img(:));
54+
55+
% Output image properties
56+
samp = vx0./vx;
57+
D = diag([samp 1]);
58+
mat = mat0/D;
59+
dm = floor(D(1:3,1:3)*dm0')';
60+
61+
% Make interpolation grid
62+
y = double(affine(dm,mat0\mat));
63+
64+
% Resample
65+
img = spm_bsplinc(img, [deg bc]);
66+
img = spm_bsplins(img,y(:,:,:,1),y(:,:,:,2),y(:,:,:,3),[deg bc]);
67+
img(~isfinite(img)) = 0;
68+
img = min(mx, max(mn, img));
69+
70+
% Make output
71+
[odir1,nam,ext] = fileparts(pth_in);
72+
if isempty(odir)
73+
odir = odir1;
74+
end
75+
pth_out = fullfile(odir,['r' nam ext]);
76+
oNii = nifti;
77+
oNii.dat = file_array(pth_out,dm,Nii.dat.dtype,Nii.dat.offset,Nii.dat.scl_slope,Nii.dat.scl_inter);
78+
oNii.mat = mat;
79+
oNii.mat0 = mat;
80+
oNii.descrip = 'Resampled';
81+
create(oNii);
82+
oNii.dat(:) = img(:);
83+
%==========================================================================
84+
85+
%==========================================================================
1286
function mask_outside_fov(pth_old, pth_new, val)
1387
% If pth_new has a larger field-of-view (fov) then pth_old, the differing
1488
% fov voxels will be set to the value defined by val.

0 commit comments

Comments
 (0)