TrackerStat.cpp
Go to the documentation of this file.
00001 /*
00002  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
00003  *
00004  * Copyright 2007-2012 VTT Technical Research Centre of Finland
00005  *
00006  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
00007  *          <http://www.vtt.fi/multimedia/alvar.html>
00008  *
00009  * ALVAR is free software; you can redistribute it and/or modify it under the
00010  * terms of the GNU Lesser General Public License as published by the Free
00011  * Software Foundation; either version 2.1 of the License, or (at your option)
00012  * any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful, but WITHOUT
00015  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00017  * for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with ALVAR; if not, see
00021  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
00022  */
00023 
00024 #include "TrackerStat.h"
00025 
00026 using namespace std;
00027 
00028 namespace alvar {
00029 using namespace std;
00030 
00031 TrackerStat::TrackerStat(int binsize) : f(100,90) {
00032         hist.AddDimension(binsize); // x
00033         hist.AddDimension(binsize); // y
00034 }
00035 
00036 void TrackerStat::Reset() {
00037         f.Reset();
00038 }
00039 
00040 double TrackerStat::Track(IplImage *img) 
00041 {
00042         if (img == NULL) return -1;
00043         f.Track(img);
00044         hist.Clear();
00045         for (int p=0; p<f.prev_feature_count ; p++) {
00046                 for (int c=0; c<f.feature_count; c++) {
00047                         if (f.prev_ids[p] != f.ids[c]) continue;
00048                         float x = f.features[c].x - f.prev_features[p].x;
00049                         float y = f.features[c].y - f.prev_features[p].y;
00050                         hist.Inc(x, y);
00051                 }
00052         }
00053         xd = 0; yd = 0;
00054         return hist.GetMax(&xd, &yd);
00055 }
00056 
00057 void TrackerStat::Compensate(double *x, double *y) { 
00058         *x += xd; *y += yd; 
00059 }
00060 
00061 TrackerStatRot::TrackerStatRot(int binsize /*=8*/, int binsize_rot/*=3*/) : TrackerStat(binsize) {
00062         hist_rot.AddDimension(binsize_rot);
00063 }
00064 
00065 double TrackerStatRot::Track(IplImage *img)
00066 {
00067         if (img == NULL) return -1;
00068         f.Track(img);
00069         // Translation
00070         hist.Clear();
00071         for (int p=0; p<f.prev_feature_count ; p++) {
00072                 for (int c=0; c<f.feature_count; c++) {
00073                         if (f.prev_ids[p] != f.ids[c]) continue;
00074                         float x = f.features[c].x - f.prev_features[p].x;
00075                         float y = f.features[c].y - f.prev_features[p].y;
00076                         hist.Inc(x, y);
00077                 }
00078         }
00079         xd = 0; yd = 0;
00080         double ret = hist.GetMax(&xd, &yd);
00081         // Rotation
00082         x_res = img->width;
00083         y_res = img->height;
00084         hist_rot.Clear();
00085         for (int p=0; p<f.prev_feature_count ; p++) {
00086                 for (int c=0; c<f.feature_count; c++) {
00087                         if (f.prev_ids[p] != f.ids[c]) continue;
00088                         double x_pred = f.prev_features[p].x + xd;
00089                         double y_pred = f.prev_features[p].y + yd;
00090                         double x_curr = f.features[c].x;
00091                         double y_curr = f.features[c].y;
00092                         double x = x_curr - x_pred;
00093                         double y = y_curr - y_pred;
00094                         double theta_pred = atan2((double)y_pred-(y_res/2), (double)x_pred-(x_res/2))*180.0/3.1415926535;
00095                         double theta_curr = atan2((double)y_curr-(y_res/2), (double)x_curr-(x_res/2))*180.0/3.1415926535;
00096                         hist_rot.Inc(theta_curr-theta_pred);
00097                 }
00098         }
00099         rotd=0;
00100         hist_rot.GetMax(&rotd);
00101         return ret;
00102 }
00103 
00104 void TrackerStatRot::Compensate(double *x, double *y)
00105 {
00106         double xx = *x - (x_res/2);
00107         double yy = *y - (y_res/2);
00108         double kosini = cos(rotd*3.1415926535/180);
00109         double sini = sin(rotd*3.1415926535/180);
00110         *x = ((kosini * xx) - (sini * yy)) + xd + (x_res/2);
00111         *y = ((sini * xx) + (kosini * yy)) + yd + (y_res/2);
00112 }
00113 
00114 } // namespace alvar


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Sat Dec 28 2013 16:46:16