Welcome to mirror list, hosted at ThFree Co, Russian Federation.

detect_test.cc « simple_pipeline « libmv « libmv « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b226ad96595f5e56a9352176b6aff65b04a80ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2014 libmv authors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

#include "libmv/simple_pipeline/detect.h"

#include "testing/testing.h"
#include "libmv/logging/logging.h"

namespace libmv {

namespace {

void PreformSinglePointTest(const DetectOptions &options) {
  // Prepare the image.
  FloatImage image(15, 15);
  image.fill(1.0);
  image(7, 7) = 0.0;

  // Run the detector.
  vector<Feature> detected_features;
  Detect(image, options, &detected_features);

  // Check detected features matches our expectations.
  EXPECT_EQ(1, detected_features.size());
  if (detected_features.size() == 1) {
    Feature &feature = detected_features[0];
    EXPECT_EQ(7, feature.x);
    EXPECT_EQ(7, feature.y);
  }
}

#if 0
void PreformCheckerBoardTest(const DetectOptions &options) {
  // Prepare the image.
  FloatImage image(30, 30);
  for (int y = 0; y < image.Height(); ++y) {
    for (int x = 0; x < image.Width(); ++x) {
      image(y, x) = (x / 10 + y / 10) % 2 ? 1.0 : 0.0;
    }
  }

  // Run the detector.
  vector<Feature> detected_features;
  Detect(image, options, &detected_features);

  // Check detected features matches our expectations.

  // We expect here only corners of a center square to be
  // considered a feature points.
  EXPECT_EQ(4, detected_features.size());

  // We don't know which side of the corner detector will choose,
  // so what we're checking here is that detected feature is from
  // any side of the corner.
  //
  // This doesn't check whether there're multiple features which
  // are placed on different sides of the same corner. The way we
  // deal with this is requiring min_distance to be greater than 2px.
  for (int i = 0; i < detected_features.size(); ++i) {
    Feature &feature = detected_features[i];
    int rounded_x = ((feature.x + 1) / 10) * 10,
        rounded_y = ((feature.y + 1) / 10) * 10;
    EXPECT_LE(1, std::abs(feature.x - rounded_x));
    EXPECT_LE(1, std::abs(feature.y - rounded_y));
  }
}
#endif

void CheckExpectedFeatures(const vector<Feature> &detected_features,
                           const vector<Feature> &expected_features) {
  EXPECT_EQ(expected_features.size(), detected_features.size());

  // That's unsafe to iterate over vectors when their lengths
  // doesn't match. And it doesn't make any sense actually since
  // the test will already be considered failed here.
  if (expected_features.size() != detected_features.size()) {
    return;
  }

  for (int i = 0; i < expected_features.size(); ++i) {
    const Feature &extected_feature = expected_features[i];
    bool found = false;
    for (int j = 0; j < detected_features.size(); ++j) {
      const Feature &detected_feature = detected_features[j];
      if (extected_feature.x == detected_feature.x &&
          extected_feature.y == detected_feature.y) {
        found = true;
        break;
      }
    }
    EXPECT_TRUE(found);
  }
}

void PreformSingleTriangleTest(const DetectOptions &options) {
  // Prepare the image.
  FloatImage image(15, 21);
  image.fill(1.0);

  int vertex_x = 10, vertex_y = 5;
  for (int i = 0; i < 6; ++i) {
    int current_x = vertex_x - i,
        current_y = vertex_y + i;
    for (int j = 0; j < i * 2 + 1; ++j, ++current_x) {
      image(current_y, current_x) = 0.0;
    }
  }

  // Run the detector.
  vector<Feature> detected_features;
  Detect(image, options, &detected_features);

  // Check detected features matches our expectations.
  vector<Feature> expected_features;
  expected_features.push_back(Feature(6, 10));
  expected_features.push_back(Feature(14, 10));
  expected_features.push_back(Feature(10, 6));

  CheckExpectedFeatures(detected_features, expected_features);
}

}  // namespace

#ifndef LIBMV_NO_FAST_DETECTOR
TEST(Detect, FASTSinglePointTest) {
  DetectOptions options;
  options.type = DetectOptions::FAST;
  options.min_distance = 0;
  options.fast_min_trackness = 1;

  PreformSinglePointTest(options);
}
#endif  // LIBMV_NO_FAST_DETECTOR

#if 0
// TODO(sergey): FAST doesn't detect checker board corners, but should it?
TEST(Detect, FASTCheckerBoardTest) {
  DetectOptions options;
  options.type = DetectOptions::FAST;
  options.min_distance = 0;
  options.fast_min_trackness = 1;

  PreformCheckerBoardTest(options);
}
#endif

#if 0
// TODO(sergey): FAST doesn't detect triangle corners!
TEST(Detect, FASTSingleTriangleTest) {
  DetectOptions options;
  options.type = DetectOptions::FAST;
  options.margin = 3;
  options.min_distance = 0;
  options.fast_min_trackness = 2;

  PreformSingleTriangleTest(options);
}
#endif

#if 0
// TODO(sergey): This doesn't actually detect single point,
// but should it or it's expected that Moravec wouldn't consider
// single point as  feature?
//
// Uncomment this or remove as soon as we know answer for the
// question.
TEST(Detect, MoravecSinglePointTest) {
  DetectOptions options;
  options.type = DetectOptions::MORAVEC;
  options.min_distance = 0;
  options.moravec_max_count = 10;

  PreformSinglePointTest(options);
}

// TODO(sergey): Moravec doesn't detect checker board corners, but should it?
TEST(Detect, MoravecCheckerBoardTest) {
  DetectOptions options;
  options.type = DetectOptions::MORAVEC;
  options.min_distance = 0;
  options.moravec_max_count = 10;

  PreformCheckerBoardTest(options);
}
#endif

TEST(Detect, HarrisSinglePointTest) {
  DetectOptions options;
  options.type = DetectOptions::HARRIS;

  // Set this to non-zero so image corners are not considered
  // a feature points and avoid center point neighbors to be
  // considered a features as well.
  options.margin = 3;
  options.min_distance = 3;

  PreformSinglePointTest(options);
}

TEST(Detect, HarrisSingleTriangleTest) {
  DetectOptions options;
  options.type = DetectOptions::HARRIS;

  options.margin = 3;
  options.min_distance = 2;
  options.harris_threshold = 1e-3;

  PreformSingleTriangleTest(options);
}

// TODO(sergey): Add tests for margin option.

// TODO(sergey): Add tests for min_distance option.

}  // namespace libmv