source: aedes_roifill.m

Last change on this file was 168, checked in by tjniskan, 6 years ago
  • Fixed a bug in filling voxel ROIs. In some situations trying to fill a ROI from a seed voxel that

already belonged to the ROI removed the ROI completely from that slice. The Undo buffer was also
unnecessarily updated.

M aedes.m
M aedes_revision.m
M aedes_roifill.m

File size: 2.4 KB
Line 
1function bw_out = aedes_roifill(bw,seed)
2% AEDES_ROIFILL - Do flood fill operation (4-conn) for binary image
3%   
4%
5% Synopsis:
6%        bw_out = aedes_roifill(bw_in,seed);
7%
8% Description:
9%       Flood fill operation (4-connected) for binary 2D images.
10%
11% Examples:
12%
13% See also:
14%       AEDES
15
16% This function is a part of Aedes - A graphical tool for analyzing
17% medical images
18%
19% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
20%
21% Department of Physics, Department of Neurobiology
22% University of Kuopio, FINLAND
23%
24% This program may be used under the terms of the GNU General Public
25% License version 2.0 as published by the Free Software Foundation
26% and appearing in the file LICENSE.TXT included in the packaging of
27% this program.
28%
29% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
30% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31
32% Flood fill algorithm to be used
33method = 1;
34
35% Look for a quick return
36if bw(seed(1),seed(2))
37        % Seed pixel is 1, no need to do anything...
38        bw_out = bw;
39        return
40end
41
42% matrix of linear indices
43sz = size(bw);
44bw = ~bw; % Image complement
45lind = reshape(1:prod(sz),sz(1),sz(2));
46
47% Matrix of row and column indices
48[xind,yind] = ndgrid(1:sz(1),1:sz(2));
49
50switch method
51  case 1
52    % This is the simplest and slowest flood fill method...
53    Q = [];
54    bw_out = false(size(bw));
55    if ~bw(lind(seed(1),seed(2)))
56      return
57    end
58    Q(end+1) = lind(seed(1),seed(2));
59    while ~isempty(Q)
60      n = Q(1);
61      ni = [xind(n) yind(n)];
62      if bw(n)
63        bw_out(n) = true;
64      end
65      Q=Q(2:end);
66      %Q(1)=[];
67     
68      % Go west
69      if ni(2)~=1
70        nn = lind(ni(1),ni(2)-1);
71        if bw(nn) && ~bw_out(nn)
72          bw_out(nn) = true;
73          Q(end+1) = nn;
74        end
75      end
76     
77       % Go east
78      if ni(2)~=sz(2)
79        nn = lind(ni(1),ni(2)+1);
80        if bw(nn) && ~bw_out(nn)
81          bw_out(nn) = true;
82          Q(end+1) = nn;
83        end
84      end
85     
86       % Go south
87      if ni(1)~=sz(1)
88        nn = lind(ni(1)+1,ni(2));
89        if bw(nn) && ~bw_out(nn)
90          bw_out(nn) = true;
91          Q(end+1) = nn;
92        end
93      end
94     
95       % Go north
96      if ni(1)~=1
97        nn = lind(ni(1)-1,ni(2));
98        if bw(nn) && ~bw_out(nn)
99          bw_out(nn) = true;
100          Q(end+1) = nn;
101        end
102      end
103     
104     
105    end
106   
107  case 2
108    % To be written...
109   
110end
111
112bw_out = ~bw | bw_out;
Note: See TracBrowser for help on using the repository browser.

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