$search
00001 #include <fstream> 00002 #include <iostream> 00003 #include <list> 00004 #include <opencv/cv.h> 00005 #include <opencv/highgui.h> 00006 #include <Eigen/StdVector> 00007 00008 #include <vocabulary_tree/simple_kmeans.h> 00009 #include <vocabulary_tree/vocabulary_tree.h> 00010 #include <vocabulary_tree/database.h> 00011 #include <vocabulary_tree/tree_builder.h> 00012 #include <vocabulary_tree/simple_kmeans.h> 00013 00014 #include <siftfast/siftfast.h> 00015 00016 #include <common.h> 00017 using namespace odu_finder; 00018 00019 typedef Eigen::Matrix<float, 1, 128> Feature; 00020 typedef std::vector<Feature, Eigen::aligned_allocator<Feature> > FeatureVector; 00021 00022 const int COLORS = 13; 00023 00024 class DocumentInfo 00025 { 00026 private: 00027 bool delete_document; 00028 public: 00029 vt::Document* document; 00030 std::string name; 00031 DocumentInfo(); 00032 DocumentInfo(vt::Document* document, std::string& name); 00033 ~DocumentInfo(); 00034 void write (std::ostream& out); 00035 void read(std::istream& in); 00036 }; 00037 00038 class ODUFinder 00039 { 00040 public: 00041 enum VisualizationMode { FRAMES, SEQUENCES }; 00042 00043 // LOGGING 00044 std::ofstream logger; 00045 bool enable_logging_; 00046 int frame_number; 00047 std::map<std::string, int> stat_summary_map; 00048 bool extract_roi_; 00049 00050 // VISUALIZATION 00051 IplImage *camera_image, *template_image, *image ,*image_roi; 00052 CvScalar color_table[COLORS]; 00053 std::vector<unsigned int> cluster_sizes; 00054 std::string output_image_topic_; 00055 VisualizationMode visualization_mode_; 00056 std::vector<std::string> sequence_buffer; 00057 00058 // DATABASE 00059 std::vector<vt::Document> docs; 00060 vt::TreeBuilder<Feature> tree_builder; 00061 vt::VocabularyTree<Feature> tree; 00062 vt::Database* db; 00063 std::vector<std::string> image_names; 00064 std::map<int, DocumentInfo*> documents_map; 00065 00066 // RECOGNITION 00067 size_t camera_keypoints_count; 00068 //map of DocumentIDs and their scores in the database 00069 std::map<uint32_t, float> matches_map; 00070 std::list<int> sliding_window; 00071 std::map<int, int> last_templates; 00072 00073 // PARAMETERS 00074 std::string command, database_location, images_directory, images_for_visualization_directory; 00075 int votes_count, tree_k, tree_levels, min_cluster_size, object_id; 00076 double unknown_object_threshold; 00077 int enable_clustering, enable_incremental_learning, enable_visualization, sliding_window_size, templates_to_show; 00078 double radius_adaptation_r_min, radius_adaptation_r_max, radius_adaptation_K, radius_adaptation_A; 00079 int count_templates; 00080 public: 00081 ODUFinder(); 00082 00083 ~ODUFinder(); 00084 00088 void set_visualization(bool enable_visualization_in); 00089 00093 std::string process_image(IplImage* camera_image); 00094 00095 int start(); 00096 00100 void build_database(std::string directory); 00101 00106 void process_images(std::string directory); 00107 00111 void save_database_without_tree(std::string& directory); 00112 00116 void save_database(std::string& directory); 00117 00121 void load_database(std::string& directory); 00122 00127 void add_image_to_database(vt::Document& doc, std::string& name); 00128 00131 void write_stat_summary(); 00132 00137 void extract_roi (IplImage *image , std::vector<KeypointExt*> camera_keypoints); 00138 00139 00140 protected: 00147 void trace_directory(const char* dir, const char* prefix, std::vector<FeatureVector>& images, bool onlySaveImages = false); 00148 00154 void visualize(IplImage *camera_image_in, DocumentInfo** template_document_info, std::vector<KeypointExt*> *camera_keypoints); 00155 00161 void save_result_for_sequence(std::string &best_template_filename); 00162 00165 void visualize_sequence(); 00166 00169 void clear_sequence_buffer(); 00170 00171 00177 void update_matches_map(vt::Matches& matches, size_t size); 00178 00184 void process_file(std::string& filename, std::vector<FeatureVector>& images, bool onlySaveImages = false); 00185 00190 Keypoint extract_keypoints(IplImage *image, bool frames_only = false); 00191 };