Could you please inform me if the function roi2analyze is still applicable?It seems that it doesn't work anymore.

1 view (last 30 days)
function mask=roi2analyze(roi_file,Pin,DoWrite);
% Reads a volume mask in the MRIcro's ROI format.
% usage: mask = roi2analyze(roi_file,template_file,DoWrite)
% example: roi2analyze('c:\ax.roi','c:\ax.img',1);
%
% roi_file : name (and path) of MRICRO roi file (& header)
% Pin : template file (the file the ROi was defined in, or one with
% the same dimensions
% DoWrite : flag whether to write out the analyze image (=1) or just hand
% back the binary mask (=0). A '_roi' is appended to the file name.
%
% mask : return bitwise mask of roi as 3D matrix, is basically compatible to
% spm_read_vols-output (first return variable, Y),
% but only 3-dimensional. see also spm_read_vols.m
%
% 2-2007 by H.Schuetze, Dept. Neurology II, Magdeburg
% additional info at: http://www.sph.sc.edu/comd/rorden/roiformat/
%
% Here is a description of the format from
% http://hendrix.imm.dtu.dk/software/brede/doc/m2html/brede/brede_write_mricro_roi.html
% The format is a kind of slice-based run length encoding:
% [ sliceIndex wordsInSlice beginIndex runLength beginIndex
% runLength ... sliceIndex wordsInSlice beginIndex runLength
% beginIndex runLength ... ]
% Indices start from left posterior inferior (if the
% volume is oriented in the SPM99-ANALYZE way). Indices and
% lengths are stored as 16-bit little endians, thus the
% extended format is used to store large slices (e.g. 512x512).
% The extended format uses 20-bit indices and 12-bit lengths.
% There is no stereotaxic information in the file, - only voxel indices.
extendedFormat=0;
% roi_file='C:\temp\mt\ax.roi';
% Pin='C:\temp\mt\lax.img';
% DoWrite=1;
if ~exist('defaults')
%disp('loading defaults...');
spm_defaults
global defaults
end;
[a,b,c]=fileparts(Pin);
Vin=spm_vol(Pin);% comparison volume
[hdr,otherendian] = spm_read_hdr([a,filesep,b,'.hdr']); % this ignores possible spm-like .mat-file-transformations!
disp(['comparison volume has dimension: ',num2str(hdr.dime.dim(2:4))]);
roi_array=zeros(hdr.dime.dim(2:4)); % create empty array with image dimensions
% read ROI:
file=fopen(roi_file);
roi=uint16(fread(file,'ushort','l'));
fclose(file);
n=0;
% go through array
act_pos=1;
act_end=2;
act_start=1;
while act_end < size(roi,1)
sliceIndex=roi(act_start); % get slice number
if sliceIndex-((256*256)/2)>0 % substract 32768, is >0 if this is the extended roi format!
extendedFormat=1;
sliceIndex=sliceIndex-(256*256)/2;
end;
tmp_slice=roi_array(:,:,sliceIndex); % something to put the voxels in...
WordsInSlice=roi(act_start+1); % get reading length
act_end=act_start+WordsInSlice-1; % set stopper
n=n+1;
tmp=roi(act_start+2:act_end); % the stripe offset (1,:) and run stripe length (2,:)
clear('Indexes');
clear('longIndexes');
Indexes(1,:)=tmp(1:2:end);
Indexes(2,:)=tmp(2:2:end);
act_start=act_start+WordsInSlice; % set new start point
if extendedFormat
% 20bit/12 bit coding, 4 bit of stripe length are mult. by 65536
% and added to stripe offset
add=double(bitshift(Indexes(2,:),-12)).*(256*256); % shift right 12 bit, mult. 65536
subs=double(bitshift(Indexes(2,:),-12)).*(256*16); % same, but mult. by 4096
oldInd=Indexes;
longIndexes(1,:)=double(Indexes(1,:))+add; % add to stripe offset
longIndexes(2,:)=double(Indexes(2,:))-subs; % substract offset * 4096
Indexes=longIndexes;
end;
for i=1:size(Indexes,2)
tmp_slice([Indexes(1,i):Indexes(1,i)-1+Indexes(2,i)])=1; % set ROi voxels to 1 ...
end;
roi_array(:,:,sliceIndex)=double(tmp_slice); % ... and put them in the volume.
end;
% end array read loop.
if extendedFormat
disp('roi format is: ''extended'''); % this has been set somewhere in the loop
else
disp('roi format is: ''standard'''); % ... or not.
end;
if DoWrite
Vout = Vin;
[a,b,c]=fileparts(roi_file);
Vout.fname = [a,filesep,b,'_roi.img'];
Vout.descrip=[roi_file,', template: ',Pin];
spm_write_vol(Vout,roi_array);
disp([Vout.fname,', contained ',num2str(n),' slices: written']); % say good bye :-)
else
disp([roi_file,', contained ',num2str(n),' slices, ready!']); % say good bye :-)
end;
mask=roi_array; % output of function

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!