00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef DB_ENTRY_H
00021 #define DB_ENTRY_H
00022
00023 #include <vector>
00024 #include <map>
00025 #include <string>
00026
00027 #include "kpttracker.h"
00028 #include "idcluster.h"
00029 #include "sqlite3.h"
00030
00034
00035 typedef sqlite3_int64 img_id;
00036 class db_keypoint : public pyr_keypoint {
00037 public:
00038
00039 db_keypoint(unsigned cid, img_id img) : image(img) { this->cid = cid; }
00040 db_keypoint(const pyr_keypoint &a, img_id img) : pyr_keypoint(a), image(img) { }
00041
00042 img_id image;
00043
00044 bool operator < (const db_keypoint &a) const { return cid < a.cid; }
00045 };
00046
00047 class visual_database;
00048
00055 class visual_object : public id_cluster {
00056 public:
00057
00058
00059 typedef std::multiset<db_keypoint> db_keypoint_vector;
00060
00061 visual_object(visual_database *, sqlite3_int64 id, const char *comment="", int flags=0);
00062 virtual ~visual_object();
00063
00064 void prepare();
00065 void find(unsigned id, db_keypoint_vector::iterator &start, db_keypoint_vector::iterator &end);
00066
00067 img_id representative_image;
00068
00069
00070 db_keypoint* add_keypoint(pyr_keypoint *p, img_id img);
00071 unsigned add_frame(pyr_frame *frame);
00072 void add(db_keypoint &p);
00073
00074 unsigned get_total() const { return total; }
00075
00076 struct correspondence {
00077 db_keypoint *obj_kpt;
00078 pyr_keypoint *frame_kpt;
00079 float correl;
00080 correspondence(db_keypoint *o, pyr_keypoint *f, float c) : obj_kpt(o), frame_kpt(f), correl(c) {}
00081 bool operator<(const correspondence &a) const { return correl > a.correl; }
00082 };
00083 typedef std::vector<correspondence> correspondence_vector;
00084 float get_correspondences(pyr_frame *frame, correspondence_vector &corresp);
00085 float get_correspondences_std(pyr_frame *frame, correspondence_vector &corresp);
00086 float verify(pyr_frame *frame, correspondence_vector &corresp);
00087
00088 const std::string &get_comment() const { return comment; }
00089
00090 enum { VERIFY_HOMOGRAPHY=1,VERIFY_FMAT=2 };
00091 int get_flags() const { return flags; }
00092 unsigned long get_id() {return static_cast<unsigned long>(obj_id);}
00093
00094 struct annotation {
00095 sqlite3_int64 id;
00096 float x, y;
00097 std::string descr;
00098 int type;
00099
00100 annotation(sqlite3_int64 id, float x, float y, std::string descr, int type)
00101 : id(id), x(x), y(y), descr(descr), type(type) {}
00102 };
00103 typedef std::vector<annotation> annotations_vector;
00104 typedef annotations_vector::const_iterator annotation_iterator;
00105 protected:
00106 annotations_vector annotations;
00107 public:
00108 void add_annotation(float x, float y, int type, const std::string& text);
00109 unsigned get_annotations_count() const { return (unsigned)annotations.size(); }
00110 annotation_iterator annotation_begin() const { return annotations.begin(); }
00111 annotation_iterator annotation_end() const { return annotations.end(); }
00112 const annotation& get_annotation(unsigned id) const { return annotations.at(id); }
00113
00114
00115 CvMat *M;
00116
00117 protected:
00118 bool sorted;
00119
00120 db_keypoint_vector points;
00121
00122 sqlite3_int64 obj_id;
00123
00124 public:
00125 int nb_points() const { return points.size(); }
00126
00127 visual_database *vdb;
00128 protected:
00129 std::string comment;
00130 int flags;
00131
00132 friend class visual_database;
00133 };
00134
00139 class visual_database : public id_cluster_collection {
00140 public:
00141
00142 visual_database(id_cluster_collection::query_flags flags );
00143 virtual ~visual_database();
00144
00151 bool open(const char *fn);
00152
00156 bool connect_to_db(sqlite3 *sql3db);
00157
00162 visual_object *create_object(const char *comment=0, int flags=0);
00163 void add_to_index(visual_object *o) { add_cluster(o); }
00164
00165 bool remove_object(visual_object *o);
00166
00168 img_id add_image(IplImage *im);
00169
00174 IplImage *get_image(img_id im_id);
00175
00176 visual_object *query_frame(pyr_frame *frame, float *score=0);
00177
00178 incremental_query *create_incremental_query() { return new incremental_query(this); }
00179
00180 void start_update() { exec_sql("begin"); }
00181 void finish_update() { exec_sql("commit"); }
00182
00183 sqlite3 *get_sqlite3_db() { return db; }
00184 protected:
00185 sqlite3 *db;
00186
00187 sqlite3_stmt *get_cached_stmt(const char *query, bool verbose=true);
00188 bool exec_sql(const char *query);
00189
00190 typedef std::map<img_id, IplImage *> image_cache_map;
00191 image_cache_map image_cache;
00192
00193 typedef std::map<const char *, sqlite3_stmt *> stmt_cache_map;
00194 stmt_cache_map stmt_cache;
00195
00196 friend class visual_object;
00197 };
00198
00199
00200 void update_query_with_frame(incremental_query &query, kpt_tracker *tracker);
00201 void init_query_with_frame(incremental_query &query, pyr_frame *frame);
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00251 #endif