vpgl_camera_homographies.cxx
Go to the documentation of this file.
2 //:
3 // \file
4 #include <vgl/vgl_vector_3d.h>
7 #include <vnl/vnl_vector_fixed.h>
8 #include <vnl/vnl_matrix_fixed.h>
9 static vgl_h_matrix_3d<double> plane_trans(vgl_plane_3d<double> const& plane,
10  vgl_point_3d<double> const& ref_pt)
11 {
12  // get the translation that moves the plane to intersect with the origin
13  // note that calling plane.normal() pre-normalizes the vector so
14  // scale factor is lost, so form normal directly
15  vgl_vector_3d<double> n(plane.a(), plane.b(), plane.c());
16  double mag = n.length();
17  n/=mag;
18  double trans = plane.d()/mag;//translate the plane to the origin
19  // find the rotation needed to align the normal with the positive z axis
20  vgl_vector_3d<double> z(0, 0, 1.0);
22  vgl_vector_3d<double> t = R*(trans*n);
23  vgl_h_matrix_3d<double> Tr = R.as_h_matrix_3d();
24  Tr.set_translation(t.x(), t.y(), t.z());
25  //note the composition is [R][t], i.e. first translate then rotate
26  //correct the transformation if the reference point is inverse transformed
27  //to negative z
29  vgl_homg_point_3d<double> hp(ref_pt);
30  vgl_homg_point_3d<double> thp = Trinv(hp);
31  vgl_point_3d<double> tp(thp);
32  //This flip is here to standardize the reference point
33  //(typically a camera center)
34  //to be in the positive z half space of the x-y plane
35  if (tp.z()<0) {
37  m[0][0] = -1.0; m[1][1] = 1.0; m[2][2]=-1.0;
38  vgl_h_matrix_3d<double> Trflip;//180 degree rotation about the y axis
39  Trflip.set_identity();
40  Trflip.set_rotation_matrix(m);
41  Tr = Trflip*Tr;
42  }
43  return Tr;
44 }
45 
48  vgl_plane_3d<double> const& plane)
49 {
51  vgl_point_3d<double> cp(hc);
52  vgl_h_matrix_3d<double> Tr = plane_trans(plane,cp);
54  // postmultipy the camera by the inverse
55  vpgl_proj_camera<double> tcam = postmultiply(cam, Trinv);
56  // extract the homography (columns 0, 1, and 3)
59  vnl_vector_fixed<double, 3> h0, h1, h2; //columns of h
60  h0 = p.get_column(0); h1 = p.get_column(1); h2 = p.get_column(3);
61  h.set_column(0, h0); h.set_column(1, h1); h.set_column(2, h2);
63  return H;
64 }
65 
66 //: create a plane projective transformation from the camera image plane to the specified plane
69  vgl_plane_3d<double> const& plane)
70 {
71  auto const& pcam =
72  static_cast<vpgl_proj_camera<double> const&>(cam);
74 }
75 
76 //: create a plane projective transformation from the camera image plane to the specified plane
79  vgl_plane_3d<double> const& plane)
80 {
83  return H.get_inverse();
84 }
85 
86 //: create a plane projective transformation from the camera image plane to the specified plane
89  vgl_plane_3d<double> const& plane)
90 {
93  return H.get_inverse();
94 }
95 
98  vgl_plane_3d<double> const& plane)
99 {
101  vgl_point_3d<double> cp(hc);
102  vgl_h_matrix_3d<double> Tr = plane_trans(plane,cp);
104  // postmultipy the camera by the inverse
107  return tcam;
108 }
109 
112  vgl_plane_3d<double> const& plane)
113 {
115  vgl_point_3d<double> cp(hc);
116  vgl_h_matrix_3d<double> Tr = plane_trans(plane,cp);
118  // postmultipy the camera by the inverse
119  vpgl_proj_camera<double> tcam = postmultiply(cam, Trinv);
120  return tcam;
121 }
122 
123 std::vector<vgl_point_3d<double> > vpgl_camera_homographies::
125  vgl_point_3d<double> const& ref_point,
126  std::vector<vgl_point_3d<double> > const& pts )
127 {
128  std::vector<vgl_point_3d<double> > tr_pts;
129  vgl_h_matrix_3d<double> Tr = plane_trans(plane, ref_point);
130  for (const auto & pt : pts)
131  {
133  vgl_homg_point_3d<double> tr_hp = Tr(hp);
134  vgl_point_3d<double> trp(tr_hp);
135  tr_pts.push_back(trp);
136  }
137  return tr_pts;
138 }
static vpgl_perspective_camera< T > postmultiply(const vpgl_perspective_camera< T > &in_cam, const vgl_h_matrix_3d< T > &euclid_trans)
Post-multiply this perspective camera with a 3-d Euclidean transformation.
vgl_h_matrix_3d get_inverse() const
vnl_vector_fixed< T, num_rows > get_column(unsigned col) const
vgl_h_matrix_3d & set_translation(T tx, T ty, T tz)
static vgl_h_matrix_2d< double > homography_to_camera(vpgl_proj_camera< double > const &cam, vgl_plane_3d< double > const &plane)
create a plane projective transformation from the specified plane to the camera image plane.
T a() const
static std::vector< vgl_point_3d< double > > transform_points_to_plane(vgl_plane_3d< double > const &plane, vgl_point_3d< double > const &ref_point, std::vector< vgl_point_3d< double > > const &pts)
transform a point cloud so that its coordinate system has the specified plane as the world X-Y plane.
#define m
T b() const
double length() const
vpgl_perspective_camera< T > postmultiply(const vpgl_perspective_camera< T > &in_cam, const vgl_h_matrix_3d< T > &euclid_trans)
T y() const
static vgl_h_matrix_2d< double > homography_from_camera(vpgl_proj_camera< double > const &cam, vgl_plane_3d< double > const &plane)
create a plane projective transformation from the camera image plane to the specified plane.
vgl_h_matrix_3d & set_rotation_matrix(vnl_matrix_fixed< T, 3, 3 > const &R)
virtual vgl_homg_point_3d< T > camera_center() const
Find the 3d coordinates of the center of the camera.
vgl_homg_point_3d< T > camera_center() const override
Return the known camera center instead of computing it in the base class.
T x() const
T d() const
T z() const
This class implements the perspective camera class as described in Hartley & Zisserman as a finite ca...
vgl_h_matrix_3d & set_identity()
static vpgl_perspective_camera< double > transform_camera_to_plane(vpgl_perspective_camera< double > const &cam, vgl_plane_3d< double > const &plane)
transform a camera so that its world coordinate system has the specified plane as the world X-Y plane...
const vnl_matrix_fixed< T, 3, 4 > & get_matrix() const
Return a copy of the camera matrix.
vgl_h_matrix_2d get_inverse() const
vnl_matrix_fixed & set_column(unsigned i, T const *v)
Various miscellaneous methods involving cameras.
T c() const