-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsave_compressed_nii.m
42 lines (36 loc) · 1.25 KB
/
save_compressed_nii.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
function save_compressed_nii(nii_struct, output_path, untouch)
%*****************************************************************
% SAVE NIFTI compressed nifti
%
% Simple function to save a nifti volume in compressed mode.
%
% This library makes use of the wonderful toolbox provided by Jimmy Shen.
% https://www.mathworks.com/matlabcentral/fileexchange/8797-tools-for-nifti-and-analyze-image
%
% USE:
% save_compressed_nii(nii_image, image_path)
% IN:
% nii_struct - niftiTools structure, containing nifti data
% output_path - full filename to write the output nifti image, including
% file extension
% untouch - if true, use save_untouch_nii instead of save_nii (default:0)
%
% ****************************************************************
if(nargin<3)
untouch = false;
end
[pth, fname] = fileparts(output_path);
[~,fname] = fileparts(fname);
tmpNII = [pth '/',fname,'_tmp_.nii'];
tmpGZ = [tmpNII '.gz'];
% create temporary nii file
if(untouch)
save_untouch_nii(nii_struct, tmpNII);
else
save_nii(nii_struct,tmpNII);
end
% gzip + clear temporary *.nii file
gzip(tmpNII);
movefile(tmpGZ, output_path);
delete(tmpNII);
end