0001 function varargout = rest_SphereROI(AOperation, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 if nargin<1, AOperation='Init'; end
0015 switch upper(AOperation),
0016 case '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
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',
0045 theFig =findobj(allchild(0),'flat','Tag','figSetROI');
0046 if ~isempty(theFig) && rest_misc( 'ForceCheckExistFigure' , theFig),
0047
0048 uiresume(theFig);
0049 end
0050 case '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',
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',
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'
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
0101 AROICenter(1) =AROICenter(1) *(-1);
0102
0103
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
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', ...
0203 'Position',[theLeft, theBottom+25, 230,15*3]);
0204
0205
0206
0207 theConfig.hFig =theFig;
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);
0219
0220
0221
0222
0223
0224
0225 AROICenter =round(AROICenter./AVoxelSize) +AOrigin;
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
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