-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhdr2Nhdr.m
40 lines (32 loc) · 1.26 KB
/
hdr2Nhdr.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
function [fid] = hdr2Nhdr(hdr,mat,fn)
% hdr an existing nrrd hdr with the same dimentions
% fn - the name of the file to be written (with no extension).
% mat - the data file
if ~isfield(hdr,'spaceorigin')
hdr.spaceorigin = [-41.5,-41.5,-39];
end
fid = fopen([fn '.nhdr'], 'w');
[path,fn_short] = fileparts(fn);
fprintf(fid, ['NRRD0004\n' ...
'# Complete NRRD file format specification at:\n' ...
'# http://teem.sourceforge.net/nrrd/format.html\n' ...
'type: double\n' ...
'dimension: 3\n' ...
'space: %s\n' ...
'sizes: %d %d %d\n' ...
'space directions: (%g,%g,%g) (%g,%g,%g) (%g,%g,%g)\n' ...
'centerings: cell cell cell\n'...
'kinds: %s %s %s\n' ...
'endian: little\n' ...
'encoding: gzip\n' ...
'space units: "mm" "mm" "mm"\n'...
'space origin: (%g,%g,%g)\n' ...
'data file: %s.raw.gz\n'], ...
hdr.space, hdr.sizes(1:3), hdr.spacedirections, ...
char(hdr.kinds(1)),char(hdr.kinds(2)),char(hdr.kinds(3)), hdr.spaceorigin, fn_short);
fclose(fid);
fid = fopen([fn '.raw'], 'w');
fwrite(fid, mat, 'double');
fclose(fid);
system(['rm -f ' fn '.raw.gz && gzip ' fn '.raw']); % compress
end