Skip to content

Commit

Permalink
add atlas2nii
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Jul 14, 2017
1 parent 11ebe41 commit 9506108
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
43 changes: 43 additions & 0 deletions nii_atlas2nii.m
@@ -0,0 +1,43 @@
function nii_atlas2nii (atlasfnm)
%Convert NIfTI indexed atlas to series of NIfTI images, one per region
% atlasfnm = (optional) filenames to convert, e.g. 'aal.nii.gz'
%Examples
% nii_atlas2nii; %use GUI
% nii_atlas2nii('aal.nii.gz');

if ~exist('atlasfnm','var') %no files specified
atlasfnm = spm_select(1,'^.*\.(gz|voi|img|nii)$','Select Atlas to convert');
end;
[niiname, isGz] = unGzSub (atlasfnm); %eg. aal.nii.gz -> aal.nii
[p,n,~] =spm_fileparts(niiname);
hdr = spm_vol(niiname);
if ~spm_type(hdr.dt,'intt'), error('Atlases must have integer datatype %s\n', niiname); end;
img = spm_read_vols(hdr);
for i = 1 : max(img(:)) %for each region
imgI = img;
imgI(imgI ~= i) = 0; %mask images not of index value
imgI(imgI ~= 0) = 1; %binarize image
numVx = sum(imgI(:));
fprintf('region %d has %d voxels\n', i, numVx);
if numVx < 1, continue; end; %skip empty regions
hdr.fname = fullfile(p, [ n '_' num2str(i) '.nii']);
spm_write_vol(hdr,imgI);
end;
if isGz %delete uncompressed image: FSL does not allow 'img.nii' and 'img.nii.gz' to exist in same folder
delete(niiname);
end
%end nii_atlas2nii()

function [fnm, isGz] = unGzSub (fnm)
isGz = false;
[pth,nam,ext] = spm_fileparts(fnm);
if strcmpi(ext,'.gz') %.nii.gz -> .nii
isGz = true;
fnm = char(gunzip(fnm));
elseif strcmpi(ext,'.voi') %.voi -> .nii
isGz = true;
onam = char(gunzip(fnm));
fnm = fullfile(pth, [nam '.nii']);
movefile(onam,fnm);
end;
%end unGzSub()
5 changes: 4 additions & 1 deletion nii_enat_norm.m
Expand Up @@ -484,7 +484,10 @@ function coregEstTemplateSub(vols, modality)
%matlabbatch{1}.spm.spatial.coreg.estimate.source = {'/Users/rorden/Desktop/pre/bvisiblehuman.nii,1'};
matlabbatch{1}.spm.spatial.coreg.estimate.source = {[deblank(vols{1}),',1']};%{'/Users/rorden/Desktop/3D.nii,1'};
if numel(vols) > 1
matlabbatch{1}.spm.spatial.coreg.estimate.other = vols(2:end);% {''};
matlabbatch{1}.spm.spatial.coreg.estimate.other = vols(2:end);
if size(matlabbatch{1}.spm.spatial.coreg.estimate.other,2) > 1, %must be column
matlabbatch{1}.spm.spatial.coreg.estimate.other = matlabbatch{1}.spm.spatial.coreg.estimate.other';
end
else
matlabbatch{1}.spm.spatial.coreg.estimate.other = {''};
end
Expand Down
5 changes: 3 additions & 2 deletions nii_reslice_target.m
@@ -1,14 +1,14 @@
function [outhdr, outimg] = nii_reslice_target(inhdr, inimg, tarhdr, interp)
%Reslice input image to match dimensions of target image (either to disk or memory)
% inhdr: image to reslice- either filename of NIfTI header or loaded NIfTI header structure
% inimg: (optional) NIfTI image data (only if inhdr is a structure)
% inimg: (optonal) NIfTI image data (only if inhdr is a structure)
% tarhdr: image to match- either filename of NIfTI header or loaded NIfTI header structure
% interp: (optional) if 0 nearest neighbor interpolation, else trilinear interpolation (default)
% Outputs: if not specified, resliced image saved to disk, else returns resliced header and image
%Chris Rorden (2014) see John Ashburner and Ged Ridgway's reorient.m
% http://opensource.org/licenses/BSD-2-Clause
%Examples
% nii_reslice_target('T1_LM1001.nii','','jhu.nii',false);
% nii_reslice_target('T1_LM1001.nii','','jhu.nii',0);
% Next example shows how to use this without writing to disk
% inhdr = spm_vol('T1_LM1001.nii'); %load header
% inimg = spm_read_vols(inhdr); %load volume
Expand Down Expand Up @@ -38,6 +38,7 @@
if ~exist('interp','var')
interp = 1;%linear
end
if ~isnumeric(interp), interp = interp + 0; end; %change false/true to 0/1
if ~isstruct(tarhdr)
tarhdr = spm_vol(tarhdr); %load target header
end
Expand Down
1 change: 1 addition & 0 deletions nii_zeronan.m
Expand Up @@ -25,6 +25,7 @@ function nii_zeronan(fnms)
if numNan > 0
hdr.fname = fullfile(pth, ['z' nm ext]);
img(isnan(img)) = 0;%max(img(:)); % use ~isfinite instead of isnan to replace +/-inf with zero
%img = - img;
spm_write_vol(hdr,img);
end
end
Expand Down
Empty file added voi2nii.m
Empty file.

0 comments on commit 9506108

Please sign in to comment.