Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
klt.hpp
00001 /*********************************************************************
00002  * klt.hpp
00003  * jafar interface and improvements for the Birchfield implementation of the 
00004  * Kanade-Lucas-Tomasi tracker
00005  * http://www.ces.clemson.edu/~stb/klt/
00006  * contact : cyril.roussillon@laas.fr
00007  *********************************************************************/
00008 
00009 #ifndef _KLT_HPP_
00010 #define _KLT_HPP_
00011 
00012 #include "klt/klt.h"
00013 #include "image/Image.hpp"
00014 #include "klt/compatImage.hpp"
00015 
00016 #include "filter/observeModel.hpp"
00017 #include "filter/predictModel.hpp"
00018 #include "filter/kalmanFilter.hpp"
00019 
00020 
00021 
00022 namespace jafar {
00023   
00024   namespace klt {
00025 
00026     
00027     typedef double tDate;
00028     
00033     class TrackedObject
00034     {
00035     public:
00036       KLT_FeatureTable ft; 
00037                            //  (circular buffer so that you can let the algorithm running)
00038 
00039       int curFrame; 
00040 
00041       // state
00042       int scope, prevScope; 
00043           //the barycenter
00044 
00045       int boundingBox[4]; 
00046         //bounding box of the object (x1,y1,x2,y2)
00047 
00048       int pos[2]; 
00049       //(x,y)
00050       
00051       //
00052       int maxDisplacement; 
00053          //one frame to the next, determining the
00054          //size of the processing area
00055 
00056       int maxAccel; 
00057         //pixels by frames^2, determining the error of the
00058         //kalman model (speed is constant)
00059 
00060       int measureConfidence; 
00061            //(barycenter of features representing the object)
00062       
00063       int initSpeedConfidence; 
00064                                // that's to say the maximum value of initial speed 
00065                                // (because we assume it to be 0 for the first frame)
00066       
00067       //--- kalman prediction
00068       int estimatedPos[2]; 
00069       
00070       int estimatedSpeed[2]; 
00071       
00072       int confidenceDist; 
00073    
00077       TrackedObject()
00078       {
00079   estimatedPos[0] = 0;
00080   estimatedPos[1] = 0;
00081   confidenceDist = 0;
00082   ft = NULL;
00083   curFrame = 0;
00084       }
00085       
00086       ~TrackedObject()
00087       {
00088   if (ft != NULL) KLTFreeFeatureTable(ft);
00089       }
00090       
00096       int calcState(KLT_FeatureList fl, bool first=false);
00097 
00103       void detectLandscape(KLT_FeatureList fl);
00104     };
00105     
00106     
00126     class Klt
00127     {
00128       int workingPos[2];
00129       int prevWorkingPos[2];
00130       int workingDims[2];
00131       int prevWorkingDims[2];
00132     
00133       //int trackingZone[4]; // = workingZone - borders
00134       int searchingZone[4];
00135       
00136       int securityMargin;
00137       
00138       filter::KalmanFilter *kalman; 
00139       tDate lastTimeStamp;
00140       
00141     public:
00142       
00143       TrackedObject object;
00144       
00165       Klt(int maxDisplacement=50, int maxAccel=50, int securityMargin=10, 
00166     int measureConfidence=10, int initSpeedConfidence=100):
00167   securityMargin(securityMargin)
00168       { 
00169   object.maxDisplacement = maxDisplacement; 
00170   object.maxAccel = maxAccel;
00171   object.measureConfidence = measureConfidence;
00172   object.initSpeedConfidence = initSpeedConfidence;
00173   kalman = NULL;
00174       }
00175       
00176       KLT_TrackingContext CreateTrackingContext(void)
00177       { return KLTCreateTrackingContext(); }
00178 
00179       KLT_FeatureList CreateFeatureList(int nFeatures)
00180       { return KLTCreateFeatureList(nFeatures); }
00181       
00182       KLT_FeatureHistory CreateFeatureHistory(int nFrames)
00183       { return KLTCreateFeatureHistory(nFrames); }
00184 
00185       KLT_FeatureTable CreateFeatureTable(int nFrames, int nFeatures)
00186       { return KLTCreateFeatureTable(nFrames, nFeatures); }
00187 
00188       // Free 
00189       void FreeTrackingContext(KLT_TrackingContext tc)
00190       { return KLTFreeTrackingContext(tc); }
00191       
00192       void FreeFeatureList(KLT_FeatureList fl)
00193       { return KLTFreeFeatureList(fl); }
00194       
00195       void FreeFeatureHistory(KLT_FeatureHistory fh)
00196       { return KLTFreeFeatureHistory(fh); }
00197       
00198       void FreeFeatureTable(KLT_FeatureTable ft)
00199       { return KLTFreeFeatureTable(ft); }
00200 
00201       // Processing 
00202       void SelectGoodFeatures(KLT_TrackingContext tc, 
00203             image::Image &jImg, KLT_FeatureList fl)
00204       { 
00205   klt::CompatTreatment treatment;
00206   KLT_PixelType *img = compatImage(jImg, treatment);
00207   KLTSelectGoodFeatures(tc, img, jImg.width(), jImg.height(), fl);
00208   destroyCompatImage(img, treatment);
00209       }
00210   
00211       void TrackFeatures(KLT_TrackingContext tc,
00212        image::Image &jImg1, image::Image &jImg2,
00213        KLT_FeatureList fl)
00214       { 
00215   klt::CompatTreatment treatment1, treatment2;
00216   KLT_PixelType *img1 = NULL, *img2 = NULL;
00217   //  if (!tc->sequentialMode) 
00218   img1 = compatImage(jImg1, treatment1);
00219   img2 = compatImage(jImg2, treatment2); 
00220   KLTTrackFeatures(tc, img1, img2, jImg2.width(), jImg2.height(), fl); 
00221   //  if (!tc->sequentialMode) 
00222   destroyCompatImage(img1, treatment1);
00223   destroyCompatImage(img2, treatment2);
00224       }
00225   
00226       void ReplaceLostFeatures(KLT_TrackingContext tc, 
00227              image::Image &jImg,
00228              int ncols, int nrows, KLT_FeatureList fl)
00229       {
00230   klt::CompatTreatment treatment;
00231   KLT_PixelType *img = compatImage(jImg, treatment); 
00232   KLTReplaceLostFeatures(tc, img, ncols, nrows, fl); 
00233   destroyCompatImage(img, treatment);
00234       }
00235       
00270       void SelectGoodFeaturesObject(KLT_TrackingContext tc, image::Image &jImg, 
00271             KLT_FeatureList fl,int zone_x=-1, int zone_y=-1, int zone_w=-1, 
00272             int zone_h=-1,tDate timeStamp = -1);
00297       void TrackFeaturesObject(KLT_TrackingContext tc,
00298              image::Image &jImg1, image::Image &jImg2,
00299              KLT_FeatureList fl, bool replace=false, tDate timeStamp = -1);
00300       
00301       // Utilities  
00302       int CountRemainingFeatures(KLT_FeatureList fl)
00303       { return KLTCountRemainingFeatures(fl); }
00304       
00305       void PrintTrackingContext(KLT_TrackingContext tc)
00306       { return KLTPrintTrackingContext(tc); }
00307       
00308       void ChangeTCPyramid(KLT_TrackingContext tc, int search_range)
00309       { return KLTChangeTCPyramid(tc, search_range); }
00310       
00311       void UpdateTCBorder(KLT_TrackingContext tc)
00312       { return KLTUpdateTCBorder(tc); }
00313         
00314       void StopSequentialMode(KLT_TrackingContext tc)
00315       { return KLTStopSequentialMode(tc); }
00316   
00317       void SetVerbosity(int verbosity)
00318       { return KLTSetVerbosity(verbosity); }
00319       
00320       float ComputeSmoothSigma(KLT_TrackingContext tc)
00321       { return _KLTComputeSmoothSigma(tc); }
00322 
00323       // Storing/Extracting Features 
00324       void StoreFeatureList(KLT_FeatureList fl, KLT_FeatureTable ft, int frame)
00325       { return KLTStoreFeatureList(fl, ft, frame); }
00326       
00327       void ExtractFeatureList(KLT_FeatureList fl, KLT_FeatureTable ft, int frame)
00328       { return KLTExtractFeatureList(fl, ft, frame); }
00329       
00330       void StoreFeatureHistory(KLT_FeatureHistory fh, KLT_FeatureTable ft, int feat)
00331       { return KLTStoreFeatureHistory(fh, ft, feat); }
00332       
00333       void ExtractFeatureHistory(KLT_FeatureHistory fh, KLT_FeatureTable ft, int feat)
00334       { return KLTExtractFeatureHistory(fh, ft, feat); }
00335 
00336       // Writing/Reading 
00337     
00338       void WriteFeatureListToPPM(KLT_FeatureList fl, KLT_PixelType *greyimg,
00339          int ncols, int nrows, char *filename)    
00340       { return KLTWriteFeatureListToPPM(fl, greyimg, ncols, nrows, filename); }
00341 
00349       void WriteImageToImg(image::Image &jGreyimg, image::Image &jRGBimg);
00357       void WriteFeatureListToImg(KLT_FeatureList fl, image::Image &jRGBimg);
00365       void WriteBordersToImg(image::Image &jRGBimg, KLT_TrackingContext tc);
00366       
00373       void WriteKalmanToImg(image::Image &jRGBimg, KLT_TrackingContext tc);
00374       
00375       void WriteFeatureList(KLT_FeatureList fl, char *filename, char *fmt)
00376       { return KLTWriteFeatureList(fl, filename, fmt); }
00377       
00378       void WriteFeatureHistory(KLT_FeatureHistory fh, char *filename, char *fmt)
00379       { return KLTWriteFeatureHistory(fh, filename, fmt); }
00380   
00381       void WriteFeatureTable(KLT_FeatureTable ft, char *filename, char *fmt)
00382       { return KLTWriteFeatureTable(ft, filename, fmt); }
00383   
00384       KLT_FeatureList ReadFeatureList(KLT_FeatureList fl, char *filename)
00385       { return KLTReadFeatureList(fl, filename); }
00386       
00387       KLT_FeatureHistory ReadFeatureHistory(KLT_FeatureHistory fh, char *filename)
00388       { return KLTReadFeatureHistory(fh, filename); }
00389       
00390       KLT_FeatureTable ReadFeatureTable(KLT_FeatureTable ft, char *filename)
00391       { return KLTReadFeatureTable(ft, filename); }
00392     };
00393     
00394     
00395   }
00396   
00397 } // end namespaces
00398 
00399 #endif
00400 
00401 
00402 
00403 
00404 
00405 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS