Rss Feed
Tweeter button
Facebook button
Technorati button
Reddit button
Myspace button
Linkedin button
Webonews button
Delicious button
Digg button
Flickr button
Stumbleupon button
Newsvine button

Notes:

  • The site will be morphing over the next little while.
  • I am having some issues with tabs/spaces in the Python code. Sometimes they are correct, sometimes they get eaten. I am trying to figure it out.

Please feel free to leave a comment on a post if it interests you or if you have questions

Feb 082010

I am always look for different MRI file readers and writers for the myriad of formats that we use in MRI research. One of the relatively simple and common ones is the Analyze fileformat. Some of the large packages have writers (e.g., SPM) but I am typically wanting to do my own small processing and then write out the data. So, I wrote up my own writeanalyze.m function. It will do the basic formatting though the offsets etc don’t work. Try it out but I can’t guarantee anything.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function [] = writeanalyze(fname, data, ftype)

if( nargin == 2 )
    ftype = 'int16';
end

if( strcmp( ftype, 'int16' ) == 1 )
    file_type = 4; bpp = 16;
elseif( strcmp( ftype, 'uint16' ) == 1 )
    file_type = 4; bpp = 16;
elseif( strcmp( ftype, 'int32' ) == 1 )
    file_type = 8; bpp = 32;
elseif( strcmp( ftype, 'float' ) == 1 )
    file_type = 16; bpp = 32;
elseif( strcmp( ftype, 'double' ) == 1 )
    file_type = 64; bpp = 64;
else
    error(sprintf('Unknown data type %s', ftype));
end

fp = fopen([fname '.hdr'], 'wb', 'b');

%%
%%  Write the header_key part
%%
fwrite(fp, 348, 'int32');
fwrite(fp, repmat(' ', 1, 10), 'char');
fwrite(fp, repmat(' ', 1, 18), 'char');
fwrite(fp, 16384, 'int32');
fwrite(fp, 0, 'int16');
fwrite(fp, 'r ', 'char');


%%
%% Write the image_dimension part.
%%
fwrite(fp, length( size(data) ), 'int16');
for ii=1:length( size(data) )
    fwrite(fp, size(data,ii), 'int16');
end

for ii=length( size(data) )+1:7
    fwrite(fp, 1, 'int16');
end

fwrite(fp, 0, 'int16');  % unused 8
fwrite(fp, 0, 'int16');  % unused 9
fwrite(fp, 0, 'int16');  % unused 10
fwrite(fp, 0, 'int16');  % unused 11
fwrite(fp, 0, 'int16');  % unused 12
fwrite(fp, 0, 'int16');  % unused 13
fwrite(fp, 0, 'int16');  % unused 14

% data type
fwrite(fp, file_type, 'int16');   % 4 = signed short
fwrite(fp, bpp, 'int16');   % bpp
fwrite(fp, 0, 'int16');
for ii=1:8
    fwrite(fp, 1.0, 'float32');
end
fwrite(fp, 0, 'float32');
fwrite(fp, 0, 'float32'); % funused 1
fwrite(fp, 0, 'float32'); % funused 2
fwrite(fp, 0, 'float32'); % funused 3

fwrite(fp, max(data(:)), 'float32');
fwrite(fp, min(data(:)), 'float32');
fwrite(fp, 0, 'float32');
fwrite(fp, 0, 'float32');

fwrite(fp, round(max(data(:))), 'int32');   % glmax
fwrite(fp, round(min(data(:))), 'int32');   % glmin

%%
%%  Data history
%%
fwrite(fp, repmat(' ', 1, 80), 'char'); % descrip
fwrite(fp, repmat(' ', 1, 24), 'char'); % aux_file
fwrite(fp, '3', 'char'); % aux_file
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 10), 'char'); % originator
fwrite(fp, repmat(' ', 1, 3), 'char'); % originator

fwrite(fp, 0, 'int32'); % views
fwrite(fp, 0, 'int32'); % vols_added
fwrite(fp, 0, 'int32'); % start_fiedl
fwrite(fp, 0, 'int32'); % field_skip
fwrite(fp, 0, 'int32'); % omax
fwrite(fp, 0, 'int32'); % omin
fwrite(fp, 0, 'int32'); % small_max
fwrite(fp, 0, 'int32'); % small_min

fclose(fp);

%%
%%  Write the data
%%
fp = fopen([fname '.img'], 'wb', 'b');
fwrite(fp, data, ftype);
fclose(fp);

Leave a Reply

(required)

(required)

Spam Protection by WP-SpamFree

© 2010 Math, Computing and Research Please leave a comment or contact me craig@mri.brechmos.org if you have any questions. Suffusion WordPress theme by Sayontan Sinha