ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
visual_index_test.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 #define TEST_NAME "retrieval/visual_index"
33 #include "util/testing.h"
34 
35 #include "retrieval/visual_index.h"
36 
37 using namespace colmap;
38 using namespace colmap::retrieval;
39 
40 template <typename kDescType, int kDescDim, int kEmbeddingDim>
43 
44  SetPRNGSeed(0);
45 
46  {
47  VisualIndexType visual_index;
48  BOOST_CHECK_EQUAL(visual_index.NumVisualWords(), 0);
49  }
50 
51  {
52  typename VisualIndexType::DescType descriptors =
53  VisualIndexType::DescType::Random(50, kDescDim);
54  VisualIndexType visual_index;
55  BOOST_CHECK_EQUAL(visual_index.NumVisualWords(), 0);
56  typename VisualIndexType::BuildOptions build_options;
57  build_options.num_visual_words = 5;
58  build_options.branching = 5;
59  visual_index.Build(build_options, descriptors);
60  BOOST_CHECK_EQUAL(visual_index.NumVisualWords(), 5);
61  }
62 
63  {
64  typename VisualIndexType::DescType descriptors =
65  VisualIndexType::DescType::Random(1000, kDescDim);
66  VisualIndexType visual_index;
67  BOOST_CHECK_EQUAL(visual_index.NumVisualWords(), 0);
68  typename VisualIndexType::BuildOptions build_options;
69  build_options.num_visual_words = 100;
70  build_options.branching = 10;
71  visual_index.Build(build_options, descriptors);
72  BOOST_CHECK_EQUAL(visual_index.NumVisualWords(), 100);
73 
74  typename VisualIndexType::IndexOptions index_options;
75  typename VisualIndexType::GeomType keypoints1(50);
76  typename VisualIndexType::DescType descriptors1 =
77  VisualIndexType::DescType::Random(50, kDescDim);
78  visual_index.Add(index_options, 1, keypoints1, descriptors1);
79  typename VisualIndexType::GeomType keypoints2(50);
80  typename VisualIndexType::DescType descriptors2 =
81  VisualIndexType::DescType::Random(50, kDescDim);
82  visual_index.Add(index_options, 2, keypoints2, descriptors2);
83  visual_index.Prepare();
84 
85  typename VisualIndexType::QueryOptions query_options;
86  std::vector<ImageScore> image_scores;
87  visual_index.Query(query_options, descriptors1, &image_scores);
88  BOOST_CHECK_EQUAL(image_scores.size(), 2);
89  BOOST_CHECK_EQUAL(image_scores[0].image_id, 1);
90  BOOST_CHECK_EQUAL(image_scores[1].image_id, 2);
91  BOOST_CHECK_GT(image_scores[0].score, image_scores[1].score);
92 
93  query_options.max_num_images = 1;
94  visual_index.Query(query_options, descriptors1, &image_scores);
95  BOOST_CHECK_EQUAL(image_scores.size(), 1);
96  BOOST_CHECK_EQUAL(image_scores[0].image_id, 1);
97 
98  query_options.max_num_images = 3;
99  visual_index.Query(query_options, descriptors1, &image_scores);
100  BOOST_CHECK_EQUAL(image_scores.size(), 2);
101  BOOST_CHECK_EQUAL(image_scores[0].image_id, 1);
102  BOOST_CHECK_EQUAL(image_scores[1].image_id, 2);
103  BOOST_CHECK_GT(image_scores[0].score, image_scores[1].score);
104  }
105 }
106 
107 BOOST_AUTO_TEST_CASE(TestVocabTree) {
108  TestVocabTreeType<uint8_t, 128, 64>();
109  TestVocabTreeType<uint8_t, 64, 64>();
110  TestVocabTreeType<uint8_t, 32, 16>();
111  TestVocabTreeType<int, 32, 16>();
112  TestVocabTreeType<float, 32, 16>();
113  TestVocabTreeType<double, 32, 16>();
114 }
void SetPRNGSeed(unsigned seed)
Definition: random.cc:40
CorePointDescSet * descriptors
void TestVocabTreeType()
BOOST_AUTO_TEST_CASE(TestVocabTree)