Go to the documentation of this file.00001 #include "vpgl_camera_homographies.h"
00002
00003
00004 #include <vgl/vgl_vector_3d.h>
00005 #include <vgl/algo/vgl_rotation_3d.h>
00006 #include <vgl/algo/vgl_h_matrix_3d.h>
00007 #include <vnl/vnl_vector_fixed.h>
00008 #include <vnl/vnl_matrix_fixed.h>
00009 static vgl_h_matrix_3d<double> plane_trans(vgl_plane_3d<double> const& plane,
00010 vgl_point_3d<double> const& ref_pt)
00011 {
00012
00013
00014
00015 vgl_vector_3d<double> n(plane.a(), plane.b(), plane.c());
00016 double mag = n.length();
00017 n/=mag;
00018 double trans = plane.d()/mag;
00019
00020 vgl_vector_3d<double> z(0, 0, 1.0);
00021 vgl_rotation_3d<double> R(n, z);
00022 vgl_vector_3d<double> t = R*(trans*n);
00023 vgl_h_matrix_3d<double> Tr = R.as_h_matrix_3d();
00024 Tr.set_translation(t.x(), t.y(), t.z());
00025
00026
00027
00028 vgl_h_matrix_3d<double> Trinv = Tr.get_inverse();
00029 vgl_homg_point_3d<double> hp(ref_pt);
00030 vgl_homg_point_3d<double> thp = Trinv(hp);
00031 vgl_point_3d<double> tp(thp);
00032
00033
00034
00035 if(tp.z()<0){
00036 vnl_matrix_fixed<double,3, 3> m(0.0);
00037 m[0][0] = -1.0; m[1][1] = 1.0; m[2][2]=-1.0;
00038 vgl_h_matrix_3d<double> Trflip;
00039 Trflip.set_identity(); Trflip.set_rotation_matrix(m);
00040 Tr = Trflip*Tr;
00041 }
00042 return Tr;
00043 }
00044
00045 vgl_h_matrix_2d<double> vpgl_camera_homographies::
00046 homography_to_camera(vpgl_proj_camera<double> const& cam,
00047 vgl_plane_3d<double> const& plane)
00048 {
00049 vgl_homg_point_3d<double> hc = cam.camera_center();
00050 vgl_point_3d<double> cp(hc);
00051 vgl_h_matrix_3d<double> Tr = plane_trans(plane,cp);
00052 vgl_h_matrix_3d<double> Trinv = Tr.get_inverse();
00053
00054 vpgl_proj_camera<double> tcam = postmultiply(cam, Trinv);
00055
00056 vnl_matrix_fixed<double, 3, 4> p = tcam.get_matrix();
00057 vnl_matrix_fixed<double, 3, 3> h;
00058 vnl_vector_fixed<double, 3> h0, h1, h2;
00059 h0 = p.get_column(0); h1 = p.get_column(1); h2 = p.get_column(3);
00060 h.set_column(0, h0); h.set_column(1, h1); h.set_column(2, h2);
00061 vgl_h_matrix_2d<double> H(h);
00062 return H;
00063 }
00064
00065
00066 vgl_h_matrix_2d<double> vpgl_camera_homographies::
00067 homography_to_camera(vpgl_perspective_camera<double> const& cam,
00068 vgl_plane_3d<double> const& plane)
00069 {
00070 vpgl_proj_camera<double> const& pcam =
00071 static_cast<vpgl_proj_camera<double> const&>(cam);
00072 return vpgl_camera_homographies::homography_to_camera(pcam, plane);
00073 }
00074
00075
00076 vgl_h_matrix_2d<double> vpgl_camera_homographies::
00077 homography_from_camera(vpgl_proj_camera<double> const& cam,
00078 vgl_plane_3d<double> const& plane)
00079 {
00080 vgl_h_matrix_2d<double> H =
00081 vpgl_camera_homographies::homography_to_camera(cam, plane);
00082 return H.get_inverse();
00083 }
00084
00085
00086 vgl_h_matrix_2d<double> vpgl_camera_homographies::
00087 homography_from_camera(vpgl_perspective_camera<double> const& cam,
00088 vgl_plane_3d<double> const& plane)
00089 {
00090 vgl_h_matrix_2d<double> H =
00091 vpgl_camera_homographies::homography_to_camera(cam, plane);
00092 return H.get_inverse();
00093 }
00094 vpgl_perspective_camera<double> vpgl_camera_homographies::
00095 transform_camera_to_plane(vpgl_perspective_camera<double> const& cam,
00096 vgl_plane_3d<double> const& plane)
00097 {
00098 vgl_homg_point_3d<double> hc = cam.camera_center();
00099 vgl_point_3d<double> cp(hc);
00100 vgl_h_matrix_3d<double> Tr = plane_trans(plane,cp);
00101 vgl_h_matrix_3d<double> Trinv = Tr.get_inverse();
00102
00103 vpgl_perspective_camera<double> tcam =
00104 vpgl_perspective_camera<double>::postmultiply(cam, Trinv);
00105 return tcam;
00106 }
00107
00108 vpgl_proj_camera<double> vpgl_camera_homographies::
00109 transform_camera_to_plane(vpgl_proj_camera<double> const& cam,
00110 vgl_plane_3d<double> const& plane)
00111 {
00112 vgl_homg_point_3d<double> hc = cam.camera_center();
00113 vgl_point_3d<double> cp(hc);
00114 vgl_h_matrix_3d<double> Tr = plane_trans(plane,cp);
00115 vgl_h_matrix_3d<double> Trinv = Tr.get_inverse();
00116
00117 vpgl_proj_camera<double> tcam = postmultiply(cam, Trinv);
00118 return tcam;
00119 }
00120
00121 vcl_vector<vgl_point_3d<double> > vpgl_camera_homographies::
00122 transform_points_to_plane(vgl_plane_3d<double> const& plane,
00123 vgl_point_3d<double> const& ref_point,
00124 vcl_vector<vgl_point_3d<double> > const& pts )
00125 {
00126 vcl_vector<vgl_point_3d<double> > tr_pts;
00127 vgl_h_matrix_3d<double> Tr = plane_trans(plane, ref_point);
00128 for(vcl_vector<vgl_point_3d<double> >::const_iterator pit = pts.begin();
00129 pit != pts.end(); ++pit)
00130 {
00131 vgl_homg_point_3d<double> hp(*pit);
00132 vgl_homg_point_3d<double> tr_hp = Tr(hp);
00133 vgl_point_3d<double> trp(tr_hp);
00134 tr_pts.push_back(trp);
00135 }
00136 return tr_pts;
00137 }