00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <QVKLTTracker>
00026
00027 QVKLTTracker::QVKLTTracker(int width, int height, bool trackWithGain,
00028 int featuresWidth , int featuresHeight,
00029 int nIterations, int nLevels, int levelSkip,int windowWidth,
00030 float trackBorderMargin, float detectBorderMargin,
00031 float convergenceThreshold, float SSD_Threshold,
00032 int minDistance, float minCornerness) {
00033 KLT_SequenceTrackerConfig cfg;
00034
00035 cfg.nIterations = nIterations;
00036 cfg.nLevels = nLevels;
00037 cfg.levelSkip = levelSkip;
00038 cfg.trackBorderMargin = trackBorderMargin;
00039 cfg.convergenceThreshold = convergenceThreshold;
00040 cfg.SSD_Threshold = SSD_Threshold;
00041 cfg.trackWithGain = trackWithGain;
00042 cfg.minDistance = minDistance;
00043 cfg.minCornerness = minCornerness;
00044 cfg.detectBorderMargin = detectBorderMargin;
00045 cfg.windowWidth = windowWidth;
00046
00047 tracker = new KLT_SequenceTracker(cfg);
00048 tracker->allocate(width, height, nLevels, featuresWidth, featuresHeight);
00049 nFeatures = featuresHeight * featuresWidth;
00050 feat = new KLT_TrackedFeature[nFeatures];
00051 trackID = new int [nFeatures];
00052 for(int i=0; i<nFeatures; i++) {
00053 trackID[i] = 0;
00054 }
00055 lastID = 1;
00056 }
00057
00058 QVKLTTracker::~QVKLTTracker() {
00059 tracker->deallocate();
00060 delete tracker;
00061 delete feat;
00062 delete trackID;
00063 }
00064
00065 void QVKLTTracker::updateHashTable(QHash<int,QVKLTTrackerFeature> &features,int width,int height) {
00066
00067
00068
00069 for(int i=0; i<nFeatures; i++) {
00070 if(feat[i].status == 0) {
00071
00072 features.insert(trackID[i],
00073 QVKLTTrackerFeature(feat[i].pos[0]*width, feat[i].pos[1]*height,
00074 feat[i].gain));
00075
00076 } else if (feat[i].status > 0) {
00077
00078
00079 if(trackID[i] != 0)
00080 features.remove(trackID[i]);
00081
00082 trackID[i] = lastID;
00083 lastID++;
00084 features.insert(trackID[i],
00085 QVKLTTrackerFeature(feat[i].pos[0]*width, feat[i].pos[1]*height,
00086 feat[i].gain));
00087
00088 } else {
00089
00090 if(trackID[i] != 0)
00091 features.remove(trackID[i]);
00092
00093 }
00094 }
00095
00096
00097
00098 }
00099
00100 void QVKLTTracker::detect(const QVImage<uChar,1> &image, QHash<int,QVKLTTrackerFeature> &features) {
00101 int nDetectedFeatures = 0;
00102 int width = image.getCols(), height = image.getRows();
00103
00104 tracker->detect((V3D_GPU::uchar *)image.getReadData(), nDetectedFeatures, feat);
00105
00106 updateHashTable(features,width,height);
00107 tracker->advanceFrame();
00108 }
00109
00110 void QVKLTTracker::redetect(const QVImage<uChar,1> &image, QHash<int,QVKLTTrackerFeature> &features) {
00111 int nNewFeatures;
00112 int width = image.getCols(), height = image.getRows();
00113
00114 tracker->redetect((V3D_GPU::uchar *)image.getReadData(), nNewFeatures, feat);
00115
00116 updateHashTable(features,width,height);
00117 tracker->advanceFrame();
00118 }
00119
00120 void QVKLTTracker::track(const QVImage<uChar,1> &image, QHash<int,QVKLTTrackerFeature> &features) {
00121 int nPresentFeatures;
00122 int width = image.getCols(), height = image.getRows();
00123
00124 tracker->track((V3D_GPU::uchar *)image.getReadData(), nPresentFeatures, feat);
00125
00126 updateHashTable(features,width,height);
00127 tracker->advanceFrame();
00128 }