36#ifndef LIBMV_NO_FAST_DETECTOR
37# include <third_party/fast/fast.h>
41# include <emmintrin.h>
50int kDefaultFastMinTrackness = 128;
54double kDefaultHarrisThreshold = 1
e-5;
56class FeatureComparison {
69 const int min_distance_squared = min_distance * min_distance;
75 std::priority_queue<Feature, std::vector<Feature>, FeatureComparison>
78 for (
int i = 0;
i < all_features.size();
i++) {
79 priority_features.push(all_features.at(
i));
82 while (!priority_features.empty()) {
84 Feature a = priority_features.top();
86 for (
int i = 0;
i < detected_features->size();
i++) {
95 detected_features->push_back(a);
98 priority_features.pop();
102void DetectFAST(
const FloatImage& grayscale_image,
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();
115 const int byte_image_offset = margin * stride + margin;
120 xy*
all = fast9_detect(byte_image.get() + byte_image_offset,
126 if (num_features == 0) {
130 int* scores = fast9_score(byte_image.get() + byte_image_offset,
136 xy* nonmax = nonmax_suppression(
all, scores, num_features, &num_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,
151 FilterFeaturesByDistance(all_features, min_distance, detected_features);
156 (void)grayscale_image;
158 (void)detected_features;
159 LOG(FATAL) <<
"FAST detector is disabled in this build.";
164static unsigned int SAD(
const ubyte* imageA,
168 __m128i a = _mm_setzero_si128();
169 for (
int i = 0;
i < 16;
i++) {
172 _mm_sad_epu8(_mm_loadu_si128((__m128i*)(imageA +
i * strideA)),
173 _mm_loadu_si128((__m128i*)(imageB +
i * strideB))));
175 return _mm_extract_epi16(a, 0) + _mm_extract_epi16(a, 4);
178static unsigned int SAD(
const ubyte* imageA,
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]);
192void DetectMORAVEC(
const FloatImage& grayscale_image,
196 const int margin =
options.margin;
197 const unsigned char* pattern =
options.moravec_pattern;
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();
206 unsigned short histogram[256];
207 memset(histogram, 0,
sizeof(histogram));
209 memset(scores.get(), 0, width * height);
213 const ubyte* s = &byte_image[
y * stride +
x];
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);
225 score -= SAD(s, pattern, stride, 16);
234 ubyte* c = &scores[
y * width +
x];
237 int s = c[
i * width + j];
244 c[
i * width + j] = 0;
248 for (
int i = 0, j = -
distance; j < 0; j++) {
249 int s = c[
i * width + j];
256 c[
i * width + j] = 0;
259 c[0] = score, histogram[score]++;
263 int min = 255, total = 0;
265 int h = histogram[
min];
266 if (total + h >
count) {
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];
280 detected_features->push_back(
281 Feature((
float)
x + 8.0f, (
float)
y + 8.0f, (
float)s, 16.0f));
287void DetectHarris(
const FloatImage& grayscale_image,
290 const double alpha = 0.06;
291 const double sigma = 0.9;
293 const int min_distance =
options.min_distance;
294 const int margin =
options.margin;
295 const double threshold =
options.harris_threshold;
300 FloatImage gradient_xx, gradient_yy, gradient_xy;
305 FloatImage gradient_xx_blurred, gradient_yy_blurred, gradient_xy_blurred;
311 for (
int y = margin;
y < gradient_xx_blurred.
Height() - margin; ++
y) {
312 for (
int x = margin;
x < gradient_xx_blurred.
Width() - margin; ++
x) {
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);
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));
332 FilterFeaturesByDistance(all_features, min_distance, detected_features);
353 if (image.
Depth() != 1) {
357 grayscale_image = image;
361 DetectFAST(grayscale_image,
options, detected_features);
363 DetectMORAVEC(grayscale_image,
options, detected_features);
365 DetectHarris(grayscale_image,
options, detected_features);
367 LOG(FATAL) <<
"Unknown detector has been passed to featur detection";
372 os <<
"x: " << feature.
x <<
", y: " << feature.
y;
373 os <<
", score: " << feature.
score;
374 os <<
", size: " << feature.
size;
void BLI_kdtree_nd_ free(KDTree *tree)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
CCL_NAMESPACE_BEGIN struct Options options
bool all(VecOp< bool, D >) RET
float distance(VecOp< float, D >, VecOp< float, D >) RET
void Detect(const FloatImage &image, const DetectOptions &options, vector< Feature > *detected_features)
void ImageDerivatives(const Array3Df &in, double sigma, Array3Df *gradient_x, Array3Df *gradient_y)
std::ostream & operator<<(std::ostream &os, const CameraIntrinsics &intrinsics)
A human-readable representation of the camera intrinsic parameters.
void Rgb2Gray(const ImageIn &imaIn, ImageOut *imaOut)
unsigned char * FloatImageToUCharArray(const Image &image)
std::vector< ElementType, Eigen::aligned_allocator< ElementType > > vector
void MultiplyElements(const AArrayType &a, const BArrayType &b, CArrayType *c)
void ConvolveGaussian(const Array3Df &in, double sigma, Array3Df *out_pointer)
Eigen::Matrix< double, 2, 2 > Mat2
unsigned char * moravec_pattern