Mercurial > hg > octave-image
comparison inst/maketform.m @ 746:43686e9e8eae
image: new spatial transformation functions
* maketform: accept input/output control points as second and third argument
* imtransform: new function
* findsbounds: new function
* private/istform: add comment on what function does
* NEWS/INDEX: update list of new functions
author | carandraug |
---|---|
date | Sun, 28 Apr 2013 02:31:13 +0000 |
parents | bb7e5991924f |
children |
comparison
equal
deleted
inserted
replaced
745:27d87f06dd99 | 746:43686e9e8eae |
---|---|
1 ## Copyright (C) 2012 Pantxo Diribarne | 1 ## Copyright (C) 2012 Pantxo Diribarne |
2 ## | 2 ## |
3 ## This program is free software; you can redistribute it and/or modify | 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 | 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 | 5 ## the Free Software Foundation; either version 3 of the License, or |
6 ## (at your option) any later version. | 6 ## (at your option) any later version. |
7 ## | 7 ## |
8 ## This program is distributed in the hope that it will be useful, | 8 ## This program is distributed in the hope that it will be useful, |
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 ## GNU General Public License for more details. | 11 ## GNU General Public License for more details. |
12 ## | 12 ## |
13 ## You should have received a copy of the GNU General Public License | 13 ## You should have received a copy of the GNU General Public License |
14 ## along with Octave; see the file COPYING. If not, see | 14 ## along with Octave; see the file COPYING. If not, see |
15 ## <http://www.gnu.org/licenses/>. | 15 ## <http://www.gnu.org/licenses/>. |
16 | 16 |
17 ## -*- texinfo -*- | 17 ## -*- texinfo -*- |
18 ## @deftypefn {Function File} {@var{T} =} maketform (@var{ttype}, @var{tmat}) | 18 ## @deftypefn {Function File} {@var{T} =} maketform (@var{ttype}, @var{tmat}) |
19 ## @deftypefnx {Function File} {@var{T} =} maketform (@var{ttype}, @var{inc}, @var{outc}) | |
19 ## @deftypefnx {Function File} {@var{T} =} maketform ("custom", @var{ndims_in}, @var{ndims_out}, @var{forward_fcn}, @var{inverse_fcn}, @var{tdata}) | 20 ## @deftypefnx {Function File} {@var{T} =} maketform ("custom", @var{ndims_in}, @var{ndims_out}, @var{forward_fcn}, @var{inverse_fcn}, @var{tdata}) |
21 ## Create structure for spatial transformations. | |
22 ## | |
20 ## Returns a transform structure containing fields @var{ndims_in}, | 23 ## Returns a transform structure containing fields @var{ndims_in}, |
21 ## @var{ndims_out}, @var{forward_fcn}, @var{inverse_fcn} and @var{tdata}. The content | 24 ## @var{ndims_out}, @var{forward_fcn}, @var{inverse_fcn} and @var{tdata}. The |
22 ## of each field depends on the requested transform type @var{ttype}: | 25 ## content of each field depends on the requested transform type @var{ttype}: |
26 ## | |
23 ## @table @asis | 27 ## @table @asis |
24 ## @item "projective" | 28 ## @item "projective" |
25 ## A ndims_in = N -> ndims_out = N projective transformation structure | 29 ## A ndims_in = N -> @var{ndims_out} = N projective transformation structure |
26 ## is returned. | 30 ## is returned. |
27 ## The second input argument @var{tmat} must be a (N+1)-by-(N+1) | 31 ## The second input argument @var{tmat} must be a (N+1)-by-(N+1) |
28 ## transformation matrix. The | 32 ## transformation matrix. The |
29 ## (N+1)th column must contain projection coefficients. As an example a two | 33 ## (N+1)th column must contain projection coefficients. As an example a two |
30 ## dimentionnal transform from [x y] coordinates to [u v] coordinates | 34 ## dimentionnal transform from [x y] coordinates to [u v] coordinates |
31 ## is represented by a transformation matrix defined so that: | 35 ## is represented by a transformation matrix defined so that: |
36 ## | |
32 ## @example | 37 ## @example |
33 ## [xx yy zz] = [u v 1] * [a d g; | 38 ## [xx yy zz] = [u v 1] * [a d g; |
34 ## b e h; | 39 ## b e h; |
35 ## c f i] | 40 ## c f i] |
36 ## [x y] = [xx./zz yy./zz]; | 41 ## [x y] = [xx./zz yy./zz]; |
37 ## @end example | 42 ## @end example |
43 ## | |
44 ## Alternatively the transform can be specified using a quadilateral | |
45 ## coordinates (typically the 4 corners of the | |
46 ## image) in the input space (@var{inc}, 4-by-ndims_in matrix) and in | |
47 ## the output space (@var{outc}, 4-by-ndims_out matrix). This is | |
48 ## equivalent to building the transform using | |
49 ## @code{T = cp2tform (@var{inc}, @var{outc}, "projective")}. | |
50 ## | |
38 ## @item "affine" | 51 ## @item "affine" |
39 ## Affine is a subset of projective transform (see above). A ndims_in = N -> | 52 ## Affine is a subset of projective transform (see above). A |
40 ## ndims_out = N affine transformation structure is returned. | 53 ## @var{ndims_in} = N -> @var{ndims_out} = N affine transformation structure is |
54 ## returned. | |
41 ## The second input argument @var{tmat} must be a (N+1)-by-(N+1) or | 55 ## The second input argument @var{tmat} must be a (N+1)-by-(N+1) or |
42 ## (N+1)-by-(N) transformation matrix. If present, the (N+1)th column must | 56 ## (N+1)-by-(N) transformation matrix. If present, the (N+1)th column must |
43 ## contain [zeros(N,1); 1] so that projection is suppressed. | 57 ## contain [zeros(N,1); 1] so that projection is suppressed. |
58 ## | |
59 ## Alternatively the transform can be specified a using a triangle | |
60 ## coordinates (typically the 3 corners of the | |
61 ## image) in the input space (@var{inc}, 3-by-ndims_in matrix) and in | |
62 ## the output space (@var{outc}, 3-by-ndims_out matrix). This is | |
63 ## equivalent to building the transform using "T = cp2tform (@var{inc}, @var{outc}, | |
64 ## 'affine')". | |
65 ## | |
44 ## @item "custom" | 66 ## @item "custom" |
45 ## For user defined transforms every field of the transform structure | 67 ## For user defined transforms every field of the transform structure |
46 ## must be supplied. The prototype of the transform functions, | 68 ## must be supplied. The prototype of the transform functions, |
47 ## @var{forward_fcn} and @var{inverse_fcn}, should be X' = | 69 ## @var{forward_fcn} and @var{inverse_fcn}, should be X' = |
48 ## transform_fcn (X, T). X and X' are respectively p-by-ndims_in and | 70 ## transform_fcn (X, T). X and X' are respectively p-by-ndims_in and |
49 ## p-by-ndims_out arrays for forward_fcn and reversed for inverse_fcn. | 71 ## p-by-ndims_out arrays for forward_fcn and reversed for inverse_fcn. |
50 ## The argument T is the transformation structure which will contain | 72 ## The argument T is the transformation structure which will contain |
51 ## the user supplied transformation matrix @var{tdata}. | 73 ## the user supplied transformation matrix @var{tdata}. |
52 ## @end table | 74 ## @end table |
75 ## | |
53 ## @seealso{tformfwd, tforminv, cp2tform} | 76 ## @seealso{tformfwd, tforminv, cp2tform} |
54 ## @end deftypefn | 77 ## @end deftypefn |
55 | 78 |
56 ## Author: Pantxo Diribarne <pantxo@dibona> | 79 ## Author: Pantxo Diribarne <pantxo@dibona> |
57 ## Created: 2012-09-05 | |
58 | 80 |
59 function T = maketform (ttype, varargin) | 81 function T = maketform (ttype, varargin) |
60 | 82 |
61 if (nargin < 2 || ! any (strcmp (ttype, {"affine", "projective", "custom"}))) | 83 if (nargin < 2 || ! any (strcmpi (ttype, {"affine", "projective", "custom"}))) |
62 print_usage (); | 84 print_usage (); |
63 endif | 85 endif |
86 | |
64 if (numel (varargin) == 1) | 87 if (numel (varargin) == 1) |
65 tmat = varargin {1}; | 88 tmat = varargin {1}; |
66 ndin = rows (tmat) - 1; | 89 ndin = rows (tmat) - 1; |
67 ndout = columns (tmat) - 1; | 90 ndout = columns (tmat) - 1; |
68 if (ndin < 2); | 91 if (ndin < 2); |
92 T.ndims_out = ndout; | 115 T.ndims_out = ndout; |
93 T.forward_fcn = forward_fcn; | 116 T.forward_fcn = forward_fcn; |
94 T.inverse_fcn = inverse_fcn; | 117 T.inverse_fcn = inverse_fcn; |
95 T.tdata.T = tmat; | 118 T.tdata.T = tmat; |
96 T.tdata.Tinv = inv (tmat); | 119 T.tdata.Tinv = inv (tmat); |
120 | |
121 elseif (numel (varargin) == 2) | |
122 inc = varargin{1}; | |
123 outc = varargin{2}; | |
124 if (strcmp (ttype, "affine")) | |
125 if (all (size (inc) == size (outc)) && | |
126 all (size (inc) == [3 2])) | |
127 T = cp2tform (inc, outc, ttype); | |
128 else | |
129 error ("maketform: expect INC and OUTC to be 3-by-2 vectors."); | |
130 endif | |
131 elseif (strcmp (ttype, "projective")) | |
132 if (all (size (inc) == size (outc)) && | |
133 all (size (inc) == [4 2])) | |
134 T = cp2tform (inc, outc, ttype); | |
135 else | |
136 error ("maketform: expect INC and OUTC to be 4-by-2 vectors."); | |
137 endif | |
138 endif | |
139 | |
97 elseif (numel (varargin) == 5 && strcmpi (ttype, "custom")) | 140 elseif (numel (varargin) == 5 && strcmpi (ttype, "custom")) |
98 if (isscalar (varargin{1}) && isscalar (varargin{2}) | 141 if (isscalar (varargin{1}) && isscalar (varargin{2}) |
99 && varargin{1} > 0 && varargin{2} > 0) | 142 && varargin{1} > 0 && varargin{2} > 0) |
100 T.ndims_in = varargin{1}; | 143 T.ndims_in = varargin{1}; |
101 T.ndims_out = varargin{2}; | 144 T.ndims_out = varargin{2}; |
112 else | 155 else |
113 error ("maketform: expect function handle as inverse_fcn.") | 156 error ("maketform: expect function handle as inverse_fcn.") |
114 endif | 157 endif |
115 | 158 |
116 T.tdata = varargin{5}; | 159 T.tdata = varargin{5}; |
160 | |
117 else | 161 else |
118 print_usage (); | 162 print_usage (); |
119 endif | 163 endif |
120 endfunction | 164 endfunction |
121 | 165 |