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