1 /*
2  * Software License Agreement (BSD License)
3  *
4  *  Copyright (c) 2009, Willow Garage, Inc.
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
11  *   * Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *   * Redistributions in binary form must reproduce the above
14  *     copyright notice, this list of conditions and the following
15  *     disclaimer in the documentation and/or other materials provided
16  *     with the distribution.
17  *   * Neither the name of Willow Garage, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  *  POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #ifndef __OPENCV_SFM_FUNDAMENTAL_HPP__
37 #define __OPENCV_SFM_FUNDAMENTAL_HPP__
38 
39 #include <vector>
40 
41 #include <opencv2/core.hpp>
42 
43 namespace cv
44 {
45 namespace sfm
46 {
47 
48 //! @addtogroup fundamental
49 //! @{
50 
51 /** @brief Get projection matrices from Fundamental matrix
52   @param F Input 3x3 fundamental matrix.
53   @param P1 Output 3x4 one possible projection matrix.
54   @param P2 Output 3x4 another possible projection matrix.
55  */
56 CV_EXPORTS_W
57 void
58 projectionsFromFundamental( InputArray F,
59                             OutputArray P1,
60                             OutputArray P2 );
61 
62 /** @brief Get Fundamental matrix from Projection matrices.
63   @param P1 Input 3x4 first projection matrix.
64   @param P2 Input 3x4 second projection matrix.
65   @param F Output 3x3 fundamental matrix.
66  */
67 CV_EXPORTS_W
68 void
69 fundamentalFromProjections( InputArray P1,
70                             InputArray P2,
71                             OutputArray F );
72 
73 /** @brief Estimate the fundamental matrix between two dataset of 2D point (image coords space).
74   @param x1 Input 2xN Array of 2D points in view 1.
75   @param x2 Input 2xN Array of 2D points in view 2.
76   @param F Output 3x3 fundamental matrix.
77 
78   Uses the normalized 8-point fundamental matrix solver.
79   Reference: @cite HartleyZ00 11.2 pag.281 (x1 = x, x2 = x')
80  */
81 CV_EXPORTS_W
82 void
83 normalizedEightPointSolver( InputArray x1,
84                             InputArray x2,
85                             OutputArray F );
86 
87 /** @brief Computes the relative camera motion between two cameras.
88   @param R1 Input 3x3 first camera rotation matrix.
89   @param t1 Input 3x1 first camera translation vector.
90   @param R2 Input 3x3 second camera rotation matrix.
91   @param t2 Input 3x1 second camera translation vector.
92   @param R Output 3x3 relative rotation matrix.
93   @param t Output 3x1 relative translation vector.
94 
95   Given the motion parameters of two cameras, computes the motion parameters
96   of the second one assuming the first one to be at the origin.
97   If T1 and T2 are the camera motions, the computed relative motion is \f$T = T_2 T_1^{-1}\f$
98  */
99 CV_EXPORTS_W
100 void
101 relativeCameraMotion( InputArray R1,
102                       InputArray t1,
103                       InputArray R2,
104                       InputArray t2,
105                       OutputArray R,
106                       OutputArray t );
107 
108 /** Get Motion (R's and t's ) from Essential matrix.
109   @param E Input 3x3 essential matrix.
110   @param Rs Output vector of 3x3 rotation matrices.
111   @param ts Output vector of 3x1 translation vectors.
112 
113   Reference: @cite HartleyZ00 9.6 pag 259 (Result 9.19)
114  */
115 CV_EXPORTS_W
116 void
117 motionFromEssential( InputArray E,
118                      OutputArrayOfArrays Rs,
119                      OutputArrayOfArrays ts );
120 
121 /** Choose one of the four possible motion solutions from an essential matrix.
122   @param Rs Input vector of 3x3 rotation matrices.
123   @param ts Input vector of 3x1 translation vectors.
124   @param K1 Input 3x3 first camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$.
125   @param x1 Input 2x1 vector with first 2d point.
126   @param K2 Input 3x3 second camera matrix. The parameters are similar to K1.
127   @param x2 Input 2x1 vector with second 2d point.
128 
129   Decides the right solution by checking that the triangulation of a match
130   x1--x2 lies in front of the cameras. Return index of the right solution or -1 if no solution.
131 
132   Reference: See @cite HartleyZ00 9.6 pag 259 (9.6.3 Geometrical interpretation of the 4 solutions).
133  */
134 CV_EXPORTS_W
135 int motionFromEssentialChooseSolution( InputArrayOfArrays Rs,
136                                        InputArrayOfArrays ts,
137                                        InputArray K1,
138                                        InputArray x1,
139                                        InputArray K2,
140                                        InputArray x2 );
141 
142 /** @brief Get Essential matrix from Fundamental and Camera matrices.
143   @param E Input 3x3 essential matrix.
144   @param K1 Input 3x3 first camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$.
145   @param K2 Input 3x3 second camera matrix. The parameters are similar to K1.
146   @param F Output 3x3 fundamental matrix.
147 
148   Reference: @cite HartleyZ00 9.6 pag 257 (formula 9.12) or http://ai.stanford.edu/~birch/projective/node20.html
149  */
150 CV_EXPORTS_W
151 void
152 fundamentalFromEssential( InputArray E,
153                           InputArray K1,
154                           InputArray K2,
155                           OutputArray F );
156 
157 /** @brief Get Essential matrix from Fundamental and Camera matrices.
158   @param F Input 3x3 fundamental matrix.
159   @param K1 Input 3x3 first camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$.
160   @param K2 Input 3x3 second camera matrix. The parameters are similar to K1.
161   @param E Output 3x3 essential matrix.
162 
163   Reference: @cite HartleyZ00 9.6 pag 257 (formula 9.12)
164  */
165 CV_EXPORTS_W
166 void
167 essentialFromFundamental( InputArray F,
168                           InputArray K1,
169                           InputArray K2,
170                           OutputArray E );
171 
172 /** @brief Get Essential matrix from Motion (R's and t's ).
173   @param R1 Input 3x3 first camera rotation matrix.
174   @param t1 Input 3x1 first camera translation vector.
175   @param R2 Input 3x3 second camera rotation matrix.
176   @param t2 Input 3x1 second camera translation vector.
177   @param E Output 3x3 essential matrix.
178 
179   Reference: @cite HartleyZ00 9.6 pag 257 (formula 9.12)
180  */
181 CV_EXPORTS_W
182 void
183 essentialFromRt( InputArray R1,
184                  InputArray t1,
185                  InputArray R2,
186                  InputArray t2,
187                  OutputArray E );
188 
189 /** @brief Normalizes the Fundamental matrix.
190   @param F Input 3x3 fundamental matrix.
191   @param F_normalized Output 3x3 normalized fundamental matrix.
192 
193   By default divides the fundamental matrix by its L2 norm.
194  */
195 CV_EXPORTS_W
196 void
197 normalizeFundamental( InputArray F,
198                       OutputArray F_normalized );
199 
200 /** @brief Computes Absolute or Exterior Orientation (Pose Estimation) between 2 sets of 3D point.
201   @param x1 Input first 3xN or 2xN array of points.
202   @param x2 Input second 3xN or 2xN array of points.
203   @param R Output 3x3 computed rotation matrix.
204   @param t Output 3x1 computed translation vector.
205   @param s Output computed scale factor.
206 
207   Find the best transformation such that xp=projection*(s*R*x+t) (same as Pose Estimation, ePNP).
208   The routines below are only for the orthographic case for now.
209  */
210 CV_EXPORTS_W
211 void
212 computeOrientation( InputArrayOfArrays x1,
213                     InputArrayOfArrays x2,
214                     OutputArray R,
215                     OutputArray t,
216                     double s );
217 
218 //! @} sfm
219 
220 } /* namespace sfm */
221 } /* namespace cv */
222 
223 #endif
224 
225 /* End of file. */
226