-
Notifications
You must be signed in to change notification settings - Fork 5
/
load_compressed_nii.m
60 lines (51 loc) · 1.79 KB
/
load_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function [nifti_out] = load_compressed_nii(image_path, untouch)
%*****************************************************************
% LOAD NIFTI compressed nifti
%
% Simple function to decompress and load a nifti volume.
%
% 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:
% [nifti_out] = load_compressed_nii(image_path, untouch)
% IN:
% image_path - full path to compressed nifti file (*.nii.gz)
% untouch - if true, uses load_untouch_nii instead of load_nii (default:0)
% OUT:
% nifti_out - loaded nifti structure from niftiTools
%
% ****************************************************************
if(nargin<2)
untouch = 0;
end
% input file: full path with extension only
if(~exist(image_path,'file'))
error('Input file not found: %s',image_path);
end
[~, fnm] = fileparts(image_path);
[~,fnm] = fileparts(fnm); % get rid of *.nii
% assign a new tmp_id to each volume is useful when the same images are used in parallel
s = clock;
tmp_id = num2str(round(rand*1000*s(6)));
tmppath = fullfile(tempdir(),[fnm,'_', tmp_id]);
tmpGZ = [tmppath '.nii.gz'];
tmpNII = [tmppath '.nii'];
% copy original *.nii.gz to temp folder + unzip
copyfile(image_path, tmpGZ);
filenames = gunzip(tmpGZ);
if(length(filenames)~=1)
warning('multiple (%s) files in the gzip archive: probably not a proper .nii.gz file')
end
% load unzipped
if(untouch)
nifti_out = load_untouch_nii(tmpNII);
else
nifti_out = load_nii(tmpNII);
end
% clean-up both
delete(tmpGZ);
if exist(tmpNII, 'file')
delete(tmpNII);
end
end