annotate main/image/inst/imtransform.m @ 11662:fbd81057dab0 octave-forge

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
children 0f16ee5611b8
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{B} =} imtransform (@var{A}, @var{T})
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
19 ## @deftypefnx {Function File} {@var{B} =} imtransform (@var{A}, @var{T}, @var{interp})
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
20 ## @deftypefnx {Function File} {@var{B} =} imtransform (@dots{}, @var{prop}, @var{val})
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
21 ## Transform image.
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
22 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
23 ## Given an image @var{A} in one space, returns an image @var{B}
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
24 ## resulting from the forward transform defined in the transformation
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
25 ## structure @var{T}. An additionnal input argument @var{interp},
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
26 ## 'bicubic', 'bilinear' (default) or 'nearest',
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
27 ## specifies the interpolation method to be used. Finally, the
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
28 ## transformation can be tuned using @var{prop}/@var{val} pairs. The
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
29 ## following properties are supported:
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
30 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
31 ## @table @asis
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
32 ## @item "udata"
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
33 ## Specifies the input space horizontal limits. Value must be a two
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
34 ## elements vector [minval maxval]. Default: [1 columns(@var{A})]
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
35 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
36 ## @item "vdata"
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
37 ## Specifies the input space vertical limits. Value must be a two
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
38 ## elements vector [minval maxval]. Default: [1 rows(@var{A})]
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
39 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
40 ## @item "xdata"
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
41 ## Specifies the requiered output space horizontal limits. Value must
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
42 ## be a two elements vector [minval maxval]. Default: estimated using
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
43 ## udata, vdata and findbounds function.
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
44 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
45 ## @item "ydata"
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
46 ## Specifies the requiered output space vertical limits. Value must
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
47 ## be a two elements vector [minval maxval]. Default: estimated using
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
48 ## udata, vdata and findbounds function.
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
49 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
50 ## @item "xyscale"
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
51 ## Specifies the output scale in outputspace_units/pixel. If a scalar
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
52 ## is provided, both vertical and horizontal dimensions are scaled the
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
53 ## same way. If @var{val} is a two element vector, it must indicate
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
54 ## consecutively horizontal and vertical scales. Default value is
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
55 ## computed using the input space scale provided
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
56 ## that the number of pixel of any dimension of the output image does
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
57 ## not exceed 20000.
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
58 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
59 ## @item "size"
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
60 ## Size of the output image (1-by-2 vector). Overrides the effect of
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
61 ## "xyscale" property.
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
62 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
63 ## @item "fillvalues"
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
64 ## Color of the areas where no interpolation where possible, e.g. when
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
65 ## coorfinates of points in the output space are out of the limits of
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
66 ## the input space. @var{val} must be coherent with the input image
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
67 ## format: for grayscale and indexed images (2D) @var{val} must be
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
68 ## scalar, for RGB (n-by-m-by-3) @var{val} must be a 3 element vector.
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
69 ## e.g.:
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
70 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
71 ## @end table
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
72 ##
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
73 ## @seealso{maketform, cp2tform, tforminv, tformfwd, findbounds}
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
74 ## @end deftypefn
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
75
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
76 ## Author: Pantxo Diribarne <pantxo@dibona>
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
77
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
78 function [varargout] = imtransform (im, T, varargin)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
79
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
80 if (nargin < 2)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
81 print_usage ();
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
82 elseif (! istform (T))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
83 error ("imtransform: T must be a transformation structure (see `maketform')");
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
84 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
85
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
86 ## Parameters
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
87 interp = "linear";
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
88 imdepth = size (im, 3);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
89 maximsize = [20000 20000];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
90
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
91 udata = [1; columns(im)];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
92 vdata = [1; rows(im)];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
93 xdata = ydata = [];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
94 xyscale = [];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
95 imsize = [];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
96 fillvalues = ones (1, imdepth) * NaN;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
97
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
98 if (isempty (varargin))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
99 xydata = findbounds (T, [udata vdata]);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
100 xdata = xydata(:,1);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
101 ydata = xydata(:,2);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
102 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
103 ## interp
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
104 if (floor (numel (varargin)/2) != numel (varargin)/2)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
105 allowed = {"bicubic", "bilinear", "nearest"};
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
106 tst = strcmp (varargin{1}, allowed);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
107 if (!any (tst))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
108 error ("imtransform: expect one of %s as interp method", disp (allowed));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
109 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
110 interp = {"pchip", "linear", "nearest"}{find (tst)};
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
111 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
112 varargin = varargin(2:end);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
113 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
114
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
115 ## options
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
116 allowed = {"udata", "vdata", "xdata", "ydata", ...
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
117 "xyscale", "size", "fillvalues"};
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
118 props = varargin(1:2:end);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
119 vals = varargin(2:2:end);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
120 np = numel (props);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
121 if (!all (cellfun (@ischar, props)))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
122 error ("imtransform: expect property/value pairs.");
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
123 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
124
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
125 props = tolower (props);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
126 tst = cellfun (@(x) any (strcmp (x, allowed)), props);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
127 if (!all (tst))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
128 error ("imtransform: unknown property %s", disp (props{!tst}));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
129 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
130
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
131 ## u(vxy)data
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
132 iolims = allowed(1:4);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
133 for ii = 1:numel (iolims)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
134 tst = cellfun (@(x) any (strcmp (x, iolims{ii})), props);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
135 if (any (tst))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
136 prop = props{find (tst)(1)};
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
137 val = vals{find (tst)(1)};
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
138 if (isnumeric (val) && numel (val) == 2)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
139 if (isrow (val))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
140 val = val';
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
141 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
142 eval (sprintf ("%s = val;", prop),
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
143 "error (\"imtransform: %s\n\", lasterr ());");
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
144 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
145 error ("imtransform: expect 2 elements real vector for %s", prop)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
146 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
147 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
148 endfor
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
149 if (isempty (xdata) && isempty (ydata))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
150 xydata = findbounds (T, [udata vdata]);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
151 xdata = xydata(:,1);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
152 ydata = xydata(:,2);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
153 elseif (isempty (xdata))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
154 xydata = findbounds (T, [udata vdata]);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
155 xdata = xydata(:,1);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
156 elseif (isempty (ydata))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
157 xydata = findbounds (T, [udata vdata]);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
158 ydata = xydata(:,2);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
159 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
160
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
161 ## size and xyscale
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
162 tst = strcmp ("size", props);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
163 if (any (tst))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
164 val = vals{find (tst)(1)};
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
165 if (isnumeric (val) && numel (val) == 2 &&
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
166 all (val > 0))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
167 imsize = val;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
168 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
169 error ("imtransform: expect 2 elements real vector for size");
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
170 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
171 elseif (any (tst = strcmp ("xyscale", props)))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
172 val = vals{find (tst)(1)};
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
173 if (isnumeric (val) && all (val > 0))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
174 if (numel (val) == 1)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
175 xyscale(1:2) = val;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
176 elseif (numel (val) == 2)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
177 xyscale = val;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
178 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
179 error ("imtransform: expect 1 or 2 element(s) real vector for xyscale");
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
180 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
181 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
182 error ("imtransform: expect 1 or 2 elements real vector for xyscale");
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
183 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
184 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
185 xyscale = [(diff (udata) / columns (im)) (diff (vdata) / rows (im))];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
186 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
187
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
188 ## Fillvalues
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
189 tst = strcmp ("fillvalues", props);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
190 if (any (tst))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
191 val = vals{find (tst)(1)};
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
192 if (isnumeric (val) && numel (val) == 1)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
193 fillvalues(1:end) = val;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
194 elseif (isnumeric (val) && numel (val) == 3)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
195 fillvalues = val;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
196 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
197 error ("imtransform: expect 1 or 3 elements real vector for `fillvalues'");
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
198 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
199 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
200 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
201
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
202 ## Ouput/Input pixels
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
203 if (isempty (imsize))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
204 if (isempty (xyscale))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
205 xyscale = [(diff (udata) / columns (im)) (diff (vdata) / rows (im))];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
206 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
207 xscale = xyscale(1);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
208 yscale = xyscale(2);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
209 xsize = floor (diff (xdata) / xscale);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
210 ysize = floor (diff (ydata) / yscale);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
211 if (xsize > maximsize(2) || ysize > maximsize(1))
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
212 if (xsize >= ysize)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
213 scalefactor = (diff (xdata) / maximsize(2)) / xscale;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
214 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
215 scalefactor = (diff (ydata) / maximsize(1)) / yscale;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
216 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
217 xscale *= scalefactor
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
218 yscale *= scalefactor
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
219 xsize = floor (diff (xdata) / xscale);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
220 ysize = floor (diff (ydata) / yscale);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
221 warning ("imtransform: output image two large, adjusting the largest dimension to %d", maximsize);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
222 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
223 imsize = [ysize xsize];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
224 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
225 [xx yy] = meshgrid (linspace (xdata(1), xdata(2), imsize(2)),
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
226 linspace (ydata(1), ydata(2), imsize(1)));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
227
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
228 [uu vv] = meshgrid (linspace (udata(1), udata(2), size(im)(2)),
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
229 linspace (vdata(1), vdata(2), size(im)(1)));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
230
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
231 ## Input coordinates
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
232 [uui, vvi] = tforminv (T, reshape (xx, numel (xx), 1),
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
233 reshape (yy, numel (yy), 1));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
234 uui = reshape (uui, size (xx));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
235 vvi = reshape (vvi, size (yy));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
236 ## Interpolation
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
237 for layer = 1:imdepth
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
238 imout(:,:,layer) = interp2 (uu, vv, im(:,:,layer), ...
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
239 uui, vvi, interp, fillvalues(layer));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
240 endfor
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
241 if (nargout == 1)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
242 varargout{1} = imout;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
243 else
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
244 varargout = {imout, xdata, ydata};
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
245 endif
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
246 endfunction
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
247
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
248 %!demo
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
249 %! ## Various linear transforms
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
250 %! figure ();
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
251 %! im = [checkerboard(20, 2, 4); checkerboard(40, 1, 2)];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
252 %! %input space corners
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
253 %! incp = [1 1; 160 1; 160 160; 1 160];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
254 %! udata = [min(incp(:,1)) max(incp(:,1))];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
255 %! vdata = [min(incp(:,2)) max(incp(:,2))];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
256 %! subplot (2,3,1);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
257 %! imshow (im)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
258 %! hold on
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
259 %! plot (incp(:,1), incp(:,2), 'ob')
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
260 %! axis on
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
261 %! xlabel ('Original')
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
262 %!
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
263 %! % Translation and scaling
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
264 %! outcp = incp * 2;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
265 %! outcp(:,1) += 200;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
266 %! outcp(:,2) += 500;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
267 %! T = maketform ('affine', incp(1:3,:), outcp(1:3,:));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
268 %! subplot (2,3,2);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
269 %! [im2 xdata ydata] = imtransform (im, T, 'udata', udata,
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
270 %! 'vdata', vdata, 'fillvalues', 1);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
271 %! imh = imshow (im2); set (imh, 'xdata', xdata, 'ydata', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
272 %! set (gca, 'xlim', xdata, 'ylim', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
273 %! axis on, hold on, xlabel ('Translation / Scaling');
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
274 %! plot (outcp(:,1), outcp(:,2), 'or')
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
275 %!
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
276 %! % Shear
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
277 %! outcp = [1 1; 160 1; 140 160; -19 160]; % affine only needs 3 control points
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
278 %! T = maketform ('affine', incp(1:3,:), outcp(1:3,:));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
279 %! subplot (2,3,3);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
280 %! [im2 xdata ydata] = imtransform (im, T, 'udata', udata,
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
281 %! 'vdata', vdata, 'fillvalues', 1);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
282 %! imh = imshow (im2); set (imh, 'xdata', xdata, 'ydata', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
283 %! set (gca, 'xlim', xdata, 'ylim', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
284 %! axis on, hold on, xlabel ('Shear');
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
285 %! plot (outcp(:,1), outcp(:,2), 'or')
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
286 %!
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
287 %! % Rotation
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
288 %! theta = pi/4;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
289 %! T = maketform ('affine', [cos(theta) -sin(theta); ...
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
290 %! sin(theta) cos(theta); 0 0]);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
291 %! outcp = tformfwd (T, incp);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
292 %! subplot (2,3,4);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
293 %! [im2 xdata ydata] = imtransform (im, T, 'udata', udata,
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
294 %! 'vdata', vdata, 'fillvalues', 1 );
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
295 %! imh = imshow (im2); set (imh, 'xdata', xdata, 'ydata', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
296 %! set (gca, 'xlim', xdata, 'ylim', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
297 %! axis on, hold on, xlabel ('Rotation');
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
298 %! plot (outcp(:,1), outcp(:,2), 'or')
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
299 %!
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
300 %! % Reflection around x axis
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
301 %! outcp = incp;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
302 %! outcp(:,2) *= -1;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
303 %! T = cp2tform (incp, outcp, 'similarity');
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
304 %! subplot (2,3,5);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
305 %! [im2 xdata ydata] = imtransform (im, T, 'udata', udata,
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
306 %! 'vdata', vdata, 'fillvalues', 1 );
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
307 %! imh = imshow (im2); set (imh, 'xdata', xdata, 'ydata', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
308 %! set (gca, 'xlim', xdata, 'ylim', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
309 %! axis on, hold on, xlabel ('Reflection');
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
310 %! plot (outcp(:,1), outcp(:,2), 'or')
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
311 %!
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
312 %! % Projection
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
313 %! outcp = [1 1; 160 -40; 220 220; 12 140];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
314 %! T = maketform ('projective', incp, outcp);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
315 %! subplot (2,3,6);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
316 %! [im2 xdata ydata] = imtransform (im, T, 'udata', udata,
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
317 %! 'vdata', vdata, 'fillvalues', 1 );
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
318 %! imh = imshow (im2); set (imh, 'xdata', xdata, 'ydata', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
319 %! set (gca, 'xlim', xdata, 'ylim', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
320 %! axis on, hold on, xlabel ('Projection');
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
321 %! plot (outcp(:,1), outcp(:,2), 'or')
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
322
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
323 %!demo
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
324 %! ## Streched image
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
325 %! rad = 2; % minimum value: 4/pi
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
326 %! [uu vv] = meshgrid ((-2:2)/rad, (-2:2)/rad);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
327 %! rescfactor = sin ((uu.^2 + vv.^2).^.5);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
328 %! inpts = [(reshape (uu, numel (uu), 1)), (reshape (vv, numel (uu), 1))];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
329 %! xx = rescfactor .* sign(uu);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
330 %! yy = rescfactor .* sign(vv);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
331 %! outpts = [reshape(xx, numel (xx), 1) reshape(yy, numel (yy), 1)];
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
332 %!
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
333 %! T = cp2tform (inpts, outpts, "polynomial", 4);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
334 %! figure;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
335 %! subplot (1,2,1)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
336 %! im = zeros (800, 800, 3);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
337 %! im(:,:,1) = checkerboard (100) > 0.2;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
338 %! im(:,:,3) = checkerboard (100) < 0.2;
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
339 %! [im2 xdata ydata] = imtransform (im, T, 'udata', [-2 2],
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
340 %! 'vdata', [-2 2], 'fillvalues',
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
341 %! [0 1 0]);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
342 %! imh = imshow (im2);
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
343 %! set (imh, 'xdata', xdata, 'ydata', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
344 %! set (gca, 'xlim', xdata, 'ylim', ydata)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
345 %! [im cmap] = imread ('default.img');
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
346 %! subplot (1,2,2)
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
347 %! [im2 xdata ydata] = imtransform (im, T, 'udata', [-1 1],
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
348 %! 'vdata', [-1 1], 'fillvalues',
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
349 %! round (length (cmap) / 2));
fbd81057dab0 image: new spatial transformation functions
carandraug
parents:
diff changeset
350 %! imh = imshow (im2, cmap);