[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

projective_registration.hxx
1/************************************************************************/
2/* */
3/* Copyright 2007-2014 by Benjamin Seppke */
4/* */
5/* This file is part of the VIGRA computer vision library. */
6/* The VIGRA Website is */
7/* http://hci.iwr.uni-heidelberg.de/vigra/ */
8/* Please direct questions, bug reports, and contributions to */
9/* ullrich.koethe@iwr.uni-heidelberg.de or */
10/* vigra@informatik.uni-hamburg.de */
11/* */
12/* Permission is hereby granted, free of charge, to any person */
13/* obtaining a copy of this software and associated documentation */
14/* files (the "Software"), to deal in the Software without */
15/* restriction, including without limitation the rights to use, */
16/* copy, modify, merge, publish, distribute, sublicense, and/or */
17/* sell copies of the Software, and to permit persons to whom the */
18/* Software is furnished to do so, subject to the following */
19/* conditions: */
20/* */
21/* The above copyright notice and this permission notice shall be */
22/* included in all copies or substantial portions of the */
23/* Software. */
24/* */
25/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32/* OTHER DEALINGS IN THE SOFTWARE. */
33/* */
34/************************************************************************/
35
36#ifndef VIGRA_PROJECTIVE_REGISTRATION_HXX
37#define VIGRA_PROJECTIVE_REGISTRATION_HXX
38
39#include "mathutil.hxx"
40#include "matrix.hxx"
41#include "linear_solve.hxx"
42#include "tinyvector.hxx"
43#include "splineimageview.hxx"
44
45 namespace vigra
46{
47
48/** \addtogroup Registration Image Registration
49 */
50//@{
51
52/********************************************************/
53/* */
54/* projectiveMatrix2DFromCorrespondingPoints */
55/* */
56/********************************************************/
57
58/** \brief Create homogeneous matrix that maps corresponding points onto each other.
59
60 For use with \ref projectiveWarpImage(). Since four corresponding points are needed to be given,
61 the matrix will compute a full projective transform.
62 */
63 template <class SrcPointIterator, class DestPointIterator>
64linalg::TemporaryMatrix<double>
65 projectiveMatrix2DFromCorrespondingPoints(SrcPointIterator s, SrcPointIterator send, DestPointIterator d)
66{
67 //Calculate the matrix using least squares of all points of points like the result is:
68 // ( x2 ) ( s_x r1 t_x ) ( x1 )
69 // ( y2 ) = ( r2 s_y t_y ) * ( y1 )
70 // ( 1 ) ( p1 p2 1 ) ( 1 )
71 int size = send - s;
72
73 vigra_assert(size >= 4,
74 "projectiveMatrix2DFromCorrespondingPoints(): need at least four corresponding points.");
75
76 vigra::Matrix<double> A(2*size,8, 0.0), b(2*size,1), res(8,1);
77 for (int i =0; i<size; ++i, ++s, ++d)
78 {
79 //m_00 m_01 m_02 m_10 m_11 m_12 m_20 m_21
80 //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
81 A(i,0)=(*d)[0]; A(i,1)=(*d)[1]; A(i,2)=1; A(i,3)=0; A(i,4)=0; A(i,5)=0; A(i,6)=-1*((*d)[0])*((*s)[0]); A(i,7)=-1*((*d)[1])*((*s)[0]);
82 b(i,0)=(*s)[0];
83
84 A(size+i,0)=0; A(size+i,1)=0; A(size+i,2)=0; A(size+i,3)=(*d)[0]; A(size+i,4)=(*d)[1]; A(size+i,5)=1; A(size+i,6)=-1*((*d)[0])*((*s)[1]); A(size+i,7)=-1*((*d)[1])*((*s)[1]);
85 b(size+i,0)=(*s)[1];
86
87 }
88
89 bool solvable = linearSolve(A, b, res);
90 // silence unused variable warning in release mode
91 static_cast<void>(solvable);
92 vigra_assert(solvable,
93 "projectiveMatrix2DFromCorrespondingPoints(): singular solution matrix.");
94
95 linalg::TemporaryMatrix<double> projectiveMat(3,3);
96 projectiveMat(0,0) = res(0,0); projectiveMat(0,1) = res(1,0); projectiveMat(0,2) = res(2,0);
97 projectiveMat(1,0) = res(3,0); projectiveMat(1,1) = res(4,0); projectiveMat(1,2) = res(5,0);
98 projectiveMat(2,0) = res(6,0); projectiveMat(2,1) = res(7,0); projectiveMat(2,2) = 1;
99
100 return projectiveMat;
101}
102
103/********************************************************/
104/* */
105/* projectiveWarpImage */
106/* */
107/********************************************************/
108
109/** \brief Warp an image according to an projective transformation.
110
111 The matrix can be computed from a set of correspondung points
112 using \ref projectiveMatrix2DFromCorrespondingPoints().
113
114 <b> Declarations:</b>
115
116 <b>\#include</b> <vigra/projective_registration.hxx><br>
117 Namespace: vigra
118
119 pass 2D array views:
120 \code
121 namespace vigra {
122 template <int ORDER, class T,
123 class T2, class S2,
124 class C>
125 void
126 projectiveWarpImage(SplineImageView<ORDER, T> const & src,
127 MultiArrayView<2, T2, S2> dest,
128 MultiArrayView<2, double, C> const & projectiveMatrix);
129 }
130 \endcode
131
132 \deprecatedAPI{projectiveWarpImage}
133 pass \ref ImageIterators and \ref DataAccessors :
134
135 pass arguments explicitly:
136 \code
137 namespace vigra {
138 template <int ORDER, class T,
139 class DestIterator, class DestAccessor,
140 class C>
141 void projectiveWarpImage(SplineImageView<ORDER, T> const & src,
142 DestIterator dul, DestIterator dlr, DestAccessor dest,
143 MultiArrayView<2, double, C> const & projectiveMatrix);
144 }
145 \endcode
146
147 use argument objects in conjunction with \ref ArgumentObjectFactories :
148 \code
149 namespace vigra {
150 template <int ORDER, class T,
151 class DestIterator, class DestAccessor,
152 class C>
153 void projectiveWarpImage(SplineImageView<ORDER, T> const & src,
154 triple<DestIterator, DestIterator, DestAccessor> dest,
155 MultiArrayView<2, double, C> const & projectiveMatrix);
156 }
157 \endcode
158 \deprecatedEnd
159
160 The algorithm applies the given \a projectiveMatrix to the <i>destination coordinates</i> and copies
161 the image value from the resulting source coordinates, using the given SplineImageView \a src for interpolation.
162 If the resulting coordinate is outside the source image, nothing will be written at that destination point.
163
164 \code
165 for all dest pixels:
166 currentSrcCoordinate = projectiveMatrix * currentDestCoordinate;
167 if src.isInside(currentSrcCoordinate):
168 dest[currentDestCoordinate] = src[currentSrcCoordinate]; // copy an interpolated value
169 \endcode
170
171 The matrix represent a 2-dimensional projective transform by means of homogeneous coordinates,
172 i.e. it must be a 3x3 matrix whose last row is (p1,p2,1).
173
174 <b> Required Interface:</b>
175
176 \code
177 DestImageIterator dest_upperleft;
178
179 double x = ..., y = ...;
180
181 if (spline.isInside(x,y))
182 dest_accessor.set(spline(x, y), dest_upperleft);
183
184 \endcode
185
186 <b>See also:</b> Functions to specify projective transformation: \ref translationMatrix2D(), \ref scalingMatrix2D(),
187 \ref shearMatrix2D(), \ref rotationMatrix2DRadians(), \ref rotationMatrix2DDegrees() and \ref projectiveMatrix2DFromCorrespondingPoints()
188*/
189doxygen_overloaded_function(template <...> void projectiveWarpImage)
190
191 template <int ORDER, class T,
192 class DestIterator, class DestAccessor,
193 class C>
194 void projectiveWarpImage(SplineImageView<ORDER, T> const & src,
195 DestIterator dul, DestIterator dlr, DestAccessor dest,
196 MultiArrayView<2, double, C> const & projectiveMatrix)
197{
198 vigra_precondition(rowCount(projectiveMatrix) == 3 && columnCount(projectiveMatrix) == 3 && projectiveMatrix(2,2) == 1.0,
199 "projectiveWarpImage(): matrix doesn't represent an projective transformation with homogeneous 2D coordinates.");
200
201
202 double w = dlr.x - dul.x;
203 double h = dlr.y - dul.y;
204
205 for(double y = 0.0; y < h; ++y, ++dul.y)
206 {
207 typename DestIterator::row_iterator rd = dul.rowIterator();
208 for(double x=0.0; x < w; ++x, ++rd)
209 {
210 double fac = 1.0/(x*projectiveMatrix(2,0) + y*projectiveMatrix(2,1) + 1);
211 double sx = (x*projectiveMatrix(0,0) + y*projectiveMatrix(0,1) + projectiveMatrix(0,2)) * fac;
212 double sy = (x*projectiveMatrix(1,0) + y*projectiveMatrix(1,1) + projectiveMatrix(1,2)) * fac;
213 if(src.isInside(sx, sy))
214 dest.set(src(sx, sy), rd);
215 }
216 }
217}
218
219 template <int ORDER, class T,
220 class DestIterator, class DestAccessor,
221 class C>
222 inline
223 void projectiveWarpImage(SplineImageView<ORDER, T> const & src,
224 triple<DestIterator, DestIterator, DestAccessor> dest,
225 MultiArrayView<2, double, C> const & projectiveMatrix)
226{
227 projectiveWarpImage(src, dest.first, dest.second, dest.third, projectiveMatrix);
228}
229
230
231 template <int ORDER, class T,
232 class T2, class S2,
233 class C>
234 inline
235 void projectiveWarpImage(SplineImageView<ORDER, T> const & src,
236 MultiArrayView<2, T2, S2> dest,
237 MultiArrayView<2, double, C> const & projectiveMatrix)
238{
239 projectiveWarpImage(src, destImageRange(dest), projectiveMatrix);
240}
241
242
243//@}
244
245} // namespace vigra
246
247
248#endif /* VIGRA_PROJECTIVE_REGISTRATION_HXX */
vigra::RGBValue
Class for a single RGB value.
Definition rgbvalue.hxx:128
vigra
vigra::projectiveWarpImage
void projectiveWarpImage(...)
Warp an image according to an projective transformation.
vigra::projectiveMatrix2DFromCorrespondingPoints
linalg::TemporaryMatrix< double > projectiveMatrix2DFromCorrespondingPoints(SrcPointIterator s, SrcPointIterator send, DestPointIterator d)
Create homogeneous matrix that maps corresponding points onto each other.
Definition projective_registration.hxx:65

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.12.1