Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

GrayModel.cc

Go to the documentation of this file.
00001 #include "Vision/AprilTags/GrayModel.h"
00002 
00003 namespace AprilTags {
00004 
00005 GrayModel::GrayModel() : A(), v(), b(), nobs(0), dirty(false) {}
00006 
00007 void GrayModel::addObservation(float x, float y, float gray) {
00008   float xy = x*y;
00009 
00010   // update only upper-right elements. A'A is symmetric,
00011   // we'll fill the other elements in later.
00012   A(0,0) += x*x;
00013   A(0,1) += x*y;
00014   A(0,2) += x*xy;
00015   A(0,3) += x;
00016   A(1,1) += y*y;
00017   A(1,2) += y*xy;
00018   A(1,3) += y;
00019   A(2,2) += xy*xy;
00020   A(2,3) += xy;
00021   A(3,3) += 1;
00022   
00023   b[0] += x*gray;
00024   b[1] += y*gray;
00025   b[2] += xy*gray;
00026   b[3] += gray;
00027 
00028   nobs++;
00029   dirty = true;
00030 }
00031 
00032 float GrayModel::interpolate(float x, float y) {
00033   if (dirty) compute();
00034   return v[0]*x + v[1]*y + v[2]*x*y + v[3];
00035 }
00036 
00037 void GrayModel::compute() {
00038   // we really only need 4 linearly independent observations to fit our answer, but we'll be very
00039   // sensitive to noise if we don't have an over-determined system. Thus, require at least 6
00040   // observations (or we'll use a constant model below).
00041 
00042   dirty = false;
00043   if (nobs >= 6) {
00044     // make symmetric
00045     fmat::Matrix<4,4> Ainv;
00046     for (int i = 0; i < 4; i++)
00047       for (int j = i+1; j < 4; j++)
00048   A(j,i) = A(i,j);
00049 
00050     try {
00051       Ainv = fmat::invert(A);
00052       v = Ainv * b;
00053       return;
00054     }
00055     catch (std::underflow_error&) {
00056       std::cerr << "AprilTags::GrayModel::compute() has underflow in matrix inverse\n";
00057     }
00058   }
00059 
00060   // If we get here, either nobs < 6 or the matrix inverse generated
00061   // an underflow, so use a constant model.
00062   v = (float)0.f;   // need the cast to avoid operator= ambiguity wrt. const-ness
00063   v[3] = b[3] / nobs;      
00064 }
00065 
00066 } // namespace

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:41 2016 by Doxygen 1.6.3