source: plugins/brain_masking.m

Last change on this file was 137, checked in by tjniskan, 9 years ago
  • Fixed: Aedes didn't detect Matlab version correctly after rotating

images

  • Fixed: brain masking plugin now provides file information to the new

Aedes window and saved ROI-files.

M aedes.m
M plugins/brain_masking.m
M aedes_revision.m

File size: 4.2 KB
Line 
1function brain_masking(DATA,ROI,AddInfo)
2% BRAIN_MASKING - Try to mask the (Rat) brain
3%   (this is an Aedes plugin)
4%
5% Synopsis:
6%
7% Description:
8%
9% Examples:
10%
11% See also:
12%
13
14% This function is a part of Aedes - A graphical tool for analyzing
15% medical images
16%
17% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
18%
19% Department of Physics, Department of Neurobiology
20% University of Kuopio, FINLAND
21%
22% This program may be used under the terms of the GNU General Public
23% License version 2.0 as published by the Free Software Foundation
24% and appearing in the file LICENSE.TXT included in the packaging of
25% this program.
26%
27% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29
30
31% Prompt for intensity threshold and erode/dilate size
32answer = inputdlg({'Intensity threshold (range: 0 - 1)',...
33  'Erode/dilate radius (in pixels)'},...
34  'Parameters for masking',1,...
35  {'0.35','5'});
36if isempty(answer)
37  % Canceled
38  return
39end
40intensity_th=str2num(answer{1});
41if isempty(intensity_th)
42  autoThreshold = true;
43else
44  autoThreshold = false;
45end
46ErodeDilateSize=str2num(answer{2});
47
48% Mask for morphological operations
49se=strel('disk',ErodeDilateSize);
50se2 = strel('disk',ErodeDilateSize);
51
52% Initialize ROI
53ROI=[];
54
55
56for kk=1:length(DATA)
57  %L = zeros(size(DATA{kk}.FTDATA(:,:,:,AddInfo.CurrentVol)));
58  ROI(1).voxels{kk}=false(size(DATA{kk}.FTDATA(:,:,:,AddInfo.CurrentVol)));
59  ROI(1).fpath = {''};
60  ROI(1).fname = {''};
61  ROI(1).label = 'brain_mask';
62  ROI(1).color = [255 0 0];
63 
64  % Calculate index vector for center of mass
65  data=double(DATA{kk}.FTDATA(:,:,1,AddInfo.CurrentVol));
66  n1=1:size(data,1);
67  n2=1:size(data,2);
68  N2 = repmat(n2,size(data,1),1);
69  N2 = N2(:);
70  N1 = n1.';
71  N1 = repmat(N1,size(data,2),1);
72  C = [N1 N2];
73 
74  % Number of slices
75  nSlices = size(DATA{kk}.FTDATA,3);
76 
77  %Initiate waitbar
78  wbh=aedes_wbar(1/nSlices,sprintf('Estimating mask. Processing slice 1/%d',nSlices));
79 
80 
81  % Work through the 3rd direction
82  for ii=1:nSlices
83   
84    % Update waitbar
85    aedes_wbar(ii/nSlices,wbh,sprintf('Estimating mask. Processing slice %d/%d',ii,nSlices))
86   
87    data_raw=double(DATA{kk}.FTDATA(:,:,ii,AddInfo.CurrentVol));
88    data = data_raw;
89   
90    % Scale data
91    data=data./max(data(:));
92   
93    % Saturate 1% of high pixel values 
94    clim=stretchlim(data,[0 0.99]);
95    data(data>clim(2))=clim(2);
96    data=data./clim(2);
97   
98    % Median filter data to remove random noise
99    data = medfilt2(data,[3 3]);
100   
101    % Open image 
102    data = imerode(data,se);
103    data = imdilate(data,se);
104    data_ed = data;
105   
106    % Erode image for obtaining better separation for different structures
107    data = imerode(data,se);
108     
109    % Scale data again
110    data=data./max(data(:));
111    data_final_erode(:,:,ii) = data;
112   
113    % Do automatic threshold estimation if requested
114    if autoThreshold
115      [intensity_th,em] = graythresh(data);
116    end
117     
118    % Threshold image and make binary
119    data(data<intensity_th)=0;
120    data(data>=intensity_th)=1;
121    data=logical(data);
122   
123    % Fill holes and remove clusters with low number of pixels
124    data=bwfill(data,'holes');
125    data=bwareaopen(data,20,4);
126   
127    % Check that we still have something left...
128    if ~any(data(:)>0)
129      continue
130    end
131   
132    % Label separate regions in the image
133    [L,n]=bwlabel(data,4);
134    if n>1
135      % Select the region whose center of mass that is closest to the
136      % image center of mass
137      tmp=regionprops(L,'Centroid');
138      COM_regions=round(reshape([tmp(:).Centroid],2,[]).');
139      COM_data = round(data_ed(:).'*C/sum(data_ed(:),'double'));
140      COM_data = repmat(COM_data,size(COM_regions,1),1);
141      d=sqrt(sum((COM_regions-COM_data).^2,2));
142      [mn,ind]=min(d);
143      L(L~=ind)=0;
144      data=L>0;
145    end
146   
147    % Account for the last erosion
148    data=imdilate(data,se2);
149   
150    % Assign mask to ROI
151    ROI(1).voxels{kk}(:,:,ii) = data;
152  end
153 
154  % Close waitbar
155  close(wbh);
156end
157
158%assignin('base','data_ed',data_final_erode)
159
160% Open data and mask in Aedes
161if length(DATA)>1
162  aedes(DATA,ROI);
163else
164  if size(DATA{1}.FTDATA,4)==1
165    aedes(DATA,ROI);
166  else
167    aedes(DATA{1}.FTDATA(:,:,:,AddInfo.CurrentVol),ROI);
168  end
169end
170
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008