Skip to content

Commit

Permalink
tiff2nii updates, avoid conflicts with nii_preprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Aug 4, 2016
1 parent 31c3d92 commit 5537eca
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions nii_make3d.m
Expand Up @@ -4,8 +4,11 @@

inname = fullfile(spm('Dir'),'apriori','brainmask.nii');
if ~exist(inname, 'file')
fprintf('%s error: unable to find image named %s\n', mfilename,inname);
return;
inname = fullfile(spm('Dir'),'canonical','avg152T1.nii');
if ~exist(inname, 'file')
fprintf('%s error: unable to find image named %s\n', mfilename,inname);
return;
end;
end
hdr = spm_vol([inname,',1']);
img = spm_read_vols(hdr);
Expand Down
7 changes: 4 additions & 3 deletions nii_scale_dims.m
Expand Up @@ -37,9 +37,10 @@ function nii_scale_dims(fnms, scale)
img = spm_read_vols(hdr);

hdrOut = hdr(1);
hdrOut.dim(1) = hdr(1).dim(1)*scale(1);
hdrOut.dim(2) = hdr(1).dim(2)*scale(2);
hdrOut.dim(3) = hdr(1).dim(3)*scale(3);
hdrOut.dim(1) = round(hdr(1).dim(1)*scale(1));
hdrOut.dim(2) = round(hdr(1).dim(2)*scale(2));
hdrOut.dim(3) = round(hdr(1).dim(3)*scale(3));

hdrOut.mat(1:3, 1:3) = hdr(1).mat(1:3, 1:3)*[1/scale(1) 0 0; 0 1/scale(2) 0; 0 0 1/scale(3)];
% http://stackoverflow.com/questions/12520152/resizing-3d-matrix-image-in-matlab
% monotonic, use the methods '*linear', '*cubic', or '*nearest'.
Expand Down
31 changes: 29 additions & 2 deletions nii_tiff2nii.m
@@ -1,15 +1,29 @@
function nii_tiff2nii (files)
function nii_tiff2nii (files, maskborder, invert)
%Convert TIFF stack to NIFTI
% files : (optional) cell string with filenames to convert
% maskborder : 0=no change, 1=dark, 2=bright
% invert: 0=no, 1=yes
%Examples
% nii_tiff2nii %use GUI
% nii_tiff2nii({'DWI_STAC.tiff'})
% nii_tiff2nii({'DWI_STAC.tiff','test.TIF'})

if ~exist('files','var')
[files,pth] = uigetfile({'*.tiff;';'*.*'},'Select TIFF Image[s]', 'MultiSelect', 'on');
[files,pth] = uigetfile({'*.tiff;*.tif';'*.*'},'Select TIFF Image[s]', 'MultiSelect', 'on');
files = strcat(pth, files); %add path to all file names
end;
if ~exist('invert','var')
prompt = {'Mask border (0=no, 1=dark, 2=bright):',...
'Invert intensity (0=no, 1=yes):'};
dlg_title = ['TIFF conversion options'];
num_lines = [1 50];
def = {'0','0'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
if isempty(answer), return; end;
maskborder = str2num(answer{1});
invert = str2num(answer{2});
end;

files = cellstr(files); %make cellstr nii_tiff2nii({'DWI_STAC.tiff'}) == nii_tiff2nii('DWI_STAC.tiff')
niiname = fullfile(spm('Dir'),'canonical','avg152T1.nii'); %sample nifti file for header
if ~exist(niiname,'file')
Expand Down Expand Up @@ -50,7 +64,20 @@ function nii_tiff2nii (files)
hdr.mat(3,4) = -0.5 * hdr.mat(3,3) * sz(3);
hdr.dim = sz;
hdr.pinfo = [0;0;0];
if maskborder ~= 0;
if maskborder == 1
msk = 0;
else
msk = 255;
end
img(img == img(2,2,1)) = msk;
img(:,end,:) = msk; %some JHU TIF images have wierd row
end
if invert
img = max(img(:)) - img;
end
spm_write_vol(hdr,img);

nii_setOrigin12(hdr.fname,3,true);
end;
%end nii_tiff2nii()

0 comments on commit 5537eca

Please sign in to comment.