1 1 `new file mode 100644` ... ... `@@ -0,0 +1,95 @@` 1 ```+% Convert image of different orientations to standard Analyze orientation ``` 2 ```+% ``` 3 ```+% Usage: nii = rri_orient(nii); ``` 4 ```+ ``` 5 ```+% Jimmy Shen (jimmy@rotman-baycrest.on.ca), 26-APR-04 ``` 6 ```+%___________________________________________________________________ ``` 7 ```+ ``` 8 ```+function [nii, orient, pattern] = rri_orient(nii, varargin) ``` 9 ```+ ``` 10 ```+ if nargin > 1 ``` 11 ```+ pattern = varargin{1}; ``` 12 ```+ else ``` 13 ```+ pattern = []; ``` 14 ```+ end ``` 15 ```+ ``` 16 ```+ orient = [1 2 3]; ``` 17 ```+ dim = double(nii.hdr.dime.dim([2:4])); ``` 18 ```+ ``` 19 ```+ if ~isempty(pattern) & ~isequal(length(pattern), prod(dim)) ``` 20 ```+ return; ``` 21 ```+ end ``` 22 ```+ ``` 23 ```+ % get orient of the current image ``` 24 ```+ % ``` 25 ```+ orient = rri_orient_ui; ``` 26 ```+ pause(.1); ``` 27 ```+ ``` 28 ```+ % no need for conversion ``` 29 ```+ % ``` 30 ```+ if isequal(orient, [1 2 3]) ``` 31 ```+ return; ``` 32 ```+ end ``` 33 ```+ ``` 34 ```+ if isempty(pattern) ``` 35 ```+ pattern = 1:prod(dim); ``` 36 ```+ end ``` 37 ```+ ``` 38 ```+ pattern = reshape(pattern, dim); ``` 39 ```+ img = nii.img; ``` 40 ```+ ``` 41 ```+ % calculate after flip orient ``` 42 ```+ % ``` 43 ```+ rot_orient = mod(orient + 2, 3) + 1; ``` 44 ```+ ``` 45 ```+ % do flip: ``` 46 ```+ % ``` 47 ```+ flip_orient = orient - rot_orient; ``` 48 ```+ ``` 49 ```+ for i = 1:3 ``` 50 ```+ if flip_orient(i) ``` 51 ```+ pattern = flipdim(pattern, i); ``` 52 ```+ img = flipdim(img, i); ``` 53 ```+ end ``` 54 ```+ end ``` 55 ```+ ``` 56 ```+ % get index of orient (do inverse) ``` 57 ```+ % ``` 58 ```+ [tmp rot_orient] = sort(rot_orient); ``` 59 ```+ ``` 60 ```+ % do rotation: ``` 61 ```+ % ``` 62 ```+ pattern = permute(pattern, rot_orient); ``` 63 ```+ img = permute(img, [rot_orient 4 5 6]); ``` 64 ```+ ``` 65 ```+ % rotate resolution, or 'dim' ``` 66 ```+ % ``` 67 ```+ new_dim = nii.hdr.dime.dim([2:4]); ``` 68 ```+ new_dim = new_dim(rot_orient); ``` 69 ```+ nii.hdr.dime.dim([2:4]) = new_dim; ``` 70 ```+ ``` 71 ```+ % rotate voxel_size, or 'pixdim' ``` 72 ```+ % ``` 73 ```+ tmp = nii.hdr.dime.pixdim([2:4]); ``` 74 ```+ tmp = tmp(rot_orient); ``` 75 ```+ nii.hdr.dime.pixdim([2:4]) = tmp; ``` 76 ```+ ``` 77 ```+ % re-calculate originator ``` 78 ```+ % ``` 79 ```+ tmp = nii.hdr.hist.originator([1:3]); ``` 80 ```+ tmp = tmp(rot_orient); ``` 81 ```+ flip_orient = flip_orient(rot_orient); ``` 82 ```+ ``` 83 ```+ for i = 1:3 ``` 84 ```+ if flip_orient(i) & ~isequal(double(tmp(i)), 0) ``` 85 ```+ tmp(i) = int16(double(new_dim(i)) - double(tmp(i)) + 1); ``` 86 ```+ end ``` 87 ```+ end ``` 88 ```+ ``` 89 ```+ nii.hdr.hist.originator([1:3]) = tmp; ``` 90 ```+ ``` 91 ```+ nii.img = img; ``` 92 ```+ pattern = pattern(:); ``` 93 ```+ ``` 94 ```+ return; % rri_orient ``` 95 ```+ ```