0001 function Bout = vb_dilation_3d(B, R)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 if vb_matlab_version >= 7
0024 Bout = vb_dilation_3d_int(B, R);
0025 return;
0026 end
0027
0028 [N1,N2,N3] = size(B);
0029
0030 Bout = B;
0031
0032
0033 Bo = zeros(N1,N2,N3);
0034 iz = find( B == 0 );
0035 Bo(iz) = 1;
0036
0037
0038
0039 j1d = 1:(N1-1);
0040 j1u = 2:N1;
0041
0042 j2d = 1:(N2-1);
0043 j2u = 2:N2;
0044
0045
0046 Rmax = ceil(R);
0047 x = -Rmax:Rmax;
0048
0049 [x1, x2, x3 ] = ndgrid(x);
0050
0051 jx = find( (x1.^2 + x2.^2 +x3.^2 ) <= R^2 );
0052
0053 x1 = x1(jx);
0054 x2 = x2(jx);
0055 x3 = x3(jx);
0056
0057 NN = length(jx);
0058 N1N2 = N1*N2;
0059
0060 Bx = zeros(N1,N2);
0061
0062 for zz = 1:N3
0063
0064 Bx = zeros(N1,N2);
0065
0066
0067
0068 Bx(j1d, : ) = Bx(j1d, : ) + Bo(j1u, : , zz );
0069 Bx(j1u, : ) = Bx(j1u, : ) + Bo(j1d, : , zz );
0070 Bx( : ,j2d) = Bx( : ,j2d) + Bo( : ,j2u, zz );
0071 Bx( : ,j2u) = Bx( : ,j2u) + Bo( : ,j2d, zz );
0072
0073 if zz > 1,
0074 Bx = Bx + Bo(:,:,zz-1);
0075 end
0076 if zz < N3,
0077 Bx = Bx + Bo(:,:,zz+1);
0078 end
0079
0080
0081 Bx = Bx.*B(:,:,zz);
0082
0083
0084 iz = find( Bx > 0 );
0085 Nz = length(iz);
0086
0087
0088
0089 j3 = zz;
0090 j2 = floor((iz-1)/N1)+1;
0091 j1 = rem((iz-1),N1)+1;
0092
0093
0094 k1 = zeros(Nz,1);
0095 k2 = zeros(Nz,1);
0096
0097 for n=1:NN,
0098
0099 k1 = j1 + x1(n);
0100 k2 = j2 + x2(n);
0101 k3 = j3 + x3(n);
0102
0103
0104 k1 = max(k1,1);
0105 k2 = max(k2,1);
0106 k3 = max(k3,1);
0107
0108 k1 = min(k1,N1);
0109 k2 = min(k2,N2);
0110 k3 = min(k3,N3);
0111
0112
0113
0114 inx = k1+ N1*(k2 - 1)+ N1N2*(k3 - 1);
0115
0116 Bout(inx) = 1;
0117 end;
0118 end