|
| 1 | +#include <beam_cv/ImageDatabase.h> |
| 2 | + |
| 3 | +namespace beam_cv { |
| 4 | + |
| 5 | +ImageDatabase::ImageDatabase() { |
| 6 | + // construct default detector and descriptor |
| 7 | + beam_cv::GFTTDetector::Params detector_params; |
| 8 | + beam_cv::ORBDescriptor::Params descriptor_params; |
| 9 | + detector_ = beam_cv::GFTTDetector(detector_params); |
| 10 | + descriptor_ = beam_cv::ORBDescriptor(descriptor_params); |
| 11 | + // construct database |
| 12 | + DBoW3::Vocabulary default_vocab(DEFAULT_VOCAB_PATH); |
| 13 | + dbow_db_ = std::make_shared<DBoW3::Database>(default_vocab); |
| 14 | + index_to_timestamp_map_ = json::object(); |
| 15 | +} |
| 16 | + |
| 17 | +ImageDatabase::ImageDatabase( |
| 18 | + const beam_cv::GFTTDetector::Params& detector_params, |
| 19 | + const beam_cv::ORBDescriptor::Params& descriptor_params) { |
| 20 | + // construct detector and descriptor |
| 21 | + detector_ = beam_cv::GFTTDetector(detector_params); |
| 22 | + descriptor_ = beam_cv::ORBDescriptor(descriptor_params); |
| 23 | + // construct database |
| 24 | + DBoW3::Vocabulary default_vocab(DEFAULT_VOCAB_PATH); |
| 25 | + dbow_db_ = std::make_shared<DBoW3::Database>(default_vocab); |
| 26 | + index_to_timestamp_map_ = json::object(); |
| 27 | +} |
| 28 | + |
| 29 | +ImageDatabase::ImageDatabase( |
| 30 | + const beam_cv::GFTTDetector::Params& detector_params, |
| 31 | + const beam_cv::ORBDescriptor::Params& descriptor_params, |
| 32 | + const std::string& dbow_file_path, |
| 33 | + const std::string& timestamps_file_path) { |
| 34 | + // construct detector and descriptor |
| 35 | + detector_ = beam_cv::GFTTDetector(detector_params); |
| 36 | + descriptor_ = beam_cv::ORBDescriptor(descriptor_params); |
| 37 | + // construct database |
| 38 | + dbow_db_ = std::make_shared<DBoW3::Database>(dbow_file_path); |
| 39 | + std::ifstream timestamps_file(timestamps_file_path); |
| 40 | + index_to_timestamp_map_ = json::object(); |
| 41 | + timestamps_file >> index_to_timestamp_map_; |
| 42 | +} |
| 43 | + |
| 44 | +void ImageDatabase::SaveDatabase(const std::string& dbow_file_path, |
| 45 | + const std::string& timestamps_file_path) { |
| 46 | + dbow_db_->save(dbow_file_path); |
| 47 | + std::ofstream timestamps_file(timestamps_file_path); |
| 48 | + timestamps_file << std::setw(4) << index_to_timestamp_map_ << std::endl; |
| 49 | +} |
| 50 | + |
| 51 | +cv::Mat ImageDatabase::GetWord(const cv::Mat& descriptor) { |
| 52 | + auto vocab = dbow_db_->getVocabulary(); |
| 53 | + auto word_id = vocab->transform(descriptor); |
| 54 | + return vocab->getWord(word_id); |
| 55 | +} |
| 56 | + |
| 57 | +std::vector<unsigned int> |
| 58 | + ImageDatabase::QueryDatabase(const cv::Mat& query_image, int N) { |
| 59 | + std::vector<cv::KeyPoint> kps = detector_.DetectFeatures(query_image); |
| 60 | + cv::Mat features = descriptor_.ExtractDescriptors(query_image, kps); |
| 61 | + DBoW3::QueryResults results; |
| 62 | + dbow_db_->query(features, results, N); |
| 63 | + |
| 64 | + std::vector<unsigned int> indices; |
| 65 | + for (size_t i = 0; i < results.size(); i++) { |
| 66 | + DBoW3::Result res = results[i]; |
| 67 | + indices.push_back(res.Id); |
| 68 | + } |
| 69 | + return indices; |
| 70 | +} |
| 71 | + |
| 72 | +unsigned int ImageDatabase::AddImage(const cv::Mat& image, |
| 73 | + const ros::Time& timestamp) { |
| 74 | + std::vector<cv::KeyPoint> kps = detector_.DetectFeatures(image); |
| 75 | + cv::Mat features = descriptor_.ExtractDescriptors(image, kps); |
| 76 | + unsigned int idx = dbow_db_->add(features); |
| 77 | + index_to_timestamp_map_[std::to_string(idx)] = timestamp.toSec(); |
| 78 | + return idx; |
| 79 | +} |
| 80 | + |
| 81 | +beam::opt<ros::Time> ImageDatabase::GetImageTimestamp(unsigned int index) { |
| 82 | + std::string index_str = std::to_string(index); |
| 83 | + if (!index_to_timestamp_map_.contains(index_str)) { return {}; } |
| 84 | + double time = index_to_timestamp_map_[std::to_string(index)]; |
| 85 | + return ros::Time(time); |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace beam_cv |
0 commit comments