Here is a matlab m-function (Ifl2ana1.m) to convert GE MRI image file to
spm-compatible Analyze file.
Read the comment lines at the beginning of that file for how to use it.
Please note:
(1), this only works for 4X(?) format (some older scanners use), not for
5X
(2), the input files to this program are the GE files I.001,
I.002,..... with offset of 14336
(3), the program hard-codes the image size as 256 by 256, and voxel size
[0.9375 0.9375 1.5]mm. If yours are different, change them.
(4), this program does not read any header info from the GE file.
I have a separate file (GE_header.m) for doing that (thanks David Kennedy
for providing his c-code and the header files).
Kewei
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kewei Chen, Ph.D.
Bio-mathematician
Samaritan PET Center
602-239-4851
[log in to unmask]
[log in to unmask]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function Ifl2ana1(num_sli,byte,raw_mriana,flip,ssn,esn);
%function Ifl2ana(num_sli,byte,raw_mriana,flip,ssn,esn);
%num_sli: number of slices in the MRI (124, e.g.)
%byte=2: save as two byte data
% =1: save as one byte data (be cautious using this)
%raw_mridata: the analyze file name
%flip='no': do not fliplr and flipud (keep original orientation)
% ='yes': do flip (making it neurological orientation)
%ssn: start slice # (optional) (throw away any slice below it)
%esn: end slice # (optional) (throw away any slice above it)
% ssn and esn are optional. If not given, they
% will be determined interactively.
%example:
%Ifl2ana1(124,2,'mri_inAna.img','no');
%or
%Ifl2ana1(124,2,'mri_inAna.img','no',12,108);
%NOTE: In our hospital, sometimes the brain images was split
% half-half from the middel. In that case, the images have
% to be un-split. This is the 1st question this program
% asks you. If you see a 'normal' brain slice
% in the graphical window,
% answer 0 for not doing the un-split. On the other hand,
% if the brain slice is displayed as two disjoint half-half
% brain, answer 1 to un-split the image.
%Written by Kewei Chen, Ph.D, Samaritan PET Center.
offset=14336;
close all
if exist(raw_mriana)==2
eval(['delete ',raw_mriana]);
end;
if exist([clipext(raw_mriana),'.hdr'])==2;
eval(['delete ',clipext(raw_mriana),'.hdr']);
end;
if exist([clipext(raw_mriana),'.mat'])==2;
eval(['delete ',clipext(raw_mriana),'.mat']);
end;
foutr=fopen(raw_mriana,'a+');
i=40;
ifile=['I.0',int2str(i)];
fin=fopen(ifile,'r');
fseek(fin,offset,'bof');
img=fread(fin,[256 256],'ushort');
imagesc((img'));
colormap(gray);axis('off');axis('square');
un_split=input('un_split the image? (1=y,0: default): ');
if isempty(un_split);un_split=0;end;
close all
if nargin<=4;
h=figure('pos',[40 100 850 700]);
colormap(gray);
%epart=input('number of slices in the lower brain part to display: ');
epart=round(num_sli/4);
n=ceil(epart);
n=sqrt(n);kkk=floor(n);n=ceil(n);
if kkk*n<epart;kkk=kkk+1;end
ii=0;
for i=1:epart
ii=ii+1;
subplot(n,kkk,ii);
if i<10;
ifile=['I.00',int2str(i)];
elseif i<100;
ifile=['I.0',int2str(i)];
else
ifile=['I.',int2str(i)];
end;
fin=fopen(ifile,'r');
fseek(fin,offset,'bof');
img=fread(fin,[256 256],'ushort');
if un_split==1;
l=img(1:128,:);r=img(129:256,:);
img=[r; l];
end;
if size(img,2)<256;
aimg=zeros(256,256-size(img,2));
img=[img aimg];
end;
if flip(1)=='y';
img=fliplr(img);
img=flipud(img);
end;
fclose(fin);
%make image display 128 by 128
if nrow(img)==512;img=img(1:8:512,:);end
if ncol(img)==512;img=img(:,1:8:512);end
if nrow(img)==256;img=img(1:2:256,:);end
if ncol(img)==256;img=img(:,1:2:256);end
if any(any(img~=zeros(size(img))))==1;
imagesc((img'));
end;
text(100,100,int2str(i),'color','y');
axis('image');
axis('off');
pause(.1);
end
ssn=input('start slice number: ');
%epart=input('number of slices in the upper brain part to display: ');
%epart=33;
n=ceil(epart);
n=sqrt(n);kkk=floor(n);n=ceil(n);
if kkk*n<epart;kkk=kkk+1;end
clf
ii=0;
for i=num_sli-epart+1:num_sli
ii=ii+1;
subplot(n,kkk,ii);
if i<10;
ifile=['I.00',int2str(i)];
elseif i<100;
ifile=['I.0',int2str(i)];
else
ifile=['I.',int2str(i)];
end;
fin=fopen(ifile,'r');
fseek(fin,offset,'bof');
img=fread(fin,[256 256],'ushort');
if un_split==1;
l=img(1:128,:);r=img(129:256,:);
img=[r; l];
end;
if size(img,2)<256;
aimg=zeros(256,256-size(img,2));
img=[img aimg];
end;
if flip(1)=='y';
img=fliplr(img);
img=flipud(img);
end;
fclose(fin);
%make image display 128 by 128
if nrow(img)==512;img=img(1:8:512,:);end
if ncol(img)==512;img=img(:,1:8:512);end
if nrow(img)==256;img=img(1:2:256,:);end
if ncol(img)==256;img=img(:,1:2:256);end
if any(any(img~=zeros(size(img))))==1;
imagesc((img'));
end;
text(100,100,int2str(i),'color','y');
axis('image');
axis('off');
pause(.1);
end
esn=input('end slice number: ');
close;
end; %of if nargin<=4;
thre=255/4;
for i=ssn:esn
if i<10;
ifile=['I.00',int2str(i)];
elseif i<100;
ifile=['I.0',int2str(i)];
else
ifile=['I.',int2str(i)];
end;
%disp(ifile);
fin=fopen(ifile,'r');
if fin==-1;
disp(['slice ',int2str(i),' is missing']);
disp('using intepolation');
pause(4);
II=i-1;
if II<10;
ifile=['I.00',int2str(II)];
elseif II<100;
ifile=['I.0',int2str(II)];
else
ifile=['I.',int2str(II)];
end;
fin=fopen(ifile,'r');
fseek(fin,offset,'bof');
img1=fread(fin,[256 256],'ushort');
fclose(fin);
II=i+1;
if II<10;
ifile=['I.00',int2str(II)];
elseif II<100;
ifile=['I.0',int2str(II)];
else
ifile=['I.',int2str(II)];
end;
fin=fopen(ifile,'r');
fseek(fin,offset,'bof');
img2=fread(fin,[256 256],'ushort');
%fclose(fin);
img=(img1+img2)/2;
%********
else;
fseek(fin,offset,'bof');
img=fread(fin,[256 256],'ushort');
end; %of fin==-1;
if un_split==1;
l=img(1:128,:);r=img(129:256,:);
img=[r; l];
end;
%disp(['image size is ']);disp(size(img));
if size(img,2)<256;
aimg=zeros(256,256-size(img,2));
img=[img aimg];
end;
%disp(['image size is ']);disp(size(img));
if flip(1)=='y';
img=fliplr(img);
img=flipud(img);
end;
fclose(fin);
if byte==2
fwrite(foutr,img,'ushort');
elseif byte==1
brainmax=300;%something you can change
%may not be adequate for your scanner/research
img=img*255/brainmax;
ind=find(img>255);
img(ind)=255*ones(size(ind));
img=round(img);
fwrite(foutr,img,'uchar');
else
error('error in data type');
end;
disp(['slice ',int2str(i)]);
end; %of for i=ssn:esn (I think)
fclose(foutr);
num_sli=esn-ssn+1;
if byte==2;
spm_hwrite(raw_mriana,[256 256 num_sli],[0.9375 0.9375 1.5],1,...
spm_type('uint16'),0,[0 0 0],'GE2ana two byte');
elseif byte==1;
spm_hwrite(raw_mriana,[256 256 num_sli],[0.9375 0.9375 1.5],1,...
spm_type('uint8'),0,[0 0 0],'GE2ana one byte');
end;
return;
%to test:
%if the start slice is 21:
mri=getGEfile('I.021');
img=getimgsli('orig_mri.img',1,1);
img=flipud(fliplr(img));
if mri==img;disp('ok');end
|