0001 function [V, Vflag] = vb_flood_fill_2d(V,start,fillval,level)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 if nargin<3, fillval=1; end;
0027 if nargin<4, level=1; end;
0028 if fillval < level,
0029 error('level value is larger than fill value\n')
0030 end
0031
0032 [NX,NY] = size(V);
0033
0034 NN = NX*NY;
0035 D = 2;
0036
0037
0038 Vflag = zeros(NX,NY);
0039 Plist = zeros(NN,D);
0040 Vlist = zeros(NN,D);
0041 Nroot = size(start,1);
0042
0043 Nlist = 0;
0044
0045
0046 for n=1:Nroot,
0047
0048 root = start(n,:);
0049
0050 if root(1)<1 | root(1)>NX | root(2)<1 | root(2)>NY,
0051 continue;
0052 end
0053
0054 if V(root(1),root(2)) < level,
0055 V(root(1),root(2)) = fillval;
0056 Vflag(root(1),root(2)) = 1;
0057 Nlist = Nlist + 1;
0058 Vlist(Nlist,:) = root ;
0059 end;
0060 end
0061
0062
0063 Nroot = Nlist;
0064 Plist(1:Nroot,:) = Vlist(1:Nroot,:);
0065
0066
0067 while Nroot > 0,
0068
0069 Nlist = 0;
0070
0071
0072 for n=1:Nroot,
0073
0074 root = Plist(n,:);
0075
0076
0077 next = [root(1)+1, root(2)];
0078
0079 if root(1) < NX ...
0080 & Vflag(next(1),next(2))==0 ...
0081 & V(next(1),next(2)) < level,
0082
0083 V(next(1),next(2)) = fillval;
0084 Vflag(next(1),next(2)) = 1;
0085 Nlist = Nlist + 1;
0086 Vlist(Nlist,:) = next ;
0087 end;
0088
0089
0090 next = [root(1)-1, root(2)];
0091
0092 if root(1) > 1 ...
0093 & Vflag(next(1),next(2))==0 ...
0094 & V(next(1),next(2)) < level,
0095
0096 V(next(1),next(2)) = fillval;
0097 Vflag(next(1),next(2)) = 1;
0098 Nlist = Nlist + 1;
0099 Vlist(Nlist,:) = next ;
0100 end;
0101
0102
0103 next = [root(1), root(2) + 1];
0104
0105 if root(2) < NY ...
0106 & Vflag(next(1),next(2))==0 ...
0107 & V(next(1),next(2)) < level,
0108
0109 V(next(1),next(2)) = fillval;
0110 Vflag(next(1),next(2)) = 1;
0111 Nlist = Nlist + 1;
0112 Vlist(Nlist,:) = next ;
0113 end;
0114
0115
0116 next = [root(1), root(2) - 1];
0117
0118 if root(2) > 1 ...
0119 & Vflag(next(1),next(2))==0 ...
0120 & V(next(1),next(2)) < level,
0121
0122 V(next(1),next(2)) = fillval;
0123 Vflag(next(1),next(2)) = 1;
0124 Nlist = Nlist + 1;
0125 Vlist(Nlist,:) = next ;
0126 end;
0127
0128 end;
0129
0130
0131
0132 Nroot = Nlist;
0133 Plist(1:Nroot,:) = Vlist(1:Nroot,:);
0134
0135 end
0136