annotate inst/immaximas.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 a0ec22eef32c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
1 ## Copyright (c) 2003-2005 Peter Kovesi
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
2 ## School of Computer Science & Software Engineering
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
3 ## The University of Western Australia
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
4 ## http://www.csse.uwa.edu.au/
662
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
5 ##
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
6 ## Permission is hereby granted, free of charge, to any person obtaining a copy
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
7 ## of this software and associated documentation files (the "Software"), to deal
662
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
8 ## in the Software without restriction, including without limitation the rights
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
9 ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
10 ## copies of the Software, and to permit persons to whom the Software is
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
11 ## furnished to do so, subject to the following conditions:
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
12 ##
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
13 ## The above copyright notice and this permission notice shall be included in
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
14 ## all copies or substantial portions of the Software.
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
15 ##
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
16 ## The software is provided "as is", without warranty of any kind, express or
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
17 ## implied, including but not limited to the warranties of merchantability,
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
18 ## fitness for a particular purpose and noninfringement. In no event shall the
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
19 ## authors or copyright holders be liable for any claim, damages or other
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
20 ## liability, whether in an action of contract, tort or otherwise, arising from,
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
21 ## out of or in connection with the software or the use or other dealings in the
a0ec22eef32c immaximas: fix license by using proper MIT license text after private conversation with copyright owner Peter Kovesi
carandraug
parents: 651
diff changeset
22 ## software.
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
23 ##
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
24 ## I've made minor changes compared to the original 'nonmaxsuppts' function developed
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
25 ## by Peter Kovesi. The original is available at
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
26 ## http://www.csse.uwa.edu.au/~pk/research/matlabfns/Spatial/nonmaxsuppts.m
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
27 ## -- Søren Hauberg, 2008
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
28
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
29 ## -*- texinfo -*-
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
30 ## @deftypefn {Function File} {[@var{r}, @var{c}] =} immaximas (@var{im}, @var{radius})
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
31 ## @deftypefnx{Function File} {[@var{r}, @var{c}] =} immaximas (@var{im}, @var{radius}, @var{thresh})
651
0d5958711749 image: use @dots{} for help text
carandraug
parents: 355
diff changeset
32 ## @deftypefnx{Function File} {[@var{r}, @var{c}, @dots{}] =} immaximas (@dots{})
0d5958711749 image: use @dots{} for help text
carandraug
parents: 355
diff changeset
33 ## @deftypefnx{Function File} {[@dots{}, @var{val}] =} immaximas (@dots{})
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
34 ## Finds local spatial maximas of the given image. A local spatial maxima is
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
35 ## defined as an image point with a value that is larger than all neighbouring
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
36 ## values in a square region of width 2*@var{radius}+1. By default @var{radius}
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
37 ## is 1, such that a 3 by 3 neighbourhood is searched. If the @var{thresh} input
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
38 ## argument is supplied, only local maximas with a value greater than @var{thresh}
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
39 ## are retained.
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
40 ##
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
41 ## The output vectors @var{r} and @var{c} contain the row-column coordinates
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
42 ## of the local maximas. The actual values are computed to sub-pixel precision
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
43 ## by fitting a parabola to the data around the pixel. If @var{im} is
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
44 ## @math{N}-dimensional, then @math{N} vectors will be returned.
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
45 ##
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
46 ## If @var{im} is @math{N}-dimensional, and @math{N}+1 outputs are requested,
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
47 ## then the last output will contain the image values at the maximas. Currently
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
48 ## this value is not interpolated.
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
49 ##
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
50 ## @seealso{ordfilt2, ordfiltn}
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
51 ## @end deftypefn
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
52
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
53 function varargout = immaximas(im, radius, thresh)
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
54 ## Check input
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
55 if (nargin == 0)
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
56 error("immaximas: not enough input arguments");
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
57 endif
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
58 if (nargin <= 1 || isempty(radius))
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
59 radius = 1;
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
60 endif
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
61 if (nargin <= 2)
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
62 thresh = [];
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
63 endif
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
64 if (!ismatrix(im))
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
65 error("immaximas: first input argument must be an array");
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
66 endif
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
67 if (!isscalar(radius))
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
68 error("immaximas: second input argument must be a scalar or an empty matrix");
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
69 endif
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
70 if (!isscalar(thresh) && !isempty(thresh))
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
71 error("immaximas: third input argument must be a scalar or an empty matrix");
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
72 endif
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
73
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
74 ## Find local maximas
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
75 nd = ndims(im);
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
76 s = size(im);
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
77 sze = 2*radius+1;
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
78 mx = ordfiltn(im, sze^nd, ones(repmat(sze,1, nd), "logical"), "reflect");
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
79 mx2 = ordfiltn(im, sze^nd-1, ones(repmat(sze,1, nd), "logical"), "reflect");
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
80
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
81 # Find maxima, threshold
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
82 immx = (im == mx) & (im != mx2);
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
83 if (!isempty(thresh))
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
84 immx &= (im>thresh);
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
85 endif
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
86
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
87 ## Find local maximas and fit parabolas locally
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
88 ind = find(immx);
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
89 [sub{1:nd}] = ind2sub(s, ind);
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
90 if (!isempty(ind))
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
91 w = 1; # Width that we look out on each side of the feature point to fit a local parabola
355
7f70d7848175 Fix indexing (by Dongik Shin)
hauberg
parents: 302
diff changeset
92 ws = w*cumprod([1; s(:)]);
7f70d7848175 Fix indexing (by Dongik Shin)
hauberg
parents: 302
diff changeset
93
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
94 ## We fit a parabola to the points in each dimension
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
95 for d = 1:nd
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
96 ## Indices of points above, below, left and right of feature point
355
7f70d7848175 Fix indexing (by Dongik Shin)
hauberg
parents: 302
diff changeset
97 indminus1 = max(ind-ws(d), 1);
7f70d7848175 Fix indexing (by Dongik Shin)
hauberg
parents: 302
diff changeset
98 indplus1 = min(ind+ws(d), numel(immx));
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
99
302
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
100 ## Solve quadratic
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
101 c = im(ind);
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
102 a = (im(indminus1) + im(indplus1))/2 - c;
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
103 b = a + c - im(indminus1);
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
104 shift = -w*b./(2*a); # Maxima of quadradic
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
105
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
106 ## Move point
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
107 sub{d} += shift;
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
108 endfor
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
109 endif
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
110
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
111 ## Output
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
112 varargout(1:nd) = sub(1:nd);
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
113 if (nargout > nd)
3aab53c3d0e9 Add support for N-dimensional input
hauberg
parents: 292
diff changeset
114 varargout{nd+1} = im(ind);
292
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
115 endif
2f0f137f2f05 New function for locating spatial maximas
hauberg
parents:
diff changeset
116 endfunction