Blender V4.3
detect.cc
Go to the documentation of this file.
1/****************************************************************************
2**
3** Copyright (c) 2011 libmv authors.
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to
7** deal in the Software without restriction, including without limitation the
8** rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9** sell copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11**
12** The above copyright notice and this permission notice shall be included in
13** all copies or substantial portions of the Software.
14**
15** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21** IN THE SOFTWARE.
22**
23****************************************************************************/
24
25#include <memory.h>
26#include <stdlib.h>
27#include <queue>
28
35
36#ifndef LIBMV_NO_FAST_DETECTOR
37# include <third_party/fast/fast.h>
38#endif
39
40#ifdef __SSE2__
41# include <emmintrin.h>
42#endif
43
44namespace libmv {
45
46namespace {
47
48// Default value for FAST minimal trackness in the DetectOptions structure.
49// TODO(sergey): Think of a better default value here.
50int kDefaultFastMinTrackness = 128;
51
52// Default value for Harris threshold in the DetectOptions structure.
53// TODO(sergey): Think of a better default value here.
54double kDefaultHarrisThreshold = 1e-5;
55
56class FeatureComparison {
57 public:
58 bool operator()(const Feature& left, const Feature& right) const {
59 return right.score > left.score;
60 }
61};
62
63// Filter the features so there are no features closer than
64// minimal distance to each other.
65// This is a naive implementation with O(n^2) asymptotic.
66void FilterFeaturesByDistance(const vector<Feature>& all_features,
67 int min_distance,
68 vector<Feature>* detected_features) {
69 const int min_distance_squared = min_distance * min_distance;
70
71 // Use priority queue to sort the features by their score.
72 //
73 // Do this on copy of the input features to prevent possible
74 // distortion in callee function behavior.
75 std::priority_queue<Feature, std::vector<Feature>, FeatureComparison>
76 priority_features;
77
78 for (int i = 0; i < all_features.size(); i++) {
79 priority_features.push(all_features.at(i));
80 }
81
82 while (!priority_features.empty()) {
83 bool ok = true;
84 Feature a = priority_features.top();
85
86 for (int i = 0; i < detected_features->size(); i++) {
87 Feature& b = detected_features->at(i);
88 if (Square(a.x - b.x) + Square(a.y - b.y) < min_distance_squared) {
89 ok = false;
90 break;
91 }
92 }
93
94 if (ok) {
95 detected_features->push_back(a);
96 }
97
98 priority_features.pop();
99 }
100}
101
102void DetectFAST(const FloatImage& grayscale_image,
103 const DetectOptions& options,
104 vector<Feature>* detected_features) {
105#ifndef LIBMV_NO_FAST_DETECTOR
106 const int min_distance = options.min_distance;
107 const int min_trackness = options.fast_min_trackness;
108 const int margin = options.margin;
109 const int width = grayscale_image.Width() - 2 * margin;
110 const int height = grayscale_image.Width() - 2 * margin;
111 const int stride = grayscale_image.Width();
112
113 scoped_array<unsigned char> byte_image(
114 FloatImageToUCharArray(grayscale_image));
115 const int byte_image_offset = margin * stride + margin;
116
117 // TODO(MatthiasF): Support targetting a feature count (binary search
118 // trackness)
119 int num_features;
120 xy* all = fast9_detect(byte_image.get() + byte_image_offset,
121 width,
122 height,
123 stride,
124 min_trackness,
125 &num_features);
126 if (num_features == 0) {
127 free(all);
128 return;
129 }
130 int* scores = fast9_score(byte_image.get() + byte_image_offset,
131 stride,
132 all,
133 num_features,
134 min_trackness);
135 // TODO(MatthiasF): merge with close feature suppression
136 xy* nonmax = nonmax_suppression(all, scores, num_features, &num_features);
137 free(all);
138 // Remove too close features
139 // TODO(MatthiasF): A resolution independent parameter would be better than
140 // distance e.g. a coefficient going from 0 (no minimal distance) to 1
141 // (optimal circle packing)
142 // FIXME(MatthiasF): this method will not necessarily give all maximum markers
143 if (num_features) {
144 vector<Feature> all_features;
145 for (int i = 0; i < num_features; ++i) {
146 all_features.push_back(Feature((float)nonmax[i].x + margin,
147 (float)nonmax[i].y + margin,
148 (float)scores[i],
149 9.0));
150 }
151 FilterFeaturesByDistance(all_features, min_distance, detected_features);
152 }
153 free(scores);
154 free(nonmax);
155#else
156 (void)grayscale_image; // Ignored.
157 (void)options; // Ignored.
158 (void)detected_features; // Ignored.
159 LOG(FATAL) << "FAST detector is disabled in this build.";
160#endif
161}
162
163#ifdef __SSE2__
164static unsigned int SAD(const ubyte* imageA,
165 const ubyte* imageB,
166 int strideA,
167 int strideB) {
168 __m128i a = _mm_setzero_si128();
169 for (int i = 0; i < 16; i++) {
170 a = _mm_adds_epu16(
171 a,
172 _mm_sad_epu8(_mm_loadu_si128((__m128i*)(imageA + i * strideA)),
173 _mm_loadu_si128((__m128i*)(imageB + i * strideB))));
174 }
175 return _mm_extract_epi16(a, 0) + _mm_extract_epi16(a, 4);
176}
177#else
178static unsigned int SAD(const ubyte* imageA,
179 const ubyte* imageB,
180 int strideA,
181 int strideB) {
182 unsigned int sad = 0;
183 for (int i = 0; i < 16; i++) {
184 for (int j = 0; j < 16; j++) {
185 sad += abs((int)imageA[i * strideA + j] - imageB[i * strideB + j]);
186 }
187 }
188 return sad;
189}
190#endif
191
192void DetectMORAVEC(const FloatImage& grayscale_image,
193 const DetectOptions& options,
194 vector<Feature>* detected_features) {
195 const int distance = options.min_distance;
196 const int margin = options.margin;
197 const unsigned char* pattern = options.moravec_pattern;
198 const int count = options.moravec_max_count;
199 const int width = grayscale_image.Width() - 2 * margin;
200 const int height = grayscale_image.Width() - 2 * margin;
201 const int stride = grayscale_image.Width();
202
203 scoped_array<unsigned char> byte_image(
204 FloatImageToUCharArray(grayscale_image));
205
206 unsigned short histogram[256];
207 memset(histogram, 0, sizeof(histogram));
208 scoped_array<ubyte> scores(new ubyte[width * height]);
209 memset(scores.get(), 0, width * height);
210 const int r = 1; // radius for self similarity comparison
211 for (int y = distance; y < height - distance; y++) {
212 for (int x = distance; x < width - distance; x++) {
213 const ubyte* s = &byte_image[y * stride + x];
214 // low self-similarity with overlapping patterns
215 // OPTI: load pattern once
216 // clang-format off
217 int score =
218 SAD(s, s-r*stride-r, stride, stride)+SAD(s, s-r*stride, stride, stride)+SAD(s, s-r*stride+r, stride, stride)+
219 SAD(s, s -r, stride, stride)+ SAD(s, s +r, stride, stride)+
220 SAD(s, s+r*stride-r, stride, stride)+SAD(s, s+r*stride, stride, stride)+SAD(s, s+r*stride+r, stride, stride);
221 // clang-format on
222
223 score /= 256; // normalize
224 if (pattern) { // find only features similar to pattern
225 score -= SAD(s, pattern, stride, 16);
226 }
227 if (score <= 16) {
228 continue; // filter very self-similar features
229 }
230 score -= 16; // translate to score/histogram values
231 if (score > 255) {
232 score = 255; // clip
233 }
234 ubyte* c = &scores[y * width + x];
235 for (int i = -distance; i < 0; i++) {
236 for (int j = -distance; j < distance; j++) {
237 int s = c[i * width + j];
238 if (s == 0) {
239 continue;
240 }
241 if (s >= score) {
242 goto nonmax;
243 }
244 c[i * width + j] = 0;
245 histogram[s]--;
246 }
247 }
248 for (int i = 0, j = -distance; j < 0; j++) {
249 int s = c[i * width + j];
250 if (s == 0) {
251 continue;
252 }
253 if (s >= score) {
254 goto nonmax;
255 }
256 c[i * width + j] = 0;
257 histogram[s]--;
258 }
259 c[0] = score, histogram[score]++;
260 nonmax: {} // Do nothing.
261 }
262 }
263 int min = 255, total = 0;
264 for (; min > 0; min--) {
265 int h = histogram[min];
266 if (total + h > count) {
267 break;
268 }
269 total += h;
270 }
271 for (int y = 16; y < height - 16; y++) {
272 for (int x = 16; x < width - 16; x++) {
273 int s = scores[y * width + x];
274 if (s > min) {
275 // Currently SAD works with the patterns of 16x16 pixels.
276 //
277 // Score calculation above uses top left corner of the
278 // patch as the origin, here we need to convert this value
279 // to a pattrn center by adding 8 pixels.
280 detected_features->push_back(
281 Feature((float)x + 8.0f, (float)y + 8.0f, (float)s, 16.0f));
282 }
283 }
284 }
285}
286
287void DetectHarris(const FloatImage& grayscale_image,
288 const DetectOptions& options,
289 vector<Feature>* detected_features) {
290 const double alpha = 0.06;
291 const double sigma = 0.9;
292
293 const int min_distance = options.min_distance;
294 const int margin = options.margin;
295 const double threshold = options.harris_threshold;
296
297 FloatImage gradient_x, gradient_y;
298 ImageDerivatives(grayscale_image, sigma, &gradient_x, &gradient_y);
299
300 FloatImage gradient_xx, gradient_yy, gradient_xy;
301 MultiplyElements(gradient_x, gradient_x, &gradient_xx);
302 MultiplyElements(gradient_y, gradient_y, &gradient_yy);
303 MultiplyElements(gradient_x, gradient_y, &gradient_xy);
304
305 FloatImage gradient_xx_blurred, gradient_yy_blurred, gradient_xy_blurred;
306 ConvolveGaussian(gradient_xx, sigma, &gradient_xx_blurred);
307 ConvolveGaussian(gradient_yy, sigma, &gradient_yy_blurred);
308 ConvolveGaussian(gradient_xy, sigma, &gradient_xy_blurred);
309
310 vector<Feature> all_features;
311 for (int y = margin; y < gradient_xx_blurred.Height() - margin; ++y) {
312 for (int x = margin; x < gradient_xx_blurred.Width() - margin; ++x) {
313 // Construct matrix
314 //
315 // A = [ Ix^2 Ix*Iy ]
316 // [ Ix*Iy Iy^2 ]
317 Mat2 A;
318 A(0, 0) = gradient_xx_blurred(y, x);
319 A(1, 1) = gradient_yy_blurred(y, x);
320 A(0, 1) = A(1, 0) = gradient_xy_blurred(y, x);
321
322 double detA = A.determinant();
323 double traceA = A.trace();
324 double harris_function = detA - alpha * traceA * traceA;
325 if (harris_function > threshold) {
326 all_features.push_back(
327 Feature((float)x, (float)y, (float)harris_function, 5.0f));
328 }
329 }
330 }
331
332 FilterFeaturesByDistance(all_features, min_distance, detected_features);
333}
334
335} // namespace
336
338 : type(DetectOptions::HARRIS),
339 margin(0),
340 min_distance(120),
341 fast_min_trackness(kDefaultFastMinTrackness),
342 moravec_max_count(0),
343 moravec_pattern(NULL),
344 harris_threshold(kDefaultHarrisThreshold) {
345}
346
347void Detect(const FloatImage& image,
348 const DetectOptions& options,
349 vector<Feature>* detected_features) {
350 // Currently all the detectors requires image to be grayscale.
351 // Do it here to avoid code duplication.
352 FloatImage grayscale_image;
353 if (image.Depth() != 1) {
354 Rgb2Gray(image, &grayscale_image);
355 } else {
356 // TODO(sergey): Find a way to avoid such image duplication/
357 grayscale_image = image;
358 }
359
360 if (options.type == DetectOptions::FAST) {
361 DetectFAST(grayscale_image, options, detected_features);
362 } else if (options.type == DetectOptions::MORAVEC) {
363 DetectMORAVEC(grayscale_image, options, detected_features);
364 } else if (options.type == DetectOptions::HARRIS) {
365 DetectHarris(grayscale_image, options, detected_features);
366 } else {
367 LOG(FATAL) << "Unknown detector has been passed to featur detection";
368 }
369}
370
371std::ostream& operator<<(std::ostream& os, const Feature& feature) {
372 os << "x: " << feature.x << ", y: " << feature.y;
373 os << ", score: " << feature.score;
374 os << ", size: " << feature.size;
375 return os;
376}
377
378} // namespace libmv
void BLI_kdtree_nd_ free(KDTree *tree)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
#define A
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
3D array (row, column, channel).
Definition array_nd.h:332
input_tx image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "preview_img") .compute_source("compositor_compute_preview.glsl") .do_static_compilation(true)
local_group_size(16, 16) .push_constant(Type b
CCL_NAMESPACE_BEGIN struct Options options
#define NULL
int count
#define LOG(severity)
Definition log.h:33
#define Square(a, x, y)
void Detect(const FloatImage &image, const DetectOptions &options, vector< Feature > *detected_features)
Definition detect.cc:347
void ImageDerivatives(const Array3Df &in, double sigma, Array3Df *gradient_x, Array3Df *gradient_y)
Definition convolve.cc:191
std::ostream & operator<<(std::ostream &os, const CameraIntrinsics &intrinsics)
A human-readable representation of the camera intrinsic parameters.
unsigned char ubyte
Definition detect.h:36
void Rgb2Gray(const ImageIn &imaIn, ImageOut *imaOut)
unsigned char * FloatImageToUCharArray(const Image &image)
Array3Df FloatImage
void MultiplyElements(const AArrayType &a, const BArrayType &b, CArrayType *c)
Definition array_nd.h:398
void ConvolveGaussian(const Array3Df &in, double sigma, Array3Df *out_pointer)
Definition convolve.cc:182
Eigen::Matrix< double, 2, 2 > Mat2
Definition numeric.h:70
float distance(float a, float b)
#define min(a, b)
Definition sort.c:32
float size
Definition detect.h:59
float score
Definition detect.h:53
ccl_device_inline int abs(int x)
Definition util/math.h:120
int xy[2]
Definition wm_draw.cc:170