This is a static copy of a profile reportHome
bwdist (808 calls, 33.697 sec)
Generated 05-Nov-2014 07:52:46 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/images/images/bwdist.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
120 | D = bwdistComputeEDT(BW); | 808 | 29.401 s | 87.3% |  |
121 | varargout{1} = sqrt(D); | 808 | 3.174 s | 9.4% |  |
95 | [BW,method] = parse_inputs(var... | 808 | 1.001 s | 3.0% |  |
108 | iptcheckinput(BW,{'logical'},{... | 808 | 0.050 s | 0.1% |  |
110 | if strcmp(method,'euclidean') | 808 | 0.020 s | 0.1% |  |
All other lines | | | 0.050 s | 0.1% |  |
Totals | | | 33.697 s | 100% | |
Children (called functions)
Code Analyzer results
No Code Analyzer messages.Coverage results
[ Show coverage for parent directory ]
Total lines in function | 149 |
Non-code lines (comments, blank lines) | 116 |
Code lines (lines that can run) | 33 |
Code lines that did run | 10 |
Code lines that did not run | 23 |
Coverage (did run/can run) | 30.30 % |
Function listing
time calls line
1 function varargout = bwdist(varargin)
2 %BWDIST Distance transform of binary image.
3 % D = BWDIST(BW) computes the Euclidean distance transform of the
4 % binary image BW. For each pixel in BW, the distance transform assigns
5 % a number that is the distance between that pixel and the nearest
6 % nonzero pixel of BW. BWDIST uses the Euclidean distance metric by
7 % default. BW can have any dimension. D is the same size as BW.
8 %
9 % [D,L] = BWDIST(BW) also computes the nearest-neighbor transform and
10 % returns it as a label matrix, L. L has the same size as BW and D.
11 % Each element of L contains the linear index of the nearest nonzero
12 % pixel of BW.
13 %
14 % [D,L] = BWDIST(BW,METHOD) lets you compute an alternate distance
15 % transform, depending on the value of METHOD. METHOD can be
16 % 'cityblock', 'chessboard', 'quasi-euclidean', or 'euclidean'. METHOD
17 % defaults to 'euclidean' if not specified. METHOD may be
18 % abbreviated.
19 %
20 % The different methods correspond to different distance metrics. In
21 % 2-D, the cityblock distance between (x1,y1) and (x2,y2) is abs(x1-x2)
22 % + abs(y1-y2). The chessboard distance is max(abs(x1-x2),
23 % abs(y1-y2)). The quasi-Euclidean distance is:
24 %
25 % abs(x1-x2) + (sqrt(2)-1)*abs(y1-y2), if abs(x1-x2) > abs(y1-y2)
26 % (sqrt(2)-1)*abs(x1-x2) + abs(y1-y2), otherwise
27 %
28 % The Euclidean distance is sqrt((x1-x2)^2 + (y1-y2)^2).
29 %
30 % Notes
31 % -----
32 % BWDIST uses a fast algorithm to compute the true Euclidean distance
33 % transform. The other methods are provided primarily for pedagogical
34 % reasons.
35 %
36 % The function BWDIST changed in version 6.4 (R2009b). Previous versions
37 % of the Image Processing Toolbox used different algorithms for
38 % computing the Euclidean distance transform and the associated label
39 % matrix. If you need the same results produced by the previous
40 % implementation, use the function BWDIST_OLD.
41 %
42 % Class support
43 % -------------
44 % BW can be numeric or logical, and it must be nonsparse. D and L are
45 % single matrices with the same size as BW.
46 %
47 % Examples
48 % --------
49 % Here is a simple example of the Euclidean distance transform:
50 %
51 % bw = zeros(5,5); bw(2,2) = 1; bw(4,4) = 1;
52 % [D,L] = bwdist(bw)
53 %
54 % This example compares 2-D distance transforms for the four methods:
55 %
56 % bw = zeros(200,200); bw(50,50) = 1; bw(50,150) = 1;
57 % bw(150,100) = 1;
58 % D1 = bwdist(bw,'euclidean');
59 % D2 = bwdist(bw,'cityblock');
60 % D3 = bwdist(bw,'chessboard');
61 % D4 = bwdist(bw,'quasi-euclidean');
62 % figure
63 % subplot(2,2,1), subimage(mat2gray(D1)), title('Euclidean')
64 % hold on, imcontour(D1)
65 % subplot(2,2,2), subimage(mat2gray(D2)), title('City block')
66 % hold on, imcontour(D2)
67 % subplot(2,2,3), subimage(mat2gray(D3)), title('Chessboard')
68 % hold on, imcontour(D3)
69 % subplot(2,2,4), subimage(mat2gray(D4)), title('Quasi-Euclidean')
70 % hold on, imcontour(D4)
71 %
72 % This example compares isosurface plots for the distance transforms of
73 % a 3-D image containing a single nonzero pixel in the center:
74 %
75 % bw = zeros(50,50,50); bw(25,25,25) = 1;
76 % D1 = bwdist(bw);
77 % D2 = bwdist(bw,'cityblock');
78 % D3 = bwdist(bw,'chessboard');
79 % D4 = bwdist(bw,'quasi-euclidean');
80 % figure
81 % subplot(2,2,1), isosurface(D1,15), axis equal, view(3)
82 % camlight, lighting gouraud, title('Euclidean')
83 % subplot(2,2,2), isosurface(D2,15), axis equal, view(3)
84 % camlight, lighting gouraud, title('City block')
85 % subplot(2,2,3), isosurface(D3,15), axis equal, view(3)
86 % camlight, lighting gouraud, title('Chessboard')
87 % subplot(2,2,4), isosurface(D4,15), axis equal, view(3)
88 % camlight, lighting gouraud, title('Quasi-Euclidean')
89 %
90 % See also BWDIST_OLD, BWULTERODE, WATERSHED.
91
92 % Copyright 1993-2010 The MathWorks, Inc.
93 % $Revision: 1.7.4.12.2.1 $ $Date: 2011/07/18 00:32:44 $
94
1.00 808 95 [BW,method] = parse_inputs(varargin{:});
96
97 % Computing the nearest-neighbor transform is expensive in memory, so we
98 % only want to call the lower-level function ddist with two output
99 % arguments if we have been called with two output arguments. Also, for the
100 % Euclidean case, bwdistComputeEDTFT will be called instead of
101 % bwdistComputeEDT, when there are two output arguments.
808 102 if nargout <= 1
0.01 808 103 varargout = cell(1,1);
104 else
105 varargout = cell(1,2);
106 end
107
0.05 808 108 iptcheckinput(BW,{'logical'},{'nonsparse'},mfilename,'BW',1);
109
0.02 808 110 if strcmp(method,'euclidean')
111 % Use really fast methods for multidimensional Euclidean distance
112 % transforms and Euclidean closest feature transforms.
0.01 808 113 if (nargout == 2)
114 s = size(BW);
115 Idx = reshape(single(1:numel(BW)), s);
116 [D, L] = bwdistComputeEDTFT(BW, Idx);
117 varargout{1} = sqrt(D);
118 varargout{2} = L;
808 119 else
29.40 808 120 D = bwdistComputeEDT(BW);
3.17 808 121 varargout{1} = sqrt(D);
808 122 end
123 else
124 % All methods other than Euclidean use the same algorithm,
125 % implemented in private/ddist. The only difference is in the
126 % connectivity and weights used.
127 [weights,conn] = computeChamferMask(ndims(BW),method);
128
129 % check if conn is valid
130 try
131 iptcheckconn(conn,mfilename,'conn',5); %bogus argument position
132 catch %#ok<CTCH>
133 displayInternalError('conn');
134 end
135
136 % Postprocess the weights to make sure the center weight is
137 % zero, and to keep only values corresponding to nonzero
138 % connectivity values.
139 weights = weights(:);
140 weights((end+1)/2) = 0.0;
141 weights(~conn) = [];
142 if ( ~isa(weights,'double') || ~isreal(weights) || issparse(weights) || ...
143 any(isnan(weights(:))) )
144 displayInternalError('weights');
145 end
146
147 % Call the dual-scan neighborhood based algorithm.
148 [varargout{:}] = ddist(BW,single(conn),single(weights));
149 end
Other subfunctions in this file are not included in this listing.