source: misclib/fdf2nifti.m

Last change on this file was 173, checked in by tjniskan, 8 years ago
  • Added a function (fdf2nifti.m) for writing Varian FDF-files into a NIfTI file
  • Fixed a small bug in aedes_headerbrowser.m

A misclib/fdf2nifti.m
M aedes_readjcamp.m
M aedes_headerbrowser.m
M aedes_revision.m

File size: 2.8 KB
Line 
1function fdf2nifti(indir,outname)
2% FDF2NIFTI - Read Varian FDF files from a folder and output a single
3% NIfTI image
4%   
5%
6% Synopsis:
7%
8% Description:
9%
10% Examples:
11%
12% See also:
13%
14
15% Copyright (C) 2011 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uef.fi>
16%
17% Department of Applied Physics, Department of Neurobiology
18% University of Eastern Finland, Kuopio, FINLAND
19%
20% This program may be used under the terms of the GNU General Public
21% License version 2.0 as published by the Free Software Foundation
22% and appearing in the file LICENSE.TXT included in the packaging of
23% this program.
24%
25% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
28% Defaults
29promptInDir = true;
30promptOutName = true;
31
32if nargin == 1
33        promptInDir = false;
34elseif nargin == 2
35        promptInDir = false;
36        promptOutName = false;
37elseif nargin > 2
38        error('Too many input arguments.')
39end
40
41% Get default directory
42if ispref('Aedes','GetDataFileDir')
43        default_dir = getpref('Aedes','GetDataFileDir');
44else
45        default_dir = [pwd,filesep];
46end
47
48% Prompt for directory and file name
49if promptInDir
50        indir = uigetdir(default_dir,'Select .img directory');
51        if isequal(indir,0)
52                return
53        end
54end
55if ~strcmp(indir(end),filesep)
56        indir = [indir,filesep];
57end
58if promptOutName
59        [fn,fp,fe] = uiputfile({'*.nii','NIfTI files (*.nii)'},...
60                'Save NIfTI file as',[default_dir,'untitled.nii']);
61        if isequal(fn,0)
62                return
63        end
64        outname = [fp,fn];
65end
66
67% Get fdf files in the folder
68d = dir(indir);
69files = {d(~[d(:).isdir]).name};
70fdf_files = regexp(files,'^.*\.fdf$');
71if all(cellfun('isempty',regexp(files,'^(.*\.fdf)$')))
72        error('Could not find any FDF-files in "%s".',indir)
73end
74fdf_files = files(~cellfun('isempty',regexp(files,'^(.*\.fdf)$')));
75
76% Get indexes (slice, image echo)
77tmp = regexprep(fdf_files,'^slice(\d+)image(\d+)echo(\d+)\.fdf$','$1 $2 $3;');
78ind = str2num([tmp{:}]);
79
80% Sort using image number
81[tmp2,ind2] = sort(ind(:,2));
82ind=ind(ind2,:);
83
84nSlices = max(ind(:,1));
85nImages = max(ind(:,2));
86nEchos = max(ind(:,3));
87
88% Read first fdf file for determining dimensions
89data = aedes_readfdf([indir,fdf_files{1}]);
90sz = size(data.FTDATA);
91
92% Allocate space for NIfTI
93nifti_data = zeros([sz(1),sz(2),nSlices,nImages],'single');
94
95
96wbh = aedes_wbar(0/nImages,sprintf('Processing image %d/%d...',0,nImages));
97for tt=1:nImages
98        count = 1;
99        for kk=1:nSlices
100                for ii=1:nEchos
101                        fname = sprintf('slice%03dimage%03decho%03d.fdf',kk,tt,ii);
102                        data = aedes_readfdf([indir,fname]);
103                        nifti_data(:,:,count,tt) = data.FTDATA;
104                        count = count+1;
105                end
106        end
107        aedes_wbar(tt/nImages,wbh,sprintf('Processing image %d/%d...',tt,nImages));
108end
109aedes_wbar(1,wbh,sprintf('Writing data in NIfTI format to\n%s',outname));
110
111
112% Write the nifti data to file
113aedes_write_nifti(nifti_data,outname);
114
115% Close waitbar
116close(wbh);
117
118
119
120
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008