contrib/oxl/mvl/HMatrix2DSimilarityCompute.cxx
Go to the documentation of this file.
00001 // This is oxl/mvl/HMatrix2DSimilarityCompute.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 
00006 #include "HMatrix2DSimilarityCompute.h"
00007 
00008 #include <vcl_cassert.h>
00009 
00010 #include <vnl/vnl_double_2.h>
00011 #include <vnl/vnl_double_2x2.h>
00012 #include <vnl/vnl_double_3x3.h>
00013 #include <vnl/vnl_transpose.h>
00014 #include <vnl/algo/vnl_svd.h>
00015 
00016 #include <mvl/HMatrix2D.h>
00017 #include <mvl/PairMatchSetCorner.h>
00018 #include <mvl/HMatrix2DAffineCompute.h>
00019 
00020 //
00021 //
00022 //
00023 HMatrix2DSimilarityCompute::HMatrix2DSimilarityCompute(void) : HMatrix2DCompute() { }
00024 HMatrix2DSimilarityCompute::~HMatrix2DSimilarityCompute() { }
00025 
00026 //
00027 //
00028 //
00029 HMatrix2D
00030 HMatrix2DSimilarityCompute::compute(PairMatchSetCorner const& matches)
00031 {
00032  PointArray pts1(matches.count());
00033  PointArray pts2(matches.count());
00034  matches.extract_matches(pts1, pts2);
00035  HMatrix2D H;
00036  tmp_fun(pts1,pts2,&H);
00037  return H;
00038 }
00039 
00040 HMatrix2D
00041 HMatrix2DSimilarityCompute::compute(PointArray const& p1,
00042                                     PointArray const& p2)
00043 {
00044   HMatrix2D H;
00045   tmp_fun(p1,p2,&H);
00046   return H;
00047 }
00048 
00049 bool
00050 HMatrix2DSimilarityCompute::compute_p(PointArray const& pts1,
00051                                       PointArray const& pts2,
00052                                       HMatrix2D *H)
00053 {
00054   return tmp_fun(pts1,pts2,H);
00055 }
00056 
00057 bool
00058 HMatrix2DSimilarityCompute::tmp_fun(PointArray const& pts1,
00059                                     PointArray const& pts2,
00060                                     HMatrix2D *H)
00061 {
00062   assert(pts1.size() == pts2.size());
00063 
00064   NonHomg p1(pts1);
00065   NonHomg p2(pts2);
00066   vnl_double_2 mn1 = mean2(p1);
00067   vnl_double_2 mn2 = mean2(p2);
00068   sub_rows(p1,mn1);
00069   sub_rows(p2,mn2);
00070 
00071   vnl_double_2x2 scatter = vnl_transpose(p2)*p1;
00072   vnl_svd<double> svd(scatter.as_ref()); // size 2x2
00073 
00074   vnl_double_2x2 R = svd.U() * vnl_transpose(svd.V());
00075   double scale = dot_product(p2,p1*R.transpose()) / dot_product(p1,p1);
00076   R *= scale;
00077   vnl_double_2 t = mn2 - R * mn1;
00078 
00079   vnl_double_3x3 T;
00080   T(0,0) = R(0,0); T(0,1) = R(0,1); T(0,2) = t[0];
00081   T(1,0) = R(1,0); T(1,1) = R(1,1); T(1,2) = t[1];
00082   T(2,0) = 0.0;    T(2,1) = 0.0;    T(2,2) = 1.0;
00083   H->set(T);
00084   return true;
00085 }