Skip to content

Commit

Permalink
MRI resample: Add method option (linear instead of spline for SPM exp…
Browse files Browse the repository at this point in the history
…ort)
  • Loading branch information
ftadel committed Mar 22, 2021
1 parent 2834eca commit 721ba94
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion toolbox/anatomy/bst_normalize_mni.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
bst_progress('text', 'Resampling MRI...');
% Resample volume if needed
if any(abs(sMri.Voxsize - [1 1 1]) > 0.001)
[sMriRes, Tres] = mri_resample(sMri, [256 256 256], [1 1 1]);
[sMriRes, Tres] = mri_resample(sMri, [256 256 256], [1 1 1], 'linear');
else
sMriRes = sMri;
Tres = [];
Expand Down
25 changes: 19 additions & 6 deletions toolbox/anatomy/mri_resample.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
function [sMriNew, Transf, errMsg] = mri_resample(MriFile, CubeDim, Voxsize)
function [sMriNew, Transf, errMsg] = mri_resample(MriFile, CubeDim, Voxsize, Method)
% MRI_RESAMPLE: Reslice a volume using new dimensions and voxel resolution.
%
% USAGE: [sMriNew, Transf, errMsg] = mri_resample(sMri, CubeDim=[ask], Voxsize=[ask])
% [MriFileNew, Transf, errMsg] = mri_resample(MriFile, CubeDim=[ask], Voxsize=[ask])
% USAGE: [sMriNew, Transf, errMsg] = mri_resample(sMri, CubeDim=[ask], Voxsize=[ask], Method='linear')
% [MriFileNew, Transf, errMsg] = mri_resample(MriFile, CubeDim=[ask], Voxsize=[ask], Method='linear')
%
% INPUTS:
% - MriFile : Relative path to a Brainstorm MRI file (containing a Braintsorm MRI structure)
% - sMri : Brainstorm MRI structure (fields Cube, Voxsize, SCS, NCS...)
% - CubeDim : Dimensions [x,y,z] in voxels of the output volume
% - Voxsize : Resolution [x,y,z] in millimeters of one voxel of the output volume
% - Method : Interpolation method: {'linear', 'spline', 'cubic', 'nearest'}
%
% OUTPUTS:
% - MriFileNew : Relative path to the new Brainstorm MRI file (containing the structure sMriNew)
Expand Down Expand Up @@ -55,14 +56,18 @@
else
error('Invalid call.');
end
% Resampling method
if (nargin < 4) || isempty(Method)
Method = 'linear';
end
% Ask for destination sampling
oldCubeDim = size(sMri.Cube(:,:,:,1));
if (nargin < 3) || isempty(CubeDim) || isempty(Voxsize)
% Default values: current ones
oldVoxsize = sMri.Voxsize;
% Ask for size and resolution
res = java_dialog('input', {'New MRI dimensions in voxels: [x,y,z]', 'New MRI resolution in millimeters: [x,y,z]'}, 'Resample MRI', [], ...
{sprintf('[%d, %d, %d]', oldCubeDim), sprintf('[%1.4f, %1.4f, %1.4f]', oldVoxsize)});
res = java_dialog('input', {'New MRI dimensions in voxels: [x,y,z]', 'New MRI resolution in millimeters: [x,y,z]', 'Method: linear, spline, cubic, nearest'}, 'Resample MRI', [], ...
{sprintf('[%d, %d, %d]', oldCubeDim), sprintf('[%1.4f, %1.4f, %1.4f]', oldVoxsize), 'linear'});
% If user cancelled: return
if isempty(res)
if ~isProgress
Expand All @@ -73,6 +78,7 @@
% Get new values
CubeDim = str2num(res{1});
Voxsize = str2num(res{2});
Method = res{3};
if (length(CubeDim) ~= 3) || (length(Voxsize) ~= 3)
errMsg = 'Invalid inputs.';
if ~isProgress
Expand All @@ -86,6 +92,13 @@
bst_progress('stop');
end
return;
elseif ~ismember(Method, {'linear', 'spline', 'cubic', 'nearest'})
sMriNew = sMri;
errMsg = ['Invalid method: ' Method];
if ~isProgress
bst_progress('stop');
end
return;
end
end

Expand All @@ -106,7 +119,7 @@
n4 = size(sMri.Cube,4);
newCube = cell(1,n4);
for i4 = 1:n4
newCube{i4} = single(interp3(Y1, X1, Z1, double(sMri.Cube(:,:,:,i4)), Xgrid2, Ygrid2, Zgrid2, 'spline', 0));
newCube{i4} = single(interp3(Y1, X1, Z1, double(sMri.Cube(:,:,:,i4)), Xgrid2, Ygrid2, Zgrid2, Method, 0));
end
newCube = cat(4, newCube{:});
% Initialize transformed structure
Expand Down
2 changes: 1 addition & 1 deletion toolbox/process/functions/process_export_spmvol.m
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@
if (VolDownsample > 1)
newCubeDim = [size(sMri.Cube,1), size(sMri.Cube,2), size(sMri.Cube,3)] ./ VolDownsample;
newVoxsize = sMriOut.Voxsize .* VolDownsample;
sMriOut = mri_resample(sMriOut, newCubeDim, newVoxsize);
sMriOut = mri_resample(sMriOut, newCubeDim, newVoxsize, 'linear');
end
% Cut the empty slices
if isCutEmpty
Expand Down

0 comments on commit 721ba94

Please sign in to comment.