ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
utils.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #include "feature/utils.h"
33 
34 #include "util/math.h"
35 
36 namespace colmap {
37 
38 std::vector<Eigen::Vector2d> FeatureKeypointsToPointsVector(
39  const FeatureKeypoints& keypoints) {
40  std::vector<Eigen::Vector2d> points(keypoints.size());
41  for (size_t i = 0; i < keypoints.size(); ++i) {
42  points[i] = Eigen::Vector2d(keypoints[i].x, keypoints[i].y);
43  }
44  return points;
45 }
46 
48  const Eigen::MatrixXf& descriptors) {
49  return descriptors.rowwise().normalized();
50 }
51 
53  const Eigen::MatrixXf& descriptors) {
54  Eigen::MatrixXf descriptors_normalized(descriptors.rows(),
55  descriptors.cols());
56  for (Eigen::MatrixXf::Index r = 0; r < descriptors.rows(); ++r) {
57  const float norm = descriptors.row(r).lpNorm<1>();
58  descriptors_normalized.row(r) = descriptors.row(r) / norm;
59  descriptors_normalized.row(r) =
60  descriptors_normalized.row(r).array().sqrt();
61  }
62  return descriptors_normalized;
63 }
64 
66  const Eigen::MatrixXf& descriptors) {
67  FeatureDescriptors descriptors_unsigned_byte(descriptors.rows(),
68  descriptors.cols());
69  for (Eigen::MatrixXf::Index r = 0; r < descriptors.rows(); ++r) {
70  for (Eigen::MatrixXf::Index c = 0; c < descriptors.cols(); ++c) {
71  const float scaled_value = std::round(512.0f * descriptors(r, c));
72  descriptors_unsigned_byte(r, c) =
73  TruncateCast<float, uint8_t>(scaled_value);
74  }
75  }
76  return descriptors_unsigned_byte;
77 }
78 
81  const size_t num_features) {
82  CHECK_EQ(keypoints->size(), descriptors->rows());
83  CHECK_GT(num_features, 0);
84 
85  if (static_cast<size_t>(descriptors->rows()) <= num_features) {
86  return;
87  }
88 
89  FeatureKeypoints top_scale_keypoints;
90  FeatureDescriptors top_scale_descriptors;
91 
92  std::vector<std::pair<size_t, float>> scales;
93  scales.reserve(static_cast<size_t>(keypoints->size()));
94  for (size_t i = 0; i < keypoints->size(); ++i) {
95  scales.emplace_back(i, (*keypoints)[i].ComputeScale());
96  }
97 
98  std::partial_sort(scales.begin(), scales.begin() + num_features, scales.end(),
99  [](const std::pair<size_t, float> scale1,
100  const std::pair<size_t, float> scale2) {
101  return scale1.second > scale2.second;
102  });
103 
104  top_scale_keypoints.resize(num_features);
105  top_scale_descriptors.resize(num_features, descriptors->cols());
106  for (size_t i = 0; i < num_features; ++i) {
107  top_scale_keypoints[i] = (*keypoints)[scales[i].first];
108  top_scale_descriptors.row(i) = descriptors->row(scales[i].first);
109  }
110 
111  *keypoints = top_scale_keypoints;
112  *descriptors = top_scale_descriptors;
113 }
114 
115 } // namespace colmap
int points
normal_z y
normal_z x
Eigen::MatrixXf L1RootNormalizeFeatureDescriptors(const Eigen::MatrixXf &descriptors)
Definition: utils.cc:52
void ExtractTopScaleFeatures(FeatureKeypoints *keypoints, FeatureDescriptors *descriptors, const size_t num_features)
Definition: utils.cc:79
Eigen::Matrix< uint8_t, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > FeatureDescriptors
Definition: types.h:79
FeatureDescriptors FeatureDescriptorsToUnsignedByte(const Eigen::MatrixXf &descriptors)
Definition: utils.cc:65
Eigen::MatrixXf L2NormalizeFeatureDescriptors(const Eigen::MatrixXf &descriptors)
Definition: utils.cc:47
std::vector< Eigen::Vector2d > FeatureKeypointsToPointsVector(const FeatureKeypoints &keypoints)
Definition: utils.cc:38
std::vector< FeatureKeypoint > FeatureKeypoints
Definition: types.h:77
Eigen::MatrixXd::Index Index
Definition: knncpp.h:26
CorePointDescSet * descriptors