Browse code

searchlight ready. missing nifti-image-write support. added timing to FBS

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@167 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on30/03/2009 17:54:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,81 @@
1
+%  When you load any ANALYZE or NIfTI file with 'load_nii.m', and view
2
+%  it with 'view_nii.m', you may find that the image is L-R flipped.
3
+%  This is because of the confusion of radiological and neurological
4
+%  convention in the medical image before NIfTI format is adopted.
5
+%
6
+%  Sometime, people even want to convert RAS (standard orientation) back
7
+%  to LAS orientation to satisfy the legend programs or processes. This
8
+%  program is only written for those purpose. So PLEASE BE VERY CAUTIOUS
9
+%  WHEN USING THIS 'FLIP_LR.M' PROGRAM.
10
+%
11
+%  With 'flip_lr.m', you can convert any ANALYZE or NIfTI (no matter
12
+%  3D or 4D) file to a flipped NIfTI file. This is implemented simply
13
+%  by flipping the affine matrix in the NIfTI header. Since the L-R
14
+%  orientation is determined there, so the image will be flipped.
15
+%
16
+%  Usage: flip_lr(original_fn, flipped_fn, [old_RGB],[tolerance],[preferredForm])
17
+%
18
+%  original_fn  -  filename of the original ANALYZE or NIfTI (3D or 4D) file
19
+%
20
+%  flipped_fn  -  filename of the L-R flipped NIfTI file
21
+%
22
+%  old_RGB (optional)  -  a scale number to tell difference of new RGB24
23
+%	from old RGB24. New RGB24 uses RGB triple sequentially for each
24
+%	voxel, like [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 from AnalyzeDirect
25
+%	uses old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for
26
+%	each slices. If the image that you view is garbled, try to set 
27
+%	old_RGB variable to 1 and try again, because it could be in
28
+%	old RGB24. It will be set to 0, if it is default or empty.
29
+%
30
+%  tolerance (optional) - distortion allowed for non-orthogonal rotation
31
+%	or shearing in NIfTI affine matrix. It will be set to 0.1 (10%),
32
+%	if it is default or empty.
33
+%
34
+%  preferredForm (optional)  -  selects which transformation from voxels
35
+%	to RAS coordinates; values are s,q,S,Q.  Lower case s,q indicate
36
+%	"prefer sform or qform, but use others if preferred not present". 
37
+%	Upper case indicate the program is forced to use the specificied
38
+%	tranform or fail loading.  'preferredForm' will be 's', if it is
39
+%	default or empty.	- Jeff Gunter
40
+%
41
+%  Example: flip_lr('avg152T1_LR_nifti.nii', 'flipped_lr.nii');
42
+%           flip_lr('avg152T1_RL_nifti.nii', 'flipped_rl.nii');
43
+%
44
+%  You will find that 'avg152T1_LR_nifti.nii' and 'avg152T1_RL_nifti.nii'
45
+%  are the same, and 'flipped_lr.nii' and 'flipped_rl.nii' are also the
46
+%  the same, but they are L-R flipped from 'avg152T1_*'.
47
+%
48
+%  NIFTI data format can be found on: http://nifti.nimh.nih.gov
49
+%
50
+%  - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
51
+%
52
+function flip_lr(original_fn, flipped_fn, old_RGB, tolerance, preferredForm)
53
+
54
+   if ~exist('original_fn','var') | ~exist('flipped_fn','var')
55
+      error('Usage: flip_lr(original_fn, flipped_fn, [old_RGB],[tolerance])');
56
+   end
57
+
58
+   if ~exist('old_RGB','var') | isempty(old_RGB)
59
+      old_RGB = 0;
60
+   end
61
+
62
+   if ~exist('tolerance','var') | isempty(tolerance)
63
+      tolerance = 0.1;
64
+   end
65
+
66
+   if ~exist('preferredForm','var') | isempty(preferredForm)
67
+      preferredForm= 's';				% Jeff
68
+   end
69
+
70
+   nii = load_nii(original_fn, [], [], [], [], old_RGB, tolerance, preferredForm);
71
+   M = diag(nii.hdr.dime.pixdim(2:5));
72
+   M(1:3,4) = -M(1:3,1:3)*(nii.hdr.hist.originator(1:3)-1)';
73
+   M(1,:) = -1*M(1,:);
74
+   nii.hdr.hist.sform_code = 1;
75
+   nii.hdr.hist.srow_x = M(1,:);
76
+   nii.hdr.hist.srow_y = M(2,:);
77
+   nii.hdr.hist.srow_z = M(3,:);
78
+   save_nii(nii, flipped_fn);
79
+
80
+   return;					% flip_lr
81
+