FeatureHelper.h
Go to the documentation of this file.
1 /*
2  * OpenVINS: An Open Platform for Visual-Inertial Research
3  * Copyright (C) 2018-2023 Patrick Geneva
4  * Copyright (C) 2018-2023 Guoquan Huang
5  * Copyright (C) 2018-2023 OpenVINS Contributors
6  * Copyright (C) 2018-2019 Kevin Eckenhoff
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef OV_CORE_FEATURE_HELPER_H
23 #define OV_CORE_FEATURE_HELPER_H
24 
25 #include <Eigen/Eigen>
26 #include <memory>
27 #include <mutex>
28 #include <vector>
29 
30 #include "Feature.h"
31 #include "FeatureDatabase.h"
32 #include "utils/print.h"
33 
34 namespace ov_core {
35 
42 
43 public:
60  static void compute_disparity(std::shared_ptr<ov_core::FeatureDatabase> db, double time0, double time1, double &disp_mean,
61  double &disp_var, int &total_feats) {
62 
63  // Get features seen from the first image
64  std::vector<std::shared_ptr<Feature>> feats0 = db->features_containing(time0, false, true);
65 
66  // Compute the disparity
67  std::vector<double> disparities;
68  for (auto &feat : feats0) {
69 
70  // Get the two uvs for both times
71  for (auto &campairs : feat->timestamps) {
72 
73  // First find the two timestamps
74  size_t camid = campairs.first;
75  auto it0 = std::find(feat->timestamps.at(camid).begin(), feat->timestamps.at(camid).end(), time0);
76  auto it1 = std::find(feat->timestamps.at(camid).begin(), feat->timestamps.at(camid).end(), time1);
77  if (it0 == feat->timestamps.at(camid).end() || it1 == feat->timestamps.at(camid).end())
78  continue;
79  auto idx0 = std::distance(feat->timestamps.at(camid).begin(), it0);
80  auto idx1 = std::distance(feat->timestamps.at(camid).begin(), it1);
81 
82  // Now lets calculate the disparity
83  Eigen::Vector2f uv0 = feat->uvs.at(camid).at(idx0).block(0, 0, 2, 1);
84  Eigen::Vector2f uv1 = feat->uvs.at(camid).at(idx1).block(0, 0, 2, 1);
85  disparities.push_back((uv1 - uv0).norm());
86  }
87  }
88 
89  // If no disparities, just return
90  if (disparities.size() < 2) {
91  disp_mean = -1;
92  disp_var = -1;
93  total_feats = 0;
94  }
95 
96  // Compute mean and standard deviation in respect to it
97  disp_mean = 0;
98  for (double disp_i : disparities) {
99  disp_mean += disp_i;
100  }
101  disp_mean /= (double)disparities.size();
102  disp_var = 0;
103  for (double &disp_i : disparities) {
104  disp_var += std::pow(disp_i - disp_mean, 2);
105  }
106  disp_var = std::sqrt(disp_var / (double)(disparities.size() - 1));
107  total_feats = (int)disparities.size();
108  }
109 
123  static void compute_disparity(std::shared_ptr<ov_core::FeatureDatabase> db, double &disp_mean, double &disp_var, int &total_feats,
124  double newest_time = -1, double oldest_time = -1) {
125 
126  // Compute the disparity
127  std::vector<double> disparities;
128  for (auto &feat : db->get_internal_data()) {
129  for (auto &campairs : feat.second->timestamps) {
130 
131  // Skip if only one observation
132  if (campairs.second.size() < 2)
133  continue;
134 
135  // Now lets calculate the disparity (assumes time array is monotonic)
136  size_t camid = campairs.first;
137  bool found0 = false;
138  bool found1 = false;
139  Eigen::Vector2f uv0 = Eigen::Vector2f::Zero();
140  Eigen::Vector2f uv1 = Eigen::Vector2f::Zero();
141  for (size_t idx = 0; idx < feat.second->timestamps.at(camid).size(); idx++) {
142  double time = feat.second->timestamps.at(camid).at(idx);
143  if ((oldest_time == -1 || time > oldest_time) && !found0) {
144  uv0 = feat.second->uvs.at(camid).at(idx).block(0, 0, 2, 1);
145  found0 = true;
146  continue;
147  }
148  if ((newest_time == -1 || time < newest_time) && found0) {
149  uv1 = feat.second->uvs.at(camid).at(idx).block(0, 0, 2, 1);
150  found1 = true;
151  continue;
152  }
153  }
154 
155  // If we found both an old and a new time, then we are good!
156  if (!found0 || !found1)
157  continue;
158  disparities.push_back((uv1 - uv0).norm());
159  }
160  }
161 
162  // If no disparities, just return
163  if (disparities.size() < 2) {
164  disp_mean = -1;
165  disp_var = -1;
166  total_feats = 0;
167  }
168 
169  // Compute mean and standard deviation in respect to it
170  disp_mean = 0;
171  for (double disp_i : disparities) {
172  disp_mean += disp_i;
173  }
174  disp_mean /= (double)disparities.size();
175  disp_var = 0;
176  for (double &disp_i : disparities) {
177  disp_var += std::pow(disp_i - disp_mean, 2);
178  }
179  disp_var = std::sqrt(disp_var / (double)(disparities.size() - 1));
180  total_feats = (int)disparities.size();
181  }
182 
183 private:
184  // Cannot construct this class
186 };
187 
188 } // namespace ov_core
189 
190 #endif /* OV_CORE_FEATURE_HELPER_H */
FeatureDatabase.h
ov_core::FeatureHelper::compute_disparity
static void compute_disparity(std::shared_ptr< ov_core::FeatureDatabase > db, double time0, double time1, double &disp_mean, double &disp_var, int &total_feats)
This functions will compute the disparity between common features in the two frames.
Definition: FeatureHelper.h:60
Feature.h
print.h
ov_core::FeatureHelper
Contains some nice helper functions for features.
Definition: FeatureHelper.h:41
ov_core::FeatureHelper::compute_disparity
static void compute_disparity(std::shared_ptr< ov_core::FeatureDatabase > db, double &disp_mean, double &disp_var, int &total_feats, double newest_time=-1, double oldest_time=-1)
This functions will compute the disparity over all features we have.
Definition: FeatureHelper.h:123
ov_core
Core algorithms for OpenVINS.
Definition: CamBase.h:30
ov_core::FeatureHelper::FeatureHelper
FeatureHelper()
Definition: FeatureHelper.h:185


ov_core
Author(s): Patrick Geneva , Kevin Eckenhoff , Guoquan Huang
autogenerated on Mon Dec 16 2024 03:06:46