Add next and previous navigation links to all tutorials
[platform/upstream/opencv.git] / doc / tutorials / features2d / homography / homography.markdown
1 Basic concepts of the homography explained with code {#tutorial_homography}
2 ====================================================
3
4 @prev_tutorial{tutorial_akaze_tracking}
5
6 @tableofcontents
7
8 Introduction {#tutorial_homography_Introduction}
9 ============
10
11 This tutorial will demonstrate the basic concepts of the homography with some codes.
12 For detailed explanations about the theory, please refer to a computer vision course or a computer vision book, e.g.:
13 *   Multiple View Geometry in Computer Vision, @cite HartleyZ00.
14 *   An Invitation to 3-D Vision: From Images to Geometric Models, @cite Ma:2003:IVI
15 *   Computer Vision: Algorithms and Applications, @cite RS10
16
17 The tutorial code can be found here [C++](https://github.com/opencv/opencv/tree/3.4/samples/cpp/tutorial_code/features2D/Homography),
18 [Python](https://github.com/opencv/opencv/tree/3.4/samples/python/tutorial_code/features2D/Homography),
19 [Java](https://github.com/opencv/opencv/tree/3.4/samples/java/tutorial_code/features2D/Homography).
20 The images used in this tutorial can be found [here](https://github.com/opencv/opencv/tree/3.4/samples/data) (`left*.jpg`).
21
22 Basic theory {#tutorial_homography_Basic_theory}
23 ------------
24
25 ### What is the homography matrix? {#tutorial_homography_What_is_the_homography_matrix}
26
27 Briefly, the planar homography relates the transformation between two planes (up to a scale factor):
28
29 \f[
30   s
31   \begin{bmatrix}
32   x^{'} \\
33   y^{'} \\
34   1
35   \end{bmatrix} = H
36   \begin{bmatrix}
37   x \\
38   y \\
39   1
40   \end{bmatrix} =
41   \begin{bmatrix}
42   h_{11} & h_{12} & h_{13} \\
43   h_{21} & h_{22} & h_{23} \\
44   h_{31} & h_{32} & h_{33}
45   \end{bmatrix}
46   \begin{bmatrix}
47   x \\
48   y \\
49   1
50   \end{bmatrix}
51 \f]
52
53 The homography matrix is a `3x3` matrix but with 8 DoF (degrees of freedom) as it is estimated up to a scale. It is generally normalized (see also \ref lecture_16 "1")
54 with \f$ h_{33} = 1 \f$ or \f$ h_{11}^2 + h_{12}^2 + h_{13}^2 + h_{21}^2 + h_{22}^2 + h_{23}^2 + h_{31}^2 + h_{32}^2 + h_{33}^2 = 1 \f$.
55
56 The following examples show different kinds of transformation but all relate a transformation between two planes.
57
58 *   a planar surface and the image plane (image taken from \ref projective_transformations "2")
59
60 ![](images/homography_transformation_example1.jpg)
61
62 *   a planar surface viewed by two camera positions (images taken from \ref szeliski "3" and \ref projective_transformations "2")
63
64 ![](images/homography_transformation_example2.jpg)
65
66 *   a rotating camera around its axis of projection, equivalent to consider that the points are on a plane at infinity (image taken from \ref projective_transformations "2")
67
68 ![](images/homography_transformation_example3.jpg)
69
70 ### How the homography transformation can be useful? {#tutorial_homography_How_the_homography_transformation_can_be_useful}
71
72 *   Camera pose estimation from coplanar points for augmented reality with marker for instance (see the previous first example)
73
74 ![](images/homography_pose_estimation.jpg)
75
76 *   Perspective removal / correction (see the previous second example)
77
78 ![](images/homography_perspective_correction.jpg)
79
80 *   Panorama stitching (see the previous second and third example)
81
82 ![](images/homography_panorama_stitching.jpg)
83
84 Demonstration codes {#tutorial_homography_Demonstration_codes}
85 -------------------
86
87 ### Demo 1: Pose estimation from coplanar points {#tutorial_homography_Demo1}
88
89 \note Please note that the code to estimate the camera pose from the homography is an example and you should use instead @ref cv::solvePnP if you want to estimate the camera pose for a planar or an arbitrary object.
90
91 The homography can be estimated using for instance the Direct Linear Transform (DLT) algorithm (see \ref lecture_16 "1" for more information).
92 As the object is planar, the transformation between points expressed in the object frame and projected points into the image plane expressed in the normalized camera frame is a homography. Only because the object is planar,
93 the camera pose can be retrieved from the homography, assuming the camera intrinsic parameters are known (see \ref projective_transformations "2" or \ref answer_dsp "4").
94 This can be tested easily using a chessboard object and `findChessboardCorners()` to get the corner locations in the image.
95
96 The first thing consists to detect the chessboard corners, the chessboard size (`patternSize`), here `9x6`, is required:
97
98 @snippet pose_from_homography.cpp find-chessboard-corners
99
100 ![](images/homography_pose_chessboard_corners.jpg)
101
102 The object points expressed in the object frame can be computed easily knowing the size of a chessboard square:
103
104 @snippet pose_from_homography.cpp compute-chessboard-object-points
105
106 The coordinate `Z=0` must be removed for the homography estimation part:
107
108 @snippet pose_from_homography.cpp compute-object-points
109
110 The image points expressed in the normalized camera can be computed from the corner points and by applying a reverse perspective transformation using the camera intrinsics and the distortion coefficients:
111
112 @snippet pose_from_homography.cpp load-intrinsics
113
114 @snippet pose_from_homography.cpp compute-image-points
115
116 The homography can then be estimated with:
117
118 @snippet pose_from_homography.cpp estimate-homography
119
120 A quick solution to retrieve the pose from the homography matrix is (see \ref pose_ar "5"):
121
122 @snippet pose_from_homography.cpp pose-from-homography
123
124 \f[
125   \begin{align*}
126   \boldsymbol{X} &= \left( X, Y, 0, 1 \right ) \\
127   \boldsymbol{x} &= \boldsymbol{P}\boldsymbol{X} \\
128                  &= \boldsymbol{K} \left[ \boldsymbol{r_1} \hspace{0.5em} \boldsymbol{r_2} \hspace{0.5em} \boldsymbol{r_3} \hspace{0.5em} \boldsymbol{t} \right ]
129   \begin{pmatrix}
130   X \\
131   Y \\
132   0 \\
133   1
134   \end{pmatrix} \\
135              &= \boldsymbol{K} \left[ \boldsymbol{r_1} \hspace{0.5em} \boldsymbol{r_2} \hspace{0.5em} \boldsymbol{t} \right ]
136   \begin{pmatrix}
137   X \\
138   Y \\
139   1
140   \end{pmatrix} \\
141   &= \boldsymbol{H}
142   \begin{pmatrix}
143   X \\
144   Y \\
145   1
146   \end{pmatrix}
147   \end{align*}
148 \f]
149
150 \f[
151   \begin{align*}
152   \boldsymbol{H} &= \lambda \boldsymbol{K} \left[ \boldsymbol{r_1} \hspace{0.5em} \boldsymbol{r_2} \hspace{0.5em} \boldsymbol{t} \right ] \\
153   \boldsymbol{K}^{-1} \boldsymbol{H} &= \lambda \left[ \boldsymbol{r_1} \hspace{0.5em} \boldsymbol{r_2} \hspace{0.5em} \boldsymbol{t} \right ] \\
154   \boldsymbol{P} &= \boldsymbol{K} \left[ \boldsymbol{r_1} \hspace{0.5em} \boldsymbol{r_2} \hspace{0.5em} \left( \boldsymbol{r_1} \times \boldsymbol{r_2} \right ) \hspace{0.5em} \boldsymbol{t} \right ]
155   \end{align*}
156 \f]
157
158 This is a quick solution (see also \ref projective_transformations "2") as this does not ensure that the resulting rotation matrix will be orthogonal and the scale is estimated roughly by normalize the first column to 1.
159
160 A solution to have a proper rotation matrix (with the properties of a rotation matrix) consists to apply a polar decomposition
161 (see \ref polar_decomposition "6" or \ref polar_decomposition_svd "7" for some information):
162
163 @snippet pose_from_homography.cpp polar-decomposition-of-the-rotation-matrix
164
165 To check the result, the object frame projected into the image with the estimated camera pose is displayed:
166
167 ![](images/homography_pose.jpg)
168
169 ### Demo 2: Perspective correction {#tutorial_homography_Demo2}
170
171 In this example, a source image will be transformed into a desired perspective view by computing the homography that maps the source points into the desired points.
172 The following image shows the source image (left) and the chessboard view that we want to transform into the desired chessboard view (right).
173
174 ![Source and desired views](images/homography_source_desired_images.jpg)
175
176 The first step consists to detect the chessboard corners in the source and desired images:
177
178 @add_toggle_cpp
179 @snippet perspective_correction.cpp find-corners
180 @end_toggle
181
182 @add_toggle_python
183 @snippet samples/python/tutorial_code/features2D/Homography/perspective_correction.py find-corners
184 @end_toggle
185
186 @add_toggle_java
187 @snippet samples/java/tutorial_code/features2D/Homography/PerspectiveCorrection.java find-corners
188 @end_toggle
189
190 The homography is estimated easily with:
191
192 @add_toggle_cpp
193 @snippet perspective_correction.cpp estimate-homography
194 @end_toggle
195
196 @add_toggle_python
197 @snippet samples/python/tutorial_code/features2D/Homography/perspective_correction.py estimate-homography
198 @end_toggle
199
200 @add_toggle_java
201 @snippet samples/java/tutorial_code/features2D/Homography/PerspectiveCorrection.java estimate-homography
202 @end_toggle
203
204 To warp the source chessboard view into the desired chessboard view, we use @ref cv::warpPerspective
205
206 @add_toggle_cpp
207 @snippet perspective_correction.cpp warp-chessboard
208 @end_toggle
209
210 @add_toggle_python
211 @snippet samples/python/tutorial_code/features2D/Homography/perspective_correction.py warp-chessboard
212 @end_toggle
213
214 @add_toggle_java
215 @snippet samples/java/tutorial_code/features2D/Homography/PerspectiveCorrection.java warp-chessboard
216 @end_toggle
217
218 The result image is:
219
220 ![](images/homography_perspective_correction_chessboard_warp.jpg)
221
222 To compute the coordinates of the source corners transformed by the homography:
223
224 @add_toggle_cpp
225 @snippet perspective_correction.cpp compute-transformed-corners
226 @end_toggle
227
228 @add_toggle_python
229 @snippet samples/python/tutorial_code/features2D/Homography/perspective_correction.py compute-transformed-corners
230 @end_toggle
231
232 @add_toggle_java
233 @snippet samples/java/tutorial_code/features2D/Homography/PerspectiveCorrection.java compute-transformed-corners
234 @end_toggle
235
236 To check the correctness of the calculation, the matching lines are displayed:
237
238 ![](images/homography_perspective_correction_chessboard_matches.jpg)
239
240 ### Demo 3: Homography from the camera displacement {#tutorial_homography_Demo3}
241
242 The homography relates the transformation between two planes and it is possible to retrieve the corresponding camera displacement that allows to go from the first to the second plane view (see @cite Malis for more information).
243 Before going into the details that allow to compute the homography from the camera displacement, some recalls about camera pose and homogeneous transformation.
244
245 The function @ref cv::solvePnP allows to compute the camera pose from the correspondences 3D object points (points expressed in the object frame) and the projected 2D image points (object points viewed in the image).
246 The intrinsic parameters and the distortion coefficients are required (see the camera calibration process).
247
248 \f[
249   \begin{align*}
250   s
251   \begin{bmatrix}
252   u \\
253   v \\
254   1
255   \end{bmatrix} &=
256   \begin{bmatrix}
257   f_x & 0 & c_x \\
258   0 & f_y & c_y \\
259   0 & 0 & 1
260   \end{bmatrix}
261   \begin{bmatrix}
262   r_{11} & r_{12} & r_{13} & t_x \\
263   r_{21} & r_{22} & r_{23} & t_y \\
264   r_{31} & r_{32} & r_{33} & t_z
265   \end{bmatrix}
266   \begin{bmatrix}
267   X_o \\
268   Y_o \\
269   Z_o \\
270   1
271   \end{bmatrix} \\
272   &= \boldsymbol{K} \hspace{0.2em} ^{c}\textrm{M}_o
273   \begin{bmatrix}
274   X_o \\
275   Y_o \\
276   Z_o \\
277   1
278   \end{bmatrix}
279   \end{align*}
280 \f]
281
282 \f$ \boldsymbol{K} \f$ is the intrinsic matrix and \f$ ^{c}\textrm{M}_o \f$ is the camera pose. The output of @ref cv::solvePnP is exactly this: `rvec` is the Rodrigues rotation vector and `tvec` the translation vector.
283
284 \f$ ^{c}\textrm{M}_o \f$ can be represented in a homogeneous form and allows to transform a point expressed in the object frame into the camera frame:
285
286 \f[
287   \begin{align*}
288   \begin{bmatrix}
289   X_c \\
290   Y_c \\
291   Z_c \\
292   1
293   \end{bmatrix} &=
294   \hspace{0.2em} ^{c}\textrm{M}_o
295   \begin{bmatrix}
296   X_o \\
297   Y_o \\
298   Z_o \\
299   1
300   \end{bmatrix} \\
301   &=
302   \begin{bmatrix}
303   ^{c}\textrm{R}_o & ^{c}\textrm{t}_o \\
304   0_{1\times3} & 1
305   \end{bmatrix}
306   \begin{bmatrix}
307   X_o \\
308   Y_o \\
309   Z_o \\
310   1
311   \end{bmatrix} \\
312   &=
313   \begin{bmatrix}
314   r_{11} & r_{12} & r_{13} & t_x \\
315   r_{21} & r_{22} & r_{23} & t_y \\
316   r_{31} & r_{32} & r_{33} & t_z \\
317   0 & 0 & 0 & 1
318   \end{bmatrix}
319   \begin{bmatrix}
320   X_o \\
321   Y_o \\
322   Z_o \\
323   1
324   \end{bmatrix}
325   \end{align*}
326 \f]
327
328 Transform a point expressed in one frame to another frame can be easily done with matrix multiplication:
329
330 *   \f$ ^{c_1}\textrm{M}_o \f$ is the camera pose for the camera 1
331 *   \f$ ^{c_2}\textrm{M}_o \f$ is the camera pose for the camera 2
332
333 To transform a 3D point expressed in the camera 1 frame to the camera 2 frame:
334
335 \f[
336   ^{c_2}\textrm{M}_{c_1} = \hspace{0.2em} ^{c_2}\textrm{M}_{o} \cdot \hspace{0.1em} ^{o}\textrm{M}_{c_1} = \hspace{0.2em} ^{c_2}\textrm{M}_{o} \cdot \hspace{0.1em} \left( ^{c_1}\textrm{M}_{o} \right )^{-1} =
337   \begin{bmatrix}
338   ^{c_2}\textrm{R}_{o} & ^{c_2}\textrm{t}_{o} \\
339   0_{3 \times 1} & 1
340   \end{bmatrix} \cdot
341   \begin{bmatrix}
342   ^{c_1}\textrm{R}_{o}^T & - \hspace{0.2em} ^{c_1}\textrm{R}_{o}^T \cdot \hspace{0.2em} ^{c_1}\textrm{t}_{o} \\
343   0_{1 \times 3} & 1
344   \end{bmatrix}
345 \f]
346
347 In this example, we will compute the camera displacement between two camera poses with respect to the chessboard object. The first step consists to compute the camera poses for the two images:
348
349 @snippet homography_from_camera_displacement.cpp compute-poses
350
351 ![](images/homography_camera_displacement_poses.jpg)
352
353 The camera displacement can be computed from the camera poses using the formulas above:
354
355 @snippet homography_from_camera_displacement.cpp compute-c2Mc1
356
357 The homography related to a specific plane computed from the camera displacement is:
358
359 ![By Homography-transl.svg: Per Rosengren derivative work: Appoose (Homography-transl.svg) [CC BY 3.0 (http://creativecommons.org/licenses/by/3.0)], via Wikimedia Commons](images/homography_camera_displacement.png)
360
361 On this figure, `n` is the normal vector of the plane and `d` the distance between the camera frame and the plane along the plane normal.
362 The [equation](https://en.wikipedia.org/wiki/Homography_(computer_vision)#3D_plane_to_plane_equation) to compute the homography from the camera displacement is:
363
364 \f[
365   ^{2}\textrm{H}_{1} = \hspace{0.2em} ^{2}\textrm{R}_{1} - \hspace{0.1em} \frac{^{2}\textrm{t}_{1} \cdot n^T}{d}
366 \f]
367
368 Where \f$ ^{2}\textrm{H}_{1} \f$ is the homography matrix that maps the points in the first camera frame to the corresponding points in the second camera frame, \f$ ^{2}\textrm{R}_{1} = \hspace{0.2em} ^{c_2}\textrm{R}_{o} \cdot \hspace{0.1em} ^{c_1}\textrm{R}_{o}^{T} \f$
369 is the rotation matrix that represents the rotation between the two camera frames and \f$ ^{2}\textrm{t}_{1} = \hspace{0.2em} ^{c_2}\textrm{R}_{o} \cdot \left( - \hspace{0.1em} ^{c_1}\textrm{R}_{o}^{T} \cdot \hspace{0.1em} ^{c_1}\textrm{t}_{o} \right ) + \hspace{0.1em} ^{c_2}\textrm{t}_{o} \f$
370 the translation vector between the two camera frames.
371
372 Here the normal vector `n` is the plane normal expressed in the camera frame 1 and can be computed as the cross product of 2 vectors (using 3 non collinear points that lie on the plane) or in our case directly with:
373
374 @snippet homography_from_camera_displacement.cpp compute-plane-normal-at-camera-pose-1
375
376 The distance `d` can be computed as the dot product between the plane normal and a point on the plane or by computing the [plane equation](http://mathworld.wolfram.com/Plane.html) and using the D coefficient:
377
378 @snippet homography_from_camera_displacement.cpp compute-plane-distance-to-the-camera-frame-1
379
380 The projective homography matrix \f$ \textbf{G} \f$ can be computed from the Euclidean homography \f$ \textbf{H} \f$ using the intrinsic matrix \f$ \textbf{K} \f$ (see @cite Malis), here assuming the same camera between the two plane views:
381
382 \f[
383   \textbf{G} = \gamma \textbf{K} \textbf{H} \textbf{K}^{-1}
384 \f]
385
386 @snippet homography_from_camera_displacement.cpp compute-homography
387
388 In our case, the Z-axis of the chessboard goes inside the object whereas in the homography figure it goes outside. This is just a matter of sign:
389
390 \f[
391   ^{2}\textrm{H}_{1} = \hspace{0.2em} ^{2}\textrm{R}_{1} + \hspace{0.1em} \frac{^{2}\textrm{t}_{1} \cdot n^T}{d}
392 \f]
393
394 @snippet homography_from_camera_displacement.cpp compute-homography-from-camera-displacement
395
396 We will now compare the projective homography computed from the camera displacement with the one estimated with @ref cv::findHomography
397
398 ```
399 findHomography H:
400 [0.32903393332201, -1.244138808862929, 536.4769088231476;
401  0.6969763913334046, -0.08935909072571542, -80.34068504082403;
402  0.00040511729592961, -0.001079740100565013, 0.9999999999999999]
403
404 homography from camera displacement:
405 [0.4160569997384721, -1.306889006892538, 553.7055461075881;
406  0.7917584252773352, -0.06341244158456338, -108.2770029401219;
407  0.0005926357240956578, -0.001020651672127799, 1]
408
409 ```
410
411 The homography matrices are similar. If we compare the image 1 warped using both homography matrices:
412
413 ![Left: image warped using the homography estimated. Right: using the homography computed from the camera displacement](images/homography_camera_displacement_compare.jpg)
414
415 Visually, it is hard to distinguish a difference between the result image from the homography computed from the camera displacement and the one estimated with @ref cv::findHomography function.
416
417 ### Demo 4: Decompose the homography matrix {#tutorial_homography_Demo4}
418
419 OpenCV 3 contains the function @ref cv::decomposeHomographyMat which allows to decompose the homography matrix to a set of rotations, translations and plane normals.
420 First we will decompose the homography matrix computed from the camera displacement:
421
422 @snippet decompose_homography.cpp compute-homography-from-camera-displacement
423
424 The results of @ref cv::decomposeHomographyMat are:
425
426 @snippet decompose_homography.cpp decompose-homography-from-camera-displacement
427
428 ```
429 Solution 0:
430 rvec from homography decomposition: [-0.0919829920641369, -0.5372581036567992, 1.310868863540717]
431 rvec from camera displacement: [-0.09198299206413783, -0.5372581036567995, 1.310868863540717]
432 tvec from homography decomposition: [-0.7747961019053186, -0.02751124463434032, -0.6791980037590677] and scaled by d: [-0.1578091561210742, -0.005603443652993778, -0.1383378976078466]
433 tvec from camera displacement: [0.1578091561210745, 0.005603443652993617, 0.1383378976078466]
434 plane normal from homography decomposition: [-0.1973513139420648, 0.6283451996579074, -0.7524857267431757]
435 plane normal at camera 1 pose: [0.1973513139420654, -0.6283451996579068, 0.752485726743176]
436
437 Solution 1:
438 rvec from homography decomposition: [-0.0919829920641369, -0.5372581036567992, 1.310868863540717]
439 rvec from camera displacement: [-0.09198299206413783, -0.5372581036567995, 1.310868863540717]
440 tvec from homography decomposition: [0.7747961019053186, 0.02751124463434032, 0.6791980037590677] and scaled by d: [0.1578091561210742, 0.005603443652993778, 0.1383378976078466]
441 tvec from camera displacement: [0.1578091561210745, 0.005603443652993617, 0.1383378976078466]
442 plane normal from homography decomposition: [0.1973513139420648, -0.6283451996579074, 0.7524857267431757]
443 plane normal at camera 1 pose: [0.1973513139420654, -0.6283451996579068, 0.752485726743176]
444
445 Solution 2:
446 rvec from homography decomposition: [0.1053487907109967, -0.1561929144786397, 1.401356552358475]
447 rvec from camera displacement: [-0.09198299206413783, -0.5372581036567995, 1.310868863540717]
448 tvec from homography decomposition: [-0.4666552552894618, 0.1050032934770042, -0.913007654671646] and scaled by d: [-0.0950475510338766, 0.02138689274867372, -0.1859598508065552]
449 tvec from camera displacement: [0.1578091561210745, 0.005603443652993617, 0.1383378976078466]
450 plane normal from homography decomposition: [-0.3131715472900788, 0.8421206145721947, -0.4390403768225507]
451 plane normal at camera 1 pose: [0.1973513139420654, -0.6283451996579068, 0.752485726743176]
452
453 Solution 3:
454 rvec from homography decomposition: [0.1053487907109967, -0.1561929144786397, 1.401356552358475]
455 rvec from camera displacement: [-0.09198299206413783, -0.5372581036567995, 1.310868863540717]
456 tvec from homography decomposition: [0.4666552552894618, -0.1050032934770042, 0.913007654671646] and scaled by d: [0.0950475510338766, -0.02138689274867372, 0.1859598508065552]
457 tvec from camera displacement: [0.1578091561210745, 0.005603443652993617, 0.1383378976078466]
458 plane normal from homography decomposition: [0.3131715472900788, -0.8421206145721947, 0.4390403768225507]
459 plane normal at camera 1 pose: [0.1973513139420654, -0.6283451996579068, 0.752485726743176]
460 ```
461
462 The result of the decomposition of the homography matrix can only be recovered up to a scale factor that corresponds in fact to the distance `d` as the normal is unit length.
463 As you can see, there is one solution that matches almost perfectly with the computed camera displacement. As stated in the documentation:
464
465 ```
466 At least two of the solutions may further be invalidated if point correspondences are available by applying positive depth constraint (all points must be in front of the camera).
467 ```
468
469 As the result of the decomposition is a camera displacement, if we have the initial camera pose \f$ ^{c_1}\textrm{M}_{o} \f$, we can compute the current camera pose
470 \f$ ^{c_2}\textrm{M}_{o} = \hspace{0.2em} ^{c_2}\textrm{M}_{c_1} \cdot \hspace{0.1em} ^{c_1}\textrm{M}_{o} \f$ and test if the 3D object points that belong to the plane are projected in front of the camera or not.
471 Another solution could be to retain the solution with the closest normal if we know the plane normal expressed at the camera 1 pose.
472
473 The same thing but with the homography matrix estimated with @ref cv::findHomography
474
475 ```
476 Solution 0:
477 rvec from homography decomposition: [0.1552207729599141, -0.152132696119647, 1.323678695078694]
478 rvec from camera displacement: [-0.09198299206413783, -0.5372581036567995, 1.310868863540717]
479 tvec from homography decomposition: [-0.4482361704818117, 0.02485247635491922, -1.034409687207331] and scaled by d: [-0.09129598307571339, 0.005061910238634657, -0.2106868109173855]
480 tvec from camera displacement: [0.1578091561210745, 0.005603443652993617, 0.1383378976078466]
481 plane normal from homography decomposition: [-0.1384902722707529, 0.9063331452766947, -0.3992250922214516]
482 plane normal at camera 1 pose: [0.1973513139420654, -0.6283451996579068, 0.752485726743176]
483
484 Solution 1:
485 rvec from homography decomposition: [0.1552207729599141, -0.152132696119647, 1.323678695078694]
486 rvec from camera displacement: [-0.09198299206413783, -0.5372581036567995, 1.310868863540717]
487 tvec from homography decomposition: [0.4482361704818117, -0.02485247635491922, 1.034409687207331] and scaled by d: [0.09129598307571339, -0.005061910238634657, 0.2106868109173855]
488 tvec from camera displacement: [0.1578091561210745, 0.005603443652993617, 0.1383378976078466]
489 plane normal from homography decomposition: [0.1384902722707529, -0.9063331452766947, 0.3992250922214516]
490 plane normal at camera 1 pose: [0.1973513139420654, -0.6283451996579068, 0.752485726743176]
491
492 Solution 2:
493 rvec from homography decomposition: [-0.2886605671759886, -0.521049903923871, 1.381242030882511]
494 rvec from camera displacement: [-0.09198299206413783, -0.5372581036567995, 1.310868863540717]
495 tvec from homography decomposition: [-0.8705961357284295, 0.1353018038908477, -0.7037702049789747] and scaled by d: [-0.177321544550518, 0.02755804196893467, -0.1433427218822783]
496 tvec from camera displacement: [0.1578091561210745, 0.005603443652993617, 0.1383378976078466]
497 plane normal from homography decomposition: [-0.2284582117722427, 0.6009247303964522, -0.7659610393954643]
498 plane normal at camera 1 pose: [0.1973513139420654, -0.6283451996579068, 0.752485726743176]
499
500 Solution 3:
501 rvec from homography decomposition: [-0.2886605671759886, -0.521049903923871, 1.381242030882511]
502 rvec from camera displacement: [-0.09198299206413783, -0.5372581036567995, 1.310868863540717]
503 tvec from homography decomposition: [0.8705961357284295, -0.1353018038908477, 0.7037702049789747] and scaled by d: [0.177321544550518, -0.02755804196893467, 0.1433427218822783]
504 tvec from camera displacement: [0.1578091561210745, 0.005603443652993617, 0.1383378976078466]
505 plane normal from homography decomposition: [0.2284582117722427, -0.6009247303964522, 0.7659610393954643]
506 plane normal at camera 1 pose: [0.1973513139420654, -0.6283451996579068, 0.752485726743176]
507 ```
508
509 Again, there is also a solution that matches with the computed camera displacement.
510
511 ### Demo 5: Basic panorama stitching from a rotating camera {#tutorial_homography_Demo5}
512
513 \note This example is made to illustrate the concept of image stitching based on a pure rotational motion of the camera and should not be used to stitch panorama images.
514 The [stitching module](@ref stitching) provides a complete pipeline to stitch images.
515
516 The homography transformation applies only for planar structure. But in the case of a rotating camera (pure rotation around the camera axis of projection, no translation), an arbitrary world can be considered
517 ([see previously](@ref tutorial_homography_What_is_the_homography_matrix)).
518
519 The homography can then be computed using the rotation transformation and the camera intrinsic parameters as (see for instance \ref homography_course "8"):
520
521 \f[
522   s
523   \begin{bmatrix}
524   x^{'} \\
525   y^{'} \\
526   1
527   \end{bmatrix} =
528   \bf{K} \hspace{0.1em} \bf{R} \hspace{0.1em} \bf{K}^{-1}
529   \begin{bmatrix}
530   x \\
531   y \\
532   1
533   \end{bmatrix}
534 \f]
535
536 To illustrate, we used Blender, a free and open-source 3D computer graphics software, to generate two camera views with only a rotation transformation between each other.
537 More information about how to retrieve the camera intrinsic parameters and the `3x4` extrinsic matrix with respect to the world can be found in \ref answer_blender "9" (an additional transformation
538 is needed to get the transformation between the camera and the object frames) with Blender.
539
540 The figure below shows the two generated views of the Suzanne model, with only a rotation transformation:
541
542 ![](images/homography_stitch_compare.jpg)
543
544 With the known associated camera poses and the intrinsic parameters, the relative rotation between the two views can be computed:
545
546 @add_toggle_cpp
547 @snippet panorama_stitching_rotating_camera.cpp extract-rotation
548 @end_toggle
549
550 @add_toggle_python
551 @snippet samples/python/tutorial_code/features2D/Homography/panorama_stitching_rotating_camera.py extract-rotation
552 @end_toggle
553
554 @add_toggle_java
555 @snippet samples/java/tutorial_code/features2D/Homography/PanoramaStitchingRotatingCamera.java extract-rotation
556 @end_toggle
557
558 @add_toggle_cpp
559 @snippet panorama_stitching_rotating_camera.cpp compute-rotation-displacement
560 @end_toggle
561
562 @add_toggle_python
563 @snippet samples/python/tutorial_code/features2D/Homography/panorama_stitching_rotating_camera.py compute-rotation-displacement
564 @end_toggle
565
566 @add_toggle_java
567 @snippet samples/java/tutorial_code/features2D/Homography/PanoramaStitchingRotatingCamera.java compute-rotation-displacement
568 @end_toggle
569
570 Here, the second image will be stitched with respect to the first image. The homography can be calculated using the formula above:
571
572 @add_toggle_cpp
573 @snippet panorama_stitching_rotating_camera.cpp compute-homography
574 @end_toggle
575
576 @add_toggle_python
577 @snippet samples/python/tutorial_code/features2D/Homography/panorama_stitching_rotating_camera.py compute-homography
578 @end_toggle
579
580 @add_toggle_java
581 @snippet samples/java/tutorial_code/features2D/Homography/PanoramaStitchingRotatingCamera.java compute-homography
582 @end_toggle
583
584 The stitching is made simply with:
585
586 @add_toggle_cpp
587 @snippet panorama_stitching_rotating_camera.cpp stitch
588 @end_toggle
589
590 @add_toggle_python
591 @snippet samples/python/tutorial_code/features2D/Homography/panorama_stitching_rotating_camera.py stitch
592 @end_toggle
593
594 @add_toggle_java
595 @snippet samples/java/tutorial_code/features2D/Homography/PanoramaStitchingRotatingCamera.java stitch
596 @end_toggle
597
598 The resulting image is:
599
600 ![](images/homography_stitch_Suzanne.jpg)
601
602 Additional references {#tutorial_homography_Additional_references}
603 ---------------------
604
605 *   \anchor lecture_16 1. [Lecture 16: Planar Homographies](http://www.cse.psu.edu/~rtc12/CSE486/lecture16.pdf), Robert Collins
606 *   \anchor projective_transformations 2. [2D projective transformations (homographies)](https://ags.cs.uni-kl.de/fileadmin/inf_ags/3dcv-ws11-12/3DCV_WS11-12_lec04.pdf), Christiano Gava, Gabriele Bleser
607 *   \anchor szeliski 3. [Computer Vision: Algorithms and Applications](http://szeliski.org/Book/drafts/SzeliskiBook_20100903_draft.pdf), Richard Szeliski
608 *   \anchor answer_dsp 4. [Step by Step Camera Pose Estimation for Visual Tracking and Planar Markers](https://dsp.stackexchange.com/a/2737)
609 *   \anchor pose_ar 5. [Pose from homography estimation](https://team.inria.fr/lagadic/camera_localization/tutorial-pose-dlt-planar-opencv.html)
610 *   \anchor polar_decomposition 6. [Polar Decomposition (in Continuum Mechanics)](http://www.continuummechanics.org/polardecomposition.html)
611 *   \anchor polar_decomposition_svd 7. [A Personal Interview with the Singular Value Decomposition](https://web.stanford.edu/~gavish/documents/SVD_ans_you.pdf), Matan Gavish
612 *   \anchor homography_course 8. [Homography](http://people.scs.carleton.ca/~c_shu/Courses/comp4900d/notes/homography.pdf), Dr. Gerhard Roth
613 *   \anchor answer_blender 9. [3x4 camera matrix from blender camera](https://blender.stackexchange.com/a/38210)