Mercurial > hg > octave-image
annotate inst/imgradient.m @ 892:a2140b980079
iptcheckconn: implement in C++ as static method for connectivity.
* iptcheckconn.m: file removed; help text and tests reused for C++.
* conndef.cc: implement two new connectivity::validate() methods and
the iptcheckconn function for Octave as caller to those methods.
* conndef.h: define the connectivity::validate() static methods.
* COPYING
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Wed, 01 Oct 2014 20:22:37 +0100 |
parents | 9e4ecfd52273 |
children |
rev | line source |
---|---|
858
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
1 ## Copyright (C) 2013 Brandon Miles <brandon.miles7@gmail.com> |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
2 ## |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
3 ## This program is free software; you can redistribute it and/or modify it |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
4 ## under the terms of the GNU General Public License as published by |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
5 ## the Free Software Foundation; either version 3 of the License, or |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
6 ## (at your option) any later version. |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
7 ## |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
8 ## This program is distributed in the hope that it will be useful, |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
11 ## GNU General Public License for more details. |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
12 ## |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
13 ## You should have received a copy of the GNU General Public License |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
14 ## along with this program. If not, see <http://www.gnu.org/licenses/>. |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
15 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
16 ## -*- texinfo -*- |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
17 ## @deftypefn {Function File} {[@var{gradMag}, @var{gradDir}] =} imgradient (@var{img}) |
859
9e4ecfd52273
imgradient.m: fix typo on Texinfo
Carnë Draug <carandraug@octave.org>
parents:
858
diff
changeset
|
18 ## @deftypefnx {Function File} {[@var{gradMag}, @var{gradDir}] =} imgradient (@var{img}, @var{method}) |
858
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
19 ## @deftypefnx {Function File} {[@var{gradMag}, @var{gradDir}] =} imgradient (@var{gx}, @var{gy}) |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
20 ## Compute the gradient magnitude and direction in degrees for an image. |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
21 ## |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
22 ## These are computed from the @var{gx} and @var{xy} gradients using |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
23 ## @code{imgradientxy}. The first input @var{img} is a gray scale image to |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
24 ## compute the edges on. The second input @var{method} controls the method |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
25 ## used to calculate the gradients. Alternatively the first input @var{gx} |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
26 ## can be the x gradient and the second input @var{gy} can be the y gradient. |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
27 ## |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
28 ## The first output @var{gradMag} returns the magnitude of the gradient. |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
29 ## The second output @var{gradDir} returns the direction in degrees. |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
30 ## |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
31 ## The @var{method} input argument must be a string specifying one of the |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
32 ## methods supported by @code{imgradientxy}. |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
33 ## |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
34 ## @seealso{edge, imgradientxy} |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
35 ## @end deftypefn |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
36 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
37 function [gradMag, gradDir] = imgradient (img, method = "sobel") |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
38 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
39 if (nargin < 1 || nargin > 2) |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
40 print_usage (); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
41 elseif (ndims (img) != 2) |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
42 error("imgradient: IMG must be a 2 dimensional matrix"); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
43 endif |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
44 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
45 if (ischar (method)) |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
46 [gradX, gradY] = imgradientxy (img, method); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
47 else |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
48 ## we already got gX and gY, just confirm it's good data |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
49 if (! size_equal (img, method)) |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
50 error("imgradient: GX and GY must be of equal size") |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
51 endif |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
52 gradX = img; |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
53 gradY = method; |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
54 endif |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
55 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
56 gradMag = sqrt (gradX.^2 + gradY.^2); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
57 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
58 if (nargout > 1) |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
59 ## Why imgradient invert vertical when computing the angle |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
60 ## Use atan2(-gy,gx)*pi/180. See http://stackoverflow.com/questions/18549015 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
61 gradDir = atan2d (-gradY, gradX); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
62 endif |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
63 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
64 endfunction |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
65 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
66 %!test |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
67 %! A = [0 1 0 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
68 %! 1 1 1 |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
69 %! 0 1 0]; |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
70 %! |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
71 %! [gMag, gDir] = imgradient (A); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
72 %! assert (gMag,[sqrt(18) 4 sqrt(18); 4 0 4; sqrt(18),4,sqrt(18)]); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
73 %! assert (gDir,[-45 -90 -135; -0 -0 -180; 45 90 135]); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
74 %! |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
75 %! ## the following just test if passing gx and gy separately gets |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
76 %! ## us the same as the image and method though imgradient |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
77 %! [gxSobel, gySobel] = imgradientxy (A, "Sobel"); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
78 %! [gxPrewitt, gyPrewitt] = imgradientxy (A, "Prewitt"); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
79 %! [gxCd, gyCd] = imgradientxy (A, "CentralDifference"); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
80 %! [gxId, gyId] = imgradientxy (A, "IntermediateDifference"); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
81 %! |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
82 %! assert (imgradient (A), |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
83 %! imgradient (gxSobel, gySobel)); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
84 %! assert (imgradient (A, "Sobel"), |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
85 %! imgradient (gxSobel, gySobel)); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
86 %! assert (imgradient (A, "Prewitt"), |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
87 %! imgradient(gxPrewitt, gyPrewitt)); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
88 %! assert (imgradient (A, "CentralDifference"), |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
89 %! imgradient (gxCd, gyCd)); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
90 %! assert (imgradient (A, "IntermediateDifference"), |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
91 %! imgradient (gxId, gyId)); |
af3c7946534b
Follow coding guidelines and fix tests.
Carnë Draug <carandraug@octave.org>
parents:
857
diff
changeset
|
92 |