00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef KPT_TRACKER_H
00021 #define KPT_TRACKER_H
00022
00023 #include <stack>
00024
00025 #include <cv.h>
00026 #include "yape.h"
00027 #include "tracks.h"
00028 #include "patchtagger.h"
00029 #include "idcluster.h"
00030 #include "sqlite3.h"
00031
00035
00036
00037
00038 #define WITH_YAPE
00039
00040
00041
00042
00043
00044
00045
00046
00047 #if defined(WITH_YAPE) || defined(WITH_GOOD_FEATURES_TO_TRACK) \
00048 || defined(WITH_FAST) || defined(WITH_ADAPT_THRESH) || defined(WITH_MSER)
00049 #define WITH_PATCH_TAGGER_DESCRIPTOR
00050 #define WITH_PATCH_AS_DESCRIPTOR
00051 #endif
00052
00053
00054 #include "kmeantree.h"
00055
00056 #ifdef WITH_SIFTGPU
00057 #ifdef WIN32
00058 #include <GL/glew.h>
00059 #include <windows.h>
00060 #endif
00061 #include <SiftGPU.h>
00062 #ifdef MACOS
00063 #include <OpenGL/gl.h>
00064 #else
00065
00066 #include <GL/gl.h>
00067 #endif
00068 #endif
00069
00070 #ifdef WITH_MSER
00071 #include "mserdetector.h"
00072 #endif
00073
00074 class kpt_tracker;
00075
00077 struct pyr_frame : tframe {
00078 PyrImage *pyr;
00079 kpt_tracker *tracker;
00080
00081 pyr_frame(PyrImage *p, int bits=4);
00082 virtual ~pyr_frame();
00083 virtual void append_to(tracks &t);
00084
00085 struct pyr_frame_factory_t : factory_t {
00086 virtual pyr_frame *create(PyrImage *p, int bits=4) { return new pyr_frame(p,bits); }
00087 };
00088 };
00089
00091 struct pyr_keypoint : tkeypoint {
00092 int scale;
00093 point2d level;
00094 CvMat patch;
00095
00097 float score;
00098 unsigned char *data;
00099 float mean, stdev;
00100 int id;
00101 unsigned cid;
00102 float cscore;
00103
00104 #ifdef WITH_SURF
00105 kmean_tree::descriptor_t surf_descriptor;
00106 #endif
00107 #ifdef WITH_SIFTGPU
00108 kmean_tree::descriptor_t sift_descriptor;
00109 #endif
00110 #ifdef WITH_MSER
00111 MSERegion mser;
00112 bool get_mser_patch(PyrImage &pyr, cv::Mat mat);
00113 #endif
00114
00115 patch_tagger::descriptor descriptor;
00116 kmean_tree::node_t *node;
00117
00118 pyr_keypoint() { data=0; node=0; }
00119 pyr_keypoint(const pyr_keypoint &a);
00120
00121 void set(tframe *f, keypoint &pt, int patch_size);
00122 void set(tframe *f, float u, float v, int scale, int patch_size);
00123
00124 void prepare_patch(int size);
00125 virtual ~pyr_keypoint();
00126
00127 virtual void dispose();
00128
00129 const pyr_keypoint &operator = (const pyr_keypoint &a);
00130
00131 struct pyr_keypoint_factory_t : factory_t {
00132 virtual tkeypoint *create() { return new pyr_keypoint(); }
00133 };
00134 };
00135
00137 struct pyr_track : ttrack {
00138
00139 incremental_query id_histo;
00140
00141 int nb_lk_tracked;
00142
00144 virtual void point_added(tkeypoint *p);
00145
00147 virtual void point_removed(tkeypoint *p);
00148
00149 pyr_track(tracks *db);
00150
00151 struct pyr_track_factory_t : factory_t {
00152 virtual ttrack *create(tracks *db) { return new pyr_track(db); }
00153 };
00154 };
00155
00158 class kpt_tracker : public tracks {
00159 public:
00160
00161
00162 kpt_tracker(int width, int height, int levels, int max_motion , bool glContext=false,
00163 pyr_keypoint::pyr_keypoint_factory_t *kf=0,
00164 pyr_frame::pyr_frame_factory_t *ff=0,
00165 pyr_track::pyr_track_factory_t *tf = 0);
00166 virtual ~kpt_tracker();
00167
00168 bool load_from_db(sqlite3 *db);
00169 bool load_from_db(const char *dbfile);
00170 bool load_tree(sqlite3 *db);
00171 bool load_tree(const char *fn);
00172 bool load_clusters(sqlite3 *db);
00173 bool load_clusters(const char *fn);
00174
00175 void set_size(int width, int height, int levels, int max_motion);
00176
00180 virtual pyr_frame *process_frame(IplImage *im);
00181
00186 virtual pyr_frame *process_frame_pipeline(IplImage *im);
00187
00188 protected: pyr_frame *create_frame(IplImage *im);
00189 public:
00190 pyr_frame *add_frame(IplImage *im);
00191 void traverse_tree(pyr_frame *frame);
00192 void detect_keypoints(pyr_frame *f);
00193 void track_ncclk(pyr_frame *f, pyr_frame *lf);
00194
00196 void clear();
00197
00198
00199 #ifdef WITH_YAPE
00200 pyr_yape *detector;
00201 keypoint *points;
00202 #endif
00203
00204 #ifdef WITH_SIFTGPU
00205 SiftGPU sift;
00206 #endif
00207
00208 #ifdef WITH_MSER
00209 MSERDetector mser;
00210 #endif
00211
00212 #ifdef WITH_ADAPT_THRESH
00213 CvMat *adapt_thresh_mat;
00214 #endif
00215
00216 kmean_tree::node_t *tree;
00217 id_cluster_collection *id_clusters;
00218
00219 int nb_points;
00220 int max_motion;
00221 int nb_levels;
00222 int patch_size;
00223 float ncc_threshold, ncc_threshold_high;
00224
00225 pyr_keypoint *best_match(pyr_keypoint *templ, tracks::keypoint_frame_iterator it);
00226
00227 private:
00228
00229 class recycler_t {
00230 public:
00231 recycler_t(pyr_keypoint::pyr_keypoint_factory_t *f) : factory(f) {}
00232
00233 pyr_keypoint *get_new() {
00234
00235 #if 1
00236 return (pyr_keypoint *)factory->create();
00237 #else
00238 pyr_keypoint *r=0;
00239 if (!available.empty()) { pyr_keypoint *o = available.top(); available.pop(); r= o; }
00240 else r= (pyr_keypoint *)factory->create() ;
00241 return r;
00242 #endif
00243 }
00244
00245 #if 1
00246 void recycle(pyr_keypoint *obj) { delete (obj); }
00247 #else
00248 void recycle(pyr_keypoint *obj) { available.push(obj); }
00249 #endif
00250 void clear() {
00251 while (!available.empty()) { delete available.top(); available.pop(); }
00252 }
00253 ~recycler_t() { clear(); }
00254 protected:
00255 std::stack<pyr_keypoint *> available;
00256 pyr_keypoint::pyr_keypoint_factory_t *factory;
00257 };
00258
00259 recycler_t kpt_recycler;
00260 friend class pyr_keypoint;
00261
00262 protected:
00263 pyr_frame *pipeline_stage1;
00264 };
00265
00267 #endif