annotate inst/houghtf.m @ 821:72cd72a3bff6

bestblk: complete rewrite to suppport multi-dimensional matrices. * bestblk.m: implement support for any number of dimensions. Vectorize code (loop only over dimensions now). Add tests. * NEWS: new section for functions now supporting N-dimensional matrices.
author Carnë Draug <carandraug@octave.org>
date Fri, 01 Nov 2013 05:05:28 +0000
parents c6be7812523a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
709
c6be7812523a maint: fix Søren's name
carandraug
parents: 561
diff changeset
1 ## Copyright (C) 2008 Søren Hauberg <soren@hauberg.org>
561
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
2 ##
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
3 ## This program is free software; you can redistribute it and/or modify it under
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
4 ## the terms of the GNU General Public License as published by the Free Software
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
5 ## Foundation; either version 3 of the License, or (at your option) any later
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
6 ## version.
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
7 ##
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
11 ## details.
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
12 ##
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
13 ## You should have received a copy of the GNU General Public License along with
c45838839d86 maint: update license to GPLv3 and mention non GPL files
carandraug
parents: 463
diff changeset
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
287
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
15
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
16 ## -*- texinfo -*-
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
17 ## @deftypefn {Function File} @var{H} = houghtf (@var{bw})
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
18 ## @deftypefnx{Function File} @var{H} = houghtf (@var{bw}, @var{method})
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
19 ## @deftypefnx{Function File} @var{H} = houghtf (@var{bw}, @var{method}, @var{arg})
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
20 ## Perform the Hough transform for lines or circles.
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
21 ##
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
22 ## The @var{method} argument chooses between the Hough transform for lines and
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
23 ## circles. It can be either "line" (default) or "circle".
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
24 ##
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
25 ## @strong{Line Detection}
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
26 ##
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
27 ## If @var{method} is "line", the function will compute the Hough transform for
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
28 ## lines. A line is parametrised in @var{r} and @var{theta} as
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
29 ## @example
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
30 ## @var{r} = x*cos(@var{theta}) + y*sin(@var{theta}),
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
31 ## @end example
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
32 ## where @var{r} is distance between the line and the origin, while @var{theta}
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
33 ## is the angle of the vector from the origin to this closest point. The result
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
34 ## @var{H} is an @var{N} by @var{M} matrix containing the Hough transform. Here,
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
35 ## @var{N} is the number different values of @var{r} that has been attempted.
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
36 ## This is computed as @code{2*diag_length - 1}, where @code{diag_length} is
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
37 ## the length of the diagonal of the input image. @var{M} is the number of
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
38 ## different values of @var{theta}. These can be set through the third input
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
39 ## argument @var{arg}. This must be a vector of real numbers, and is by default
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
40 ## @code{pi*(-90:90)/180}.
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
41 ##
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
42 ## @strong{Circle Detection}
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
43 ##
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
44 ## If @var{method} is "circle" the function will compute the Hough transform for
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
45 ## circles. The circles are parametrised in @var{r} which denotes the radius of
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
46 ## the circle. The third input argument @var{arg} must be a real vector containing
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
47 ## the possible values of @var{r}.
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
48 ## If the input image is @var{N} by @var{M}, then the result @var{H} will be an
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
49 ## @var{N} by @var{M} by @var{K} array, where @var{K} denotes the number of
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
50 ## different values of @var{r}.
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
51 ##
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
52 ## As an example, the following shows how to compute the Hough transform for circles
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
53 ## with radius 3 or 7 in the image @var{im}
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
54 ## @example
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
55 ## bw = edge(im);
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
56 ## H = houghtf(bw, "circle", [3, 7]);
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
57 ## @end example
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
58 ## Here @var{H} will be an NxMx2 array, where @var{H}(:,:,1) will contain the
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
59 ## Hough transform for circles with radius 3, and @var{H}(:,:,2) for radius 7.
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
60 ## To find good circles you now need to find local maximas in @var{H}. If you
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
61 ## find a local maxima in @var{H}(row, col, 1) it means that a good circle exists
294
91de0c0b67f5 Mention 'immaximas' in 'houghtf' help text
hauberg
parents: 287
diff changeset
62 ## with center (row,col) and radius 3. One way to locate maximas is to use the
91de0c0b67f5 Mention 'immaximas' in 'houghtf' help text
hauberg
parents: 287
diff changeset
63 ## @code{immaximas} function.
287
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
64 ##
294
91de0c0b67f5 Mention 'immaximas' in 'houghtf' help text
hauberg
parents: 287
diff changeset
65 ## @seealso{hough_line, hough_circle, immaximas}
287
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
66 ## @end deftypefn
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
67
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
68 function [accum, R] = houghtf(bw, varargin)
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
69 ## Default arguments
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
70 method = "line";
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
71 args = {};
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
72
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
73 ## Check input arguments
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
74 if (nargin == 0)
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
75 error("houghtf: not enough input arguments");
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
76 endif
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
77
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
78 if (!ismatrix(bw) || ndims(bw) != 2)
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
79 error("houghtf: first arguments must be a 2-dimensional matrix");
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
80 endif
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
81
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
82 if (nargin > 1)
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
83 if (ischar(varargin{1}))
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
84 method = varargin{1};
323
bed6f83a4687 Correct typos
adb014
parents: 294
diff changeset
85 args = varargin(2:end);
287
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
86 else
323
bed6f83a4687 Correct typos
adb014
parents: 294
diff changeset
87 args = varargin;
287
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
88 endif
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
89 endif
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
90
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
91 ## Choose method
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
92 switch (lower(method))
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
93 case "line"
323
bed6f83a4687 Correct typos
adb014
parents: 294
diff changeset
94 [accum, R] = hough_line(bw, args{:});
287
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
95 case "circle"
323
bed6f83a4687 Correct typos
adb014
parents: 294
diff changeset
96 accum = hough_circle(bw, args{:});
287
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
97 otherwise
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
98 error("houghtf: unsupported method '%s'", method);
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
99 endswitch
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
100
c04781184da0 Support for circle detection using the Hough transform
hauberg
parents:
diff changeset
101 endfunction