Mercurial > forge
annotate main/image/inst/findbounds.m @ 11670:0f16ee5611b8 octave-forge
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
author | carandraug |
---|---|
date | Mon, 29 Apr 2013 12:53:23 +0000 |
parents | fbd81057dab0 |
children |
rev | line source |
---|---|
11662 | 1 ## Copyright (C) 2012 Pantxo Diribarne |
2 ## | |
3 ## This program is free software; you can redistribute it and/or modify | |
4 ## it under the terms of the GNU General Public License as published by | |
5 ## the Free Software Foundation; either version 3 of the License, or | |
6 ## (at your option) any later version. | |
7 ## | |
8 ## This program is distributed in the hope that it will be useful, | |
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 ## GNU General Public License for more details. | |
12 ## | |
13 ## You should have received a copy of the GNU General Public License | |
14 ## along with Octave; see the file COPYING. If not, see | |
15 ## <http://www.gnu.org/licenses/>. | |
16 | |
17 ## -*- texinfo -*- | |
18 ## @deftypefn {Function File} {@var{outbnd} =} findbounds (@var{T}, @var{inbnd}) | |
19 ## Estimate bounds for spatial transformation. | |
20 ## | |
21 ## Given a transformation structure @var{T} (see e.g. maketform) | |
22 ## and bounds @var{inbnd} (2-by-ndims_in) in an input space, returns | |
23 ## an estimation of the bounds in the output space @var{outbnd} | |
24 ## (2-by-ndims_out). For instance two dimensionnal bounds could | |
25 ## be represented as : [xmin ymin; xmax ymax]. If @var{T} does not | |
26 ## define a forward trasform (i.e. for 'polynomial'), the output | |
27 ## bounds are infered using fsolve and the inverse transform. | |
28 ## | |
29 ## @seealso{maketform, cp2tform, tformfwd} | |
30 ## @end deftypefn | |
31 | |
32 ## Author: Pantxo Diribarne <pantxo@dibona> | |
33 | |
34 function [outbnd] = findbounds (T, inbnd) | |
35 if (nargin != 2) | |
36 print_usage (); | |
37 elseif (! istform (T)) | |
38 error ("imtransform: T must be a transformation structure (see `maketform')"); | |
39 elseif (! all (size (inbnd) == [2 T.ndims_in])) | |
40 error ("imtransform: INBDN must have same size as T.ndims_in"); | |
41 endif | |
42 | |
43 ## Control points grid | |
44 if (columns (inbnd) == 2) | |
45 [xcp, ycp] = meshgrid (linspace (inbnd(1,1), inbnd(2,1), 3), | |
46 linspace (inbnd(1,2), inbnd(2,2), 3)); | |
47 xcp = reshape (xcp, numel (xcp), 1); | |
48 ycp = reshape (ycp, numel (ycp), 1); | |
49 xycp = [xcp, ycp]; | |
50 else | |
51 error ("findbounds: support only 2D inputbounds."); | |
52 endif | |
53 | |
54 ## Output bounds | |
55 if (!is_function_handle (T.forward_fcn)) | |
56 outbnd = zeros (size (xycp)); | |
57 for ii = 1:rows (xycp) | |
58 fun = @(x) tforminv (T, x) - xycp(ii,:); | |
59 outbnd(ii,:) = fsolve (fun, xycp(ii,:)); | |
60 endfor | |
61 else | |
62 outbnd = tformfwd (T, xycp); | |
63 endif | |
64 outbnd = [min(outbnd); max(outbnd)]; | |
65 endfunction | |
11670
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
66 |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
67 %!test |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
68 %! im = checkerboard (); |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
69 %! theta = pi/6; |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
70 %! T = maketform ('affine', [cos(theta) -sin(theta); ... |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
71 %! sin(theta) cos(theta); 0 0]); |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
72 %! inbnd = [0 0; 1 1]; |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
73 %! outbnd = findbounds (T, inbnd); |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
74 %! diag = 2^.5; |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
75 %! ang = pi/4; |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
76 %! assert (diff (outbnd(:,1)), diag * abs (cos (theta - ang)), eps) |
0f16ee5611b8
image: add tests to findbounds and imtransform. Patch by Pantxo Diribarne <pantxo@dibona>
carandraug
parents:
11662
diff
changeset
|
77 %! assert (diff (outbnd(:,2)), diag * abs (cos (theta - ang)), eps) |