[a6edca]: / HelperFunctions / matlab / cropPad / cropPadNii.m

Download this file

50 lines (44 with data), 1.6 kB

 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
function cropPadNii(niiIn,niiOut,compressFlag,normalizeFlag,padNum)
%% crop the zero padding around nifti file
% Author: Da Ma (da_ma@sfu.ca)
% compressFlag: whether to compress the output nifti file (Default: False)
% normalizeFlag: whether/how to normalize the input volume
% 0 (default): donot normalize
% 1 : normalize to [0,1]
% [Yet to be implemented]:
% padNum: Number of voxels to pad around the cropped images
%%
% By default, don't compress .nii file to .nii.gz
if ~exist('compressFlag','var'); compressFlag=false;end
% By default, compress to [0,1]
if ~exist('normalizeFlag','var'); normalizeFlag=0;end
%% Read input nifti
disp('loading nifti file ...')
% read nifti volume
vol = niftiread(niiIn);
% % read nifti head
disp('read nifti header ...')
niiHead = niftiinfo(niiIn);
%% crop zero pad
disp('cropping nifti volume ...')
vol = cropPad(vol);
% [Yet to be implemented]:
% padNum: Number of voxels to pad around the cropped images%% normalze
if normalizeFlag == 1
vol = mat2gray(vol);
vol = single(vol);
end
%% update header dimension
niiHead.ImageSize = size(vol);
niiHead.raw.dim(2:4) = size(vol);
%% save nifti
disp('saving nifti file ...')
% determine if need to save as a '.nii.gz' compression file
[niiPath,niiName,niiExt] = fileparts(niiOut);
[~,~,internalExt] = fileparts(niiName);
if internalExt == ".nii" ||niiExt == ".gz"
compressFlag = 1; % = true
end
savePath = fullfile(niiPath,niiName);
%% save nii
niftiwrite(vol,savePath,niiHead,'Compressed',compressFlag);