/
ft_realtime_dicomproxy.m
109 lines (93 loc) · 3.41 KB
/
ft_realtime_dicomproxy.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
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
105
106
107
108
function ft_realtime_dicomproxy(cfg)
% FT_REALTIME_DICOMPROXY simulates an fMRI acquisition system by reading a series of
% DICOM files from disk, and streaming them to a FieldTrip buffer.
%
% Use as
% ft_realtime_dicomproxy(cfg)
%
% The target to write the data to is configured as
% cfg.target.datafile = string, target destination for the data (default = 'buffer://localhost:1972')
% cfg.input = string or cell-array of strings (see below)
% cfg.speedup = optional speedup parameter
%
% The input files can be specified as a cell-array of filenames, or as a single
% string with a wildcard, e.g., '/myhome/scan*.ima'
%
% This function requires functions from SPM, so make sure you have set up your path correctly.
%
% See also FT_REALTIME_SIGNALPROXY, FT_REALTIME_SIGNALVIEWER
% Copyright (C) 2010, Stefan Klanke
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
% set the defaults
if isempty(cfg) || ~isfield(cfg, 'target') || ~isfield(cfg.target, 'datafile')
cfg.target.datafile = 'buffer://localhost:1972';
end
cfg.target.dataformat = [];
if ~isfield(cfg, 'speedup')
cfg.speedup = 1;
end
if iscell(cfg.input)
fullnames = cfg.input;
N = numel(fullnames);
elseif size(cfg.input,1) == 1
[basedir, name, ext] = fileparts(cfg.input);
D = dir(cfg.input);
N = numel(D);
fullnames = cell(N,1);
for k=1:N
fullnames{k} = [basedir filesep D(k).name];
end
else
ft_error('Don''t know what to do with cfg.input');
end
DN = tempname; % Dicom Name
stopWatch = tic;
for n=1:N
copyfile(fullnames{n}, DN);
dh = spm_dicom_headers(DN);
R = spm_dicom_convert(dh,'all','flat','nii');
NN = R.files{1}; % NIFTI name
V = spm_vol(NN);
Y = int16(spm_read_vols(V));
if n==1
TR = dh{1}.RepetitionTime * 0.001;
NH = spm_vol_nifti(NN); % NIFTI header
f = fopen(R.files{1},'r');
rawNifti = fread(f, 348, 'uint8=>uint8');
fclose(f);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% create a FieldTrip compatible header structure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hdr = [];
hdr.nChans = prod(NH.dim);
hdr.nSamples = 0;
hdr.Fs = 1/TR;
hdr.nSamplesPre = 0;
hdr.nTrials = 1;
hdr.nifti_1 = rawNifti;
ft_write_data(cfg.target.datafile, Y(:), 'header', hdr, 'dataformat', cfg.target.dataformat, 'append', false);
else
ft_write_data(cfg.target.datafile, Y(:), 'header', hdr, 'dataformat', cfg.target.dataformat, 'append', true);
end
fprintf(1,'Wrote scan %i of %i at time t=%f\n', n, N, toc(stopWatch));
delete(DN);
delete(NN);
pause(n*TR/cfg.speedup - toc(stopWatch));
end