|
| 1 | +function [worldCoord, voxelCoord, maxVal] = getPeakCoordinates(varargin) |
| 2 | + % |
| 3 | + % This function gets the coordinates of a peak within a specified region of |
| 4 | + % interest. |
| 5 | + % |
| 6 | + % [worldCoord, voxelCoord, maxVal] = getPeakCoordinates(dataImage, roiImage, criticalT) |
| 7 | + % |
| 8 | + % |
| 9 | + % (C) Copyright 2021 CPP ROI developers |
| 10 | + |
| 11 | + isFile = @(x) exist(x, 'file') == 2; |
| 12 | + |
| 13 | + args = inputParser; |
| 14 | + |
| 15 | + args.addRequired('dataImage', isFile); |
| 16 | + args.addRequired('roiImage', isFile); |
| 17 | + args.addOptional('criticalT', 0, @isnumeric); |
| 18 | + |
| 19 | + args.parse(varargin{:}); |
| 20 | + |
| 21 | + dataImage = args.Results.dataImage; |
| 22 | + roiImage = args.Results.roiImage; |
| 23 | + criticalT = args.Results.criticalT; |
| 24 | + |
| 25 | + voxelCoord = nan(1, 3); |
| 26 | + worldCoord = nan(1, 3); |
| 27 | + |
| 28 | + maxVal = spm_summarise(spm_vol(dataImage), roiImage, @max); |
| 29 | + |
| 30 | + if ~checkRoiOrientation(dataImage, roiImage) |
| 31 | + return |
| 32 | + end |
| 33 | + |
| 34 | + if maxVal < criticalT |
| 35 | + warning('getPeakCoordinates:noMaxBeyondThreshold', ... |
| 36 | + 'No max value found beyond threshold %f in image:\n %s\n', ... |
| 37 | + dataImage, ... |
| 38 | + criticalT); |
| 39 | + return |
| 40 | + end |
| 41 | + |
| 42 | + data = spm_read_vols(spm_vol(dataImage)); |
| 43 | + mask = spm_read_vols(spm_vol(roiImage)); |
| 44 | + |
| 45 | + data(~mask) = nan(1, sum(~mask(:))); |
| 46 | + |
| 47 | + % Get the location of the higest t-value in slice space |
| 48 | + voxeIdx = find(data == maxVal); |
| 49 | + if numel(voxeIdx) > 1 |
| 50 | + % TODO return list of all maximums? |
| 51 | + warning('getPeakCoordinates:severalMaxBeyondThreshold', ... |
| 52 | + 'Several equal max value found beyond threshold %f in image:\n %s\n', ... |
| 53 | + dataImage, ... |
| 54 | + criticalT); |
| 55 | + return |
| 56 | + end |
| 57 | + |
| 58 | + [x, y, z] = ind2sub(size(data), voxeIdx); |
| 59 | + voxelCoord = [x, y, z]; |
| 60 | + |
| 61 | + % convert space from slice number to world coordinate |
| 62 | + worldCoord = cor2mni(voxelCoord, roiImage); |
| 63 | + |
| 64 | + % TODO |
| 65 | + % world_coord(1) = world_coord(1) * -1; |
| 66 | + % If masks created from AFNI or FSL, |
| 67 | + % the x coordinate could be flipped (multiplied x -1). |
| 68 | + % If this is the case, multiply x with -1. |
| 69 | + |
| 70 | +end |
| 71 | + |
| 72 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 73 | +%%% cor2mni |
| 74 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 75 | +function mni = cor2mni(cor, nifti_image) |
| 76 | + % function mni = cor2mni(cor, T) |
| 77 | + % convert matrix coordinate to mni coordinate |
| 78 | + % |
| 79 | + % cor: an Nx3 matrix |
| 80 | + % T: (optional) rotation matrix |
| 81 | + % mni is the returned coordinate in mni space |
| 82 | + % |
| 83 | + % caution: if T is not given, the default T is |
| 84 | + % T = ... |
| 85 | + % [-4 0 0 84;... |
| 86 | + % 0 4 0 -116;... |
| 87 | + % 0 0 4 -56;... |
| 88 | + % 0 0 0 1]; |
| 89 | + % |
| 90 | + % xu cui |
| 91 | + % 2004-8-18 |
| 92 | + % last revised: 2005-04-30 |
| 93 | + |
| 94 | + % if nargin == 1 |
| 95 | + % T = ... |
| 96 | + % [-4 0 0 84;... |
| 97 | + % 0 4 0 -116;... |
| 98 | + % 0 0 4 -56;... |
| 99 | + % 0 0 0 1]; |
| 100 | + % end |
| 101 | + |
| 102 | + V = spm_vol(nifti_image); |
| 103 | + T = V.mat; |
| 104 | + |
| 105 | + cor = round(cor); |
| 106 | + mni = T * [cor(:, 1) cor(:, 2) cor(:, 3) ones(size(cor, 1), 1)]'; |
| 107 | + mni = mni'; |
| 108 | + mni(:, 4) = []; |
| 109 | +end |
0 commit comments