Hi Nathan
Have a look at the code below - it is a simplified version of what I use
for my batch processing. I have tested it once and worked OK. Save the code
as segmask.m and run it - make sure spm is started.
Let me know if it works or not.
Cheers,
Paul
On Mon, 18 Nov 2002 16:53:19 +0000, Nathan White <[log in to unmask]> wrote:
>Just wondering if someone has written a quick batch routine for cleaning
>segmented images. I've been trying to put togeather a script that (1) uses
>spm_get to select the image files to work on, (2) calls spm_xbrain to
>create the brain_xxx.img, and (3) calls spm_imcalc_ui to perform the
>nessesary operations on the images. I can imagine the routine inputing the
>three segmented files (i.e. xxx.seg1.img, xxx.seg2.img, and xxx.seg3.img)
>and outputing cleaned gray, white, and csf images.
>
>Any suggestions on how to do this?
>
>Thanks a bunch...
>
>Nathan White
function segmask( spgr_normalized_img )
% SEGMASK - Segment and mask normalized image
% SEGMASK ( FNAME) - Optional FNAME is filename of image to segement and
% mask (full path); if not included, user is asked to select a
% file.
% Segmented and masked images are written to the same directory; masked
% images are prefixed with "x."
% The segmentation parameters are set in the "spm_segment" line; ['c' 'w']
% performs a little inhomogeneity correction, and writes the corrected
% images.
% Please contact Paul Macey <[log in to unmask]> for comments or questions.
% Monday, 18 November 2002
if nargin == 0
[f,p] = uigetfile('*.img','Select normalized anatomical image');
if f == 0, return, end
spgr_normalized_img = [p,f];
end
[this_dir,nm,xt] = fileparts(spgr_normalized_img);
% Move to this subject's directory
cur_dir = pwd;
cd(this_dir)
% Overwrite flag
OVERWRITE = 0;
% 1: Segmentation
gray = [this_dir,'\',nm,'_seg1',xt];
white = [this_dir,'\',nm,'_seg2',xt];
csf = [this_dir,'\',nm,'_seg3',xt];
if OVERWRITE | ~(exist(gray,'file') & exist(white,'file') & exist
(csf,'file') )
spm_segment( spgr_normalized_img,[],['c' 'w']) % Writes corrected
image.
end
brain = [this_dir,'\brain_',nm,xt];
if OVERWRITE | ~exist(brain,'file')
% Get gray and white matter files.
Dg = dir([this_dir,'\nE*_seg1.img']);
Dw = dir([this_dir,'\nE*_seg2.img']);
if length(Dg) ~= 1 | length(Dw) ~= 1
disp(['Cannot find unique normalized gray matter file
in "',this_dir,'," cannot extract.'])
else
clear P
P(1,:) = [this_dir,'\',Dg(1).name];
P(2,:) = [this_dir,'\',Dw(1).name];
% Clear as much as possible to minimize memory use.
mode = 1; % 1 brain only; 5 => brain and surface
pack
spm_xbrain(P,mode)
end
end
% masking of segmented images
% Do not mask CSF!!!
for m = 1:3
P_mask = [];
P_mask{1} = brain;
switch m
case 1
thisfile = gray;
case 2
thisfile = white;
end
P_mask{2} = thisfile;
P_mask = char(P_mask');
[this_dir,thisnm,thisxt] = fileparts(thisfile);
% Record full file names of masked images
switch m
case 1
graymask = [this_dir,'\x',thisnm,thisxt];
case 2
whitemask = [this_dir,'\x',thisnm,thisxt];
end
outputimg = ['x',thisnm,thisxt];
if OVERWRITE | ~exist(outputimg,'file')
spm_imcalc_ui(P_mask,outputimg,'i2.*i1');
end
end
% return to original directory
cd(cur_dir)
Paul Macey
Deparment of Neurobiology
David Geffen School of Medicine at UCLA
10833 Le Conte Ave
Los Angeles, CA 90095-1763
email: [log in to unmask]
phone: +1-310-206-1679
fax: +1-310-825-2224
|