Home > rest_20090422 > rest_SphereROI.m

rest_SphereROI

PURPOSE ^

Define a ROI ball %Dawnwei.Song @ gmail.com % 20070830

SYNOPSIS ^

function varargout = rest_SphereROI(AOperation, varargin)

DESCRIPTION ^

Define a ROI ball %Dawnwei.Song @ gmail.com % 20070830
------------------------------------------------------------------------------------------------------------------------------
    Copyright(c) 2007~2010
    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
    Written by Xiao-Wei Song 
    http://resting-fmri.sourceforge.net
     Mail to Authors:  <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a> 
    Version=1.2;
    Release=20080626;
   Revised by YAN Chao-Gan 080610: NIFTI compatible
------------------------------------------------------------------------------------------------------------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

rest_SphereROI.m

SOURCE CODE ^

0001 function varargout = rest_SphereROI(AOperation, varargin)
0002 %Define a ROI ball %Dawnwei.Song @ gmail.com % 20070830
0003 %------------------------------------------------------------------------------------------------------------------------------
0004 %    Copyright(c) 2007~2010
0005 %    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
0006 %    Written by Xiao-Wei Song
0007 %    http://resting-fmri.sourceforge.net
0008 %     Mail to Authors:  <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a>
0009 %    Version=1.2;
0010 %    Release=20080626;
0011 %   Revised by YAN Chao-Gan 080610: NIFTI compatible
0012 %------------------------------------------------------------------------------------------------------------------------------
0013   
0014 if nargin<1, AOperation='Init'; end    %Revise the Start
0015 switch upper(AOperation),
0016 case 'INIT',        %Init
0017     AROICenter=[0 0 0]; AROIRadius=0;
0018     if nargin>0,
0019         if ischar(varargin{1})
0020             [AROICenter, AROIRadius] =rest_SphereROI('Str2ROIBall', varargin{1});
0021         elseif nargin==2,
0022             AROICenter=varargin{1}; 
0023             AROIRadius=varargin{2};         
0024         else
0025             error('False Input');
0026         end    
0027     end 
0028     theConfig =InitControls(AROICenter, AROIRadius);
0029     %setappdata(theConfig.hFig, 'Config', theConfig);
0030     
0031     uiwait(theConfig.hFig);
0032     theX =str2num(get(theConfig.hEditPositionX, 'String'));        
0033     theY =str2num(get(theConfig.hEditPositionY, 'String'));        
0034     theZ =str2num(get(theConfig.hEditPositionZ, 'String'));        
0035     AROICenter =[theX, theY, theZ];
0036     if get(theConfig.hTal2Mni, 'Value'),
0037         AROICenter =round(rest_tal2mni([theX,theY,theZ]));
0038     end
0039     AROIRadius=str2num(get(theConfig.hEditRadius, 'String'));
0040     varargout{1} =rest_SphereROI('ROIBall2Str', AROICenter, AROIRadius);
0041     
0042     delete(theConfig.hFig);
0043     
0044 case 'SETANDQUIT',        %SetAndQuit
0045     theFig =findobj(allchild(0),'flat','Tag','figSetROI');
0046     if ~isempty(theFig) && rest_misc( 'ForceCheckExistFigure' , theFig),
0047         %theConfig =getappdata(theFig, 'Config');
0048         uiresume(theFig);    
0049     end
0050 case 'ROIBALL2STR',            %ROIBall2Str
0051     if nargin~=3, error('Usage: result =rest_SphereROI( ''ROIBall2Str'' , AROICenter, AROIRadius);'); end
0052 
0053     AROICenter=varargin{1}; 
0054     AROIRadius=varargin{2};
0055     varargout{1} =sprintf('ROI Center(mm)=(%d, %d, %d); Radius=%.2f mm.', ...
0056                         AROICenter(1), AROICenter(2), AROICenter(3), ...
0057                         AROIRadius);
0058     
0059 case 'STR2ROIBALL',         %Str2ROIBall
0060     if nargin~=2, error('Usage: result =rest_SphereROI( ''Str2ROIBall'' , ABallDefinition);'); end
0061     
0062     ABallDefinition =varargin{1};
0063     if rest_SphereROI( 'IsBallDefinition', ABallDefinition),
0064         [posBegin, posEnd] =regexp(ABallDefinition, '=\(.*\)');
0065         AROICenter = str2num(ABallDefinition((posBegin+2):(posEnd-1)));
0066         varargout{1} =AROICenter;
0067         ABallDefinition =ABallDefinition(posEnd+1:end);
0068         [posBegin, posEnd] =regexp(ABallDefinition, '=.*mm');
0069         AROIRadius = str2num(ABallDefinition((posBegin+1):(posEnd-2)));
0070         varargout{2} =AROIRadius;
0071     else
0072         varargout{1} =[0 0 0];
0073         varargout{2} =0;
0074     end
0075     
0076     
0077     
0078 case 'ISBALLDEFINITION',        %IsBallDefinition
0079     if nargin~=2, error('Usage: result =rest_SphereROI( ''IsBallDefinition'' , ABallDefinition);'); end
0080     ABallDefinition =varargin{1};
0081     if isempty(ABallDefinition), varargout{1} =0; return; end
0082     
0083     [posBegin, posEnd] =regexp(ABallDefinition, '.*ROI\ Center\(mm\)=\(.*\);\ Radius=.*mm\..*');
0084     if (~isempty(posBegin) && ~isempty(posEnd)) && (posBegin>=1) && (posEnd<=length(ABallDefinition)),
0085         varargout{1} =1;
0086     else    
0087         varargout{1} =0;
0088     end
0089     
0090 case 'BALLDEFINITION2MASK'        %BallDefinition2Mask
0091     if nargin~=5, error('Usage: mask =rest_SphereROI( ''BallDefinition2Mask'' , ABallDefinition, ABrainSize, AVoxelSize, Header);'); end
0092     ABallDefinition =varargin{1};
0093     if isempty(ABallDefinition), varargout{1} =0; error('No Ball definition! Please Check!'); end    
0094     ABrainSize =varargin{2};
0095     AVoxelSize =varargin{3};
0096     Header =varargin{4};
0097 
0098     [AROICenter, AROIRadius] =rest_SphereROI('STR2ROIBALL', ABallDefinition);
0099     
0100     %Revise Left/Right, I think Left Img is Right brain and Left Img is +/Right brain is +
0101     AROICenter(1) =AROICenter(1) *(-1);
0102     
0103     %Prepare
0104     AROICenter=reshape(AROICenter, 1,length(AROICenter));
0105     Header.Origin=reshape(Header.Origin, 1,length(Header.Origin));
0106     AVoxelSize=reshape(AVoxelSize, 1,length(AVoxelSize));    
0107     
0108     theMask =Ball2Mask(ABrainSize, AVoxelSize, AROICenter, AROIRadius, Header.Origin);
0109     fprintf('\n\n\t\tSeed ROI Definition: %s\n\t\t\tBrain Size: (%s),\t\tVoxel Size: (%s),\t\tOrigin: (%s)\n\t\t\tContained Voxel count: %d\n\n', ABallDefinition, num2str(ABrainSize),num2str(AVoxelSize), num2str(Header.Origin), length(find(theMask)));
0110     
0111     varargout{1} =theMask;
0112     rest_writefile(theMask, ...
0113             fullfile(rest_misc('WhereIsREST'),'LastSphereMask'), ...
0114             ABrainSize,AVoxelSize, Header,'int16');
0115 
0116 otherwise    
0117  end
0118  
0119 function Result =InitControls(AROICenter, AROIRadius)
0120     theFig =figure('Units', 'pixel', 'Toolbar', 'none', 'MenuBar', 'none', ...
0121                     'Tag', 'figSetROI', 'WindowStyle', 'modal', ...
0122                     'CloseRequestFcn', 'rest_SphereROI(''SetAndQuit'');', ...
0123                     'NumberTitle', 'off', 'Visible', 'off', ... 
0124                     'Name', sprintf('Seed ROI Definition'), ...
0125                     'Position', [0,0,250, 150], 'Resize','off');
0126     movegui(theFig, 'center'); 
0127     
0128     OffsetX =0; MarginX=10; OffsetY=0; MarginY=25;
0129     theEditCallbackFcn =sprintf('');
0130     theLeft =OffsetX+MarginX; theBottom =OffsetY+MarginY+35+MarginY/2;
0131     uicontrol(theFig, 'Style','text', 'Units','pixels', ...
0132               'String', 'X(mm)', ...
0133               'BackgroundColor', get(theFig,'Color'), ...              
0134               'Position',[theLeft, theBottom, 46,15]);
0135     theLeft =OffsetX+MarginX; theBottom =OffsetY+MarginY+20;          
0136     hEditPositionX =uicontrol(theFig, 'Style','edit', 'Units','pixels', ...
0137                               'String', int2str(AROICenter(1)), ...
0138                               'BackgroundColor', 'white', ...
0139                               'Callback', theEditCallbackFcn, ...
0140                               'Position',[theLeft, theBottom, 46,20]);
0141                               
0142     theLeft =OffsetX+MarginX+30+MarginX; theBottom =OffsetY+MarginY+35+MarginY/2;                      
0143     uicontrol(theFig, 'Style','text', 'Units','pixels', ...
0144               'String', 'Y(mm)', ...
0145               'BackgroundColor', get(theFig,'Color'), ...
0146               'Position',[theLeft+8, theBottom, 46,15]);
0147     theLeft =OffsetX+MarginX+30+MarginX; theBottom =OffsetY+MarginY+20;              
0148     hEditPositionY =uicontrol(theFig, 'Style','edit', 'Units','pixels', ...
0149                               'String', int2str(AROICenter(2)), ...
0150                               'BackgroundColor', 'white', ...
0151                               'Callback', theEditCallbackFcn, ...
0152                               'Position',[theLeft+8, theBottom, 46,20]);
0153             
0154     theLeft =OffsetX+MarginX+30+MarginX+30+MarginX; theBottom =OffsetY+MarginY+35+MarginY/2;        
0155     uicontrol(theFig, 'Style','text', 'Units','pixels', ...
0156               'String', 'Z(mm)', ...
0157               'BackgroundColor', get(theFig,'Color'), ...
0158               'Position',[theLeft+16, theBottom, 46,15]);
0159               
0160     theLeft =OffsetX+MarginX+30+MarginX+30+MarginX; theBottom =OffsetY+MarginY+20;          
0161     hEditPositionZ =uicontrol(theFig, 'Style','edit', 'Units','pixels', ...
0162                               'String', int2str(AROICenter(3)), ...
0163                               'BackgroundColor', 'white', ...
0164                               'Callback', theEditCallbackFcn, ...
0165                               'Position',[theLeft+16, theBottom, 46,20]);
0166                               
0167     theLeft =OffsetX+MarginX; 
0168     hTal2Mni =uicontrol(theFig, 'Style','checkbox ', 'Units','pixels', ...
0169                         'String', 'From Talairach to MNI', ...
0170                         'Value', 0, 'Visible', 'off',...
0171                         'BackgroundColor', get(theFig,'Color'), ...
0172                         'Enable', 'on', ...                        
0173                         'Position',[theLeft, 15, 180,20]);
0174         
0175     theLeft =OffsetX+MarginX+30+MarginX+30+MarginX+30+MarginX; theBottom =OffsetY+MarginY+35+MarginY/2;
0176     uicontrol(theFig, 'Style','text', 'Units','pixels', ...
0177               'String', 'Radius(mm)', ...
0178               'BackgroundColor', get(theFig,'Color'), ...
0179               'Position',[theLeft+32, theBottom, 60,15]);
0180     theLeft =OffsetX+MarginX+30+MarginX+30+MarginX+30+MarginX; theBottom =OffsetY+MarginY+20;          
0181     hEditRadius =uicontrol(theFig, 'Style','edit', 'Units','pixels', ...
0182                               'String', int2str(AROIRadius), ...
0183                               'BackgroundColor', 'yellow', ...
0184                               'Callback', theEditCallbackFcn, ...
0185                               'Position',[theLeft+32, theBottom, 60,20]);
0186     
0187 
0188     
0189     hbtnClose=uicontrol(theFig,'Style', 'pushbutton', 'Units', 'pixels', ...
0190                         'Position', [195 10 50 25], ...
0191                         'FontSize', 10, ...
0192                         'String', 'Ok', ...
0193                         'Callback', 'rest_SphereROI(''SetAndQuit'');');
0194     
0195     %Attention!!!    20071122
0196     theLeft =OffsetX+MarginX; theBottom =OffsetY+MarginY+35+MarginY/2;
0197     uicontrol(theFig, 'Style','text', 'Units','pixels', ...
0198               'String', sprintf('Attention:\nPositive X means left and negative X means right in SliceViewer''s image!!!'), ...
0199               'HorizontalAlignment', 'left', ...
0200               'BackgroundColor', get(theFig,'Color'), ...              
0201               'ForegroundColor', 'red', ...
0202               'FontWeight', 'bold', ... %              'Callback', 'rest_misc( ''Attention_Coordinates'');', ...    %Not work!!!
0203               'Position',[theLeft, theBottom+25, 230,15*3]);
0204 
0205     
0206     %Save to config
0207     theConfig.hFig            =theFig;            %handle of the config
0208     theConfig.hEditPositionX =hEditPositionX;
0209     theConfig.hEditPositionY =hEditPositionY;
0210     theConfig.hEditPositionZ =hEditPositionZ;
0211     theConfig.hEditRadius     =hEditRadius;
0212     theConfig.hTal2Mni         =hTal2Mni;
0213         
0214     Result =theConfig;
0215     set(theFig, 'Visible', 'on');
0216     
0217 function Result =Ball2Mask(ABrainSize, AVoxelSize, AROICenter, AROIRadius, AOrigin);
0218     mask =zeros(ABrainSize); %ABrainSize, such as [61, 73, 61]
0219     
0220     %Prepare
0221     % AROICenter=reshape(AROICenter, length(AROICenter),1);
0222     % AOrigin=reshape(AOrigin, length(AOrigin),1);
0223     % AVoxelSize=reshape(AVoxelSize, length(AVoxelSize),1);
0224     
0225     AROICenter =round(AROICenter./AVoxelSize) +AOrigin;%Revised by dawnsong, 20070904
0226     
0227     radiusX =round(AROIRadius /AVoxelSize(1));
0228     if (AROICenter(1)-radiusX)>=1 && (AROICenter(1)+radiusX)<=ABrainSize(1)
0229         rangeX    =(AROICenter(1)-radiusX):(AROICenter(1)+radiusX);
0230     elseif (AROICenter(1)-radiusX)<1 && (AROICenter(1)+radiusX)<=ABrainSize(1)
0231         rangeX    =1:(AROICenter(1)+radiusX);
0232     elseif (AROICenter(1)-radiusX)>=1 && (AROICenter(1)+radiusX)>ABrainSize(1)
0233         rangeX    =(AROICenter(1)-radiusX):ABrainSize(1);
0234     else
0235         rangeX =1:ABrainSize(1);
0236     end
0237     
0238     radiusY =round(AROIRadius /AVoxelSize(2));
0239     if (AROICenter(2)-radiusY)>=1 && (AROICenter(2)+radiusY)<=ABrainSize(2)
0240         rangeY    =(AROICenter(2)-radiusY):(AROICenter(2)+radiusY);
0241     elseif (AROICenter(2)-radiusY)<1 && (AROICenter(2)+radiusY)<=ABrainSize(2)
0242         rangeY    =1:(AROICenter(2)+radiusY);
0243     elseif (AROICenter(2)-radiusY)>=1 && (AROICenter(2)+radiusY)>ABrainSize(2)
0244         rangeY    =(AROICenter(2)-radiusY):ABrainSize(2);
0245     else
0246         rangeY =1:ABrainSize(2);
0247     end
0248     
0249     radiusZ =round(AROIRadius /AVoxelSize(3));
0250     if (AROICenter(3)-radiusZ)>=1 && (AROICenter(3)+radiusZ)<=ABrainSize(3)
0251         rangeZ    =(AROICenter(3)-radiusZ):(AROICenter(3)+radiusZ);
0252     elseif (AROICenter(3)-radiusZ)<1 && (AROICenter(3)+radiusZ)<=ABrainSize(3)
0253         rangeZ    =1:(AROICenter(3)+radiusZ);
0254     elseif (AROICenter(3)-radiusZ)>=1 && (AROICenter(3)+radiusZ)>ABrainSize(3)
0255         rangeZ    =(AROICenter(3)-radiusZ):ABrainSize(3);
0256     else
0257         rangeZ =1:ABrainSize(3);
0258     end
0259     
0260     for x=rangeX, for y=rangeY, for z=rangeZ,
0261         %Ball Definition, Computing within a cubic to minimize the time to be consumed
0262         if norm(([x, y, z] -AROICenter).*AVoxelSize)<=AROIRadius,
0263             mask(x, y, z) =1;
0264         end
0265     end; end; end;
0266     
0267     Result =mask;    
0268     
0269 
0270

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005