[4cca03]: contrib / gel / mrc / vpgl / algo / vpgl_camera_homographies.cxx Maximize Restore History

Download this file

vpgl_camera_homographies.cxx    67 lines (61 with data), 2.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <vpgl/algo/vpgl_camera_homographies.h>
#include <vgl/vgl_vector_3d.h>
#include <vgl/algo/vgl_rotation_3d.h>
#include <vgl/algo/vgl_h_matrix_3d.h>
#include <vnl/vnl_vector_fixed.h>
#include <vnl/vnl_matrix_fixed.h>
vgl_h_matrix_2d<double> vpgl_camera_homographies::
homography_to_camera(vpgl_proj_camera<double> const& cam,
vgl_plane_3d<double> const& plane)
{
// get the translation that moves the plane to intersect with the origin
vgl_vector_3d<double> n = plane.normal();
double mag = n.length();
double trans = -plane.d()/(mag*mag);//translate the plane to the origin
// find the rotation needed to align the normal with the positive z axis
vgl_vector_3d<double> z(0, 0, 1.0);
vgl_rotation_3d<double> R(n, z);
// invert to transform the camera
vgl_rotation_3d<double> Rinv = R.inverse();
vgl_h_matrix_3d<double> Tr = Rinv.as_h_matrix_3d();
Tr.set_translation(trans*n.x(), trans*n.y(), trans*n.z());
// postmultipy the camera by the inverse
vpgl_proj_camera<double> tcam = postmultiply(cam, Tr);
// extract the homography (columns 0, 1, and 3)
vnl_matrix_fixed<double, 3, 4> p = tcam.get_matrix();
vnl_matrix_fixed<double, 3, 3> h;
vnl_vector_fixed<double, 3> h0, h1, h2;//colums of h
h0 = p.get_column(0); h1 = p.get_column(1); h2 = p.get_column(3);
h.set_column(0, h0); h.set_column(1, h1); h.set_column(2, h2);
return vgl_h_matrix_2d<double>(h);
}
//: create a plane projective transformation from the camera image plane to
// the specified plane
vgl_h_matrix_2d<double> vpgl_camera_homographies::
homography_to_camera(vpgl_perspective_camera<double> const& cam,
vgl_plane_3d<double> const& plane)
{
vpgl_proj_camera<double> const& pcam =
static_cast<vpgl_proj_camera<double> const&>(cam);
return vpgl_camera_homographies::homography_to_camera(pcam, plane);
}
//: create a plane projective transformation from the camera image plane to
// the specified plane
vgl_h_matrix_2d<double> vpgl_camera_homographies::
homography_from_camera(vpgl_proj_camera<double> const& cam,
vgl_plane_3d<double> const& plane)
{
vgl_h_matrix_2d<double> H =
vpgl_camera_homographies::homography_to_camera(cam, plane);
return H.get_inverse();
}
//: create a plane projective transformation from the camera image plane to
// the specified plane
vgl_h_matrix_2d<double> vpgl_camera_homographies::
homography_from_camera(vpgl_perspective_camera<double> const& cam,
vgl_plane_3d<double> const& plane)
{
vgl_h_matrix_2d<double> H =
vpgl_camera_homographies::homography_to_camera(cam, plane);
return H.get_inverse();
}