1 /*********************************************************************
2 * Software License Agreement (BSD License)
4 * Copyright (C) 2011 The Autonomous Systems Lab (ASL), ETH Zurich,
5 * Stefan Leutenegger, Simon Lynen and Margarita Chli.
6 * Copyright (c) 2009, Willow Garage, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of the Willow Garage nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *********************************************************************/
38 BRISK - Binary Robust Invariant Scalable Keypoints
39 Reference implementation of
40 [1] Stefan Leutenegger,Margarita Chli and Roland Siegwart, BRISK:
41 Binary Robust Invariant Scalable Keypoints, in Proceedings of
42 the IEEE International Conference on Computer Vision (ICCV2011).
45 #include <opencv2/features2d/features2d.hpp>
46 #include <opencv2/core/core.hpp>
47 #include <opencv2/imgproc/imgproc.hpp>
51 #include "fast_score.hpp"
56 // a layer in the Brisk detector pyramid
57 class CV_EXPORTS BriskLayer
60 // constructor arguments
61 struct CV_EXPORTS CommonParams
63 static const int HALFSAMPLE = 0;
64 static const int TWOTHIRDSAMPLE = 1;
66 // construct a base layer
67 BriskLayer(const cv::Mat& img, float scale = 1.0f, float offset = 0.0f);
69 BriskLayer(const BriskLayer& layer, int mode);
71 // Fast/Agast without non-max suppression
73 getAgastPoints(int threshold, std::vector<cv::KeyPoint>& keypoints);
75 // get scores - attention, this is in layer coordinates, not scale=1 coordinates!
77 getAgastScore(int x, int y, int threshold) const;
79 getAgastScore_5_8(int x, int y, int threshold) const;
81 getAgastScore(float xf, float yf, int threshold, float scale = 1.0f) const;
107 halfsample(const cv::Mat& srcimg, cv::Mat& dstimg);
108 // two third sampling
110 twothirdsample(const cv::Mat& srcimg, cv::Mat& dstimg);
113 // access gray values (smoothed/interpolated)
115 value(const cv::Mat& mat, float xf, float yf, float scale) const;
119 cv::Mat_<uchar> scores_;
120 // coordinate transformation
124 cv::Ptr<cv::FastFeatureDetector> fast_9_16_;
129 class CV_EXPORTS BriskScaleSpace
132 // construct telling the octaves number:
133 BriskScaleSpace(int _octaves = 3);
136 // construct the image pyramids
138 constructPyramid(const cv::Mat& image);
142 getKeypoints(const int _threshold, std::vector<cv::KeyPoint>& keypoints);
145 // nonmax suppression:
147 isMax2D(const int layer, const int x_layer, const int y_layer);
148 // 1D (scale axis) refinement:
150 refine1D(const float s_05, const float s0, const float s05, float& max) const; // around octave
152 refine1D_1(const float s_05, const float s0, const float s05, float& max) const; // around intra
154 refine1D_2(const float s_05, const float s0, const float s05, float& max) const; // around octave 0 only
155 // 2D maximum refinement:
157 subpixel2D(const int s_0_0, const int s_0_1, const int s_0_2, const int s_1_0, const int s_1_1, const int s_1_2,
158 const int s_2_0, const int s_2_1, const int s_2_2, float& delta_x, float& delta_y) const;
159 // 3D maximum refinement centered around (x_layer,y_layer)
161 refine3D(const int layer, const int x_layer, const int y_layer, float& x, float& y, float& scale, bool& ismax) const;
163 // interpolated score access with recalculation when needed:
165 getScoreAbove(const int layer, const int x_layer, const int y_layer) const;
167 getScoreBelow(const int layer, const int x_layer, const int y_layer) const;
169 // return the maximum of score patches above or below
171 getScoreMaxAbove(const int layer, const int x_layer, const int y_layer, const int threshold, bool& ismax,
172 float& dx, float& dy) const;
174 getScoreMaxBelow(const int layer, const int x_layer, const int y_layer, const int threshold, bool& ismax,
175 float& dx, float& dy) const;
177 // the image pyramids:
179 std::vector<BriskLayer> pyramid_;
181 // some constant parameters:
182 static const float safetyFactor_;
183 static const float basicSize_;
186 const float BRISK::basicSize_ = 12.0f;
187 const unsigned int BRISK::scales_ = 64;
188 const float BRISK::scalerange_ = 30.f; // 40->4 Octaves - else, this needs to be adjusted...
189 const unsigned int BRISK::n_rot_ = 1024; // discretization of the rotation look-up
191 const float BriskScaleSpace::safetyFactor_ = 1.0f;
192 const float BriskScaleSpace::basicSize_ = 12.0f;
195 BRISK::BRISK(int thresh, int octaves_in, float patternScale)
198 octaves = octaves_in;
200 std::vector<float> rList;
201 std::vector<int> nList;
203 // this is the standard pattern found to be suitable also
206 const double f = 0.85 * patternScale;
208 rList[0] = (float)(f * 0.);
209 rList[1] = (float)(f * 2.9);
210 rList[2] = (float)(f * 4.9);
211 rList[3] = (float)(f * 7.4);
212 rList[4] = (float)(f * 10.8);
220 generateKernel(rList, nList, (float)(5.85 * patternScale), (float)(8.2 * patternScale));
223 BRISK::BRISK(std::vector<float> &radiusList, std::vector<int> &numberList, float dMax, float dMin,
224 std::vector<int> indexChange)
226 generateKernel(radiusList, numberList, dMax, dMin, indexChange);
230 BRISK::generateKernel(std::vector<float> &radiusList, std::vector<int> &numberList, float dMax,
231 float dMin, std::vector<int> indexChange)
237 // get the total number of points
238 const int rings = (int)radiusList.size();
239 assert(radiusList.size()!=0&&radiusList.size()==numberList.size());
240 points_ = 0; // remember the total number of points
241 for (int ring = 0; ring < rings; ring++)
243 points_ += numberList[ring];
245 // set up the patterns
246 patternPoints_ = new BriskPatternPoint[points_ * scales_ * n_rot_];
247 BriskPatternPoint* patternIterator = patternPoints_;
249 // define the scale discretization:
250 static const float lb_scale = (float)(log(scalerange_) / log(2.0));
251 static const float lb_scale_step = lb_scale / (scales_);
253 scaleList_ = new float[scales_];
254 sizeList_ = new unsigned int[scales_];
256 const float sigma_scale = 1.3f;
258 for (unsigned int scale = 0; scale < scales_; ++scale)
260 scaleList_[scale] = (float)pow((double) 2.0, (double) (scale * lb_scale_step));
261 sizeList_[scale] = 0;
263 // generate the pattern points look-up
265 for (size_t rot = 0; rot < n_rot_; ++rot)
267 theta = double(rot) * 2 * CV_PI / double(n_rot_); // this is the rotation of the feature
268 for (int ring = 0; ring < rings; ++ring)
270 for (int num = 0; num < numberList[ring]; ++num)
272 // the actual coordinates on the circle
273 alpha = (double(num)) * 2 * CV_PI / double(numberList[ring]);
274 patternIterator->x = (float)(scaleList_[scale] * radiusList[ring] * cos(alpha + theta)); // feature rotation plus angle of the point
275 patternIterator->y = (float)(scaleList_[scale] * radiusList[ring] * sin(alpha + theta));
276 // and the gaussian kernel sigma
279 patternIterator->sigma = sigma_scale * scaleList_[scale] * 0.5f;
283 patternIterator->sigma = (float)(sigma_scale * scaleList_[scale] * (double(radiusList[ring]))
284 * sin(CV_PI / numberList[ring]));
286 // adapt the sizeList if necessary
287 const unsigned int size = cvCeil(((scaleList_[scale] * radiusList[ring]) + patternIterator->sigma)) + 1;
288 if (sizeList_[scale] < size)
290 sizeList_[scale] = size;
293 // increment the iterator
300 // now also generate pairings
301 shortPairs_ = new BriskShortPair[points_ * (points_ - 1) / 2];
302 longPairs_ = new BriskLongPair[points_ * (points_ - 1) / 2];
306 // fill indexChange with 0..n if empty
307 unsigned int indSize = (unsigned int)indexChange.size();
310 indexChange.resize(points_ * (points_ - 1) / 2);
311 indSize = (unsigned int)indexChange.size();
313 for (unsigned int i = 0; i < indSize; i++)
317 const float dMin_sq = dMin_ * dMin_;
318 const float dMax_sq = dMax_ * dMax_;
319 for (unsigned int i = 1; i < points_; i++)
321 for (unsigned int j = 0; j < i; j++)
322 { //(find all the pairs)
323 // point pair distance:
324 const float dx = patternPoints_[j].x - patternPoints_[i].x;
325 const float dy = patternPoints_[j].y - patternPoints_[i].y;
326 const float norm_sq = (dx * dx + dy * dy);
327 if (norm_sq > dMin_sq)
329 // save to long pairs
330 BriskLongPair& longPair = longPairs_[noLongPairs_];
331 longPair.weighted_dx = int((dx / (norm_sq)) * 2048.0 + 0.5);
332 longPair.weighted_dy = int((dy / (norm_sq)) * 2048.0 + 0.5);
337 else if (norm_sq < dMax_sq)
339 // save to short pairs
340 assert(noShortPairs_<indSize);
341 // make sure the user passes something sensible
342 BriskShortPair& shortPair = shortPairs_[indexChange[noShortPairs_]];
351 strings_ = (int) ceil((float(noShortPairs_)) / 128.0) * 4 * 4;
354 // simple alternative:
356 BRISK::smoothedIntensity(const cv::Mat& image, const cv::Mat& integral, const float key_x,
357 const float key_y, const unsigned int scale, const unsigned int rot,
358 const unsigned int point) const
361 // get the float position
362 const BriskPatternPoint& briskPoint = patternPoints_[scale * n_rot_ * points_ + rot * points_ + point];
363 const float xf = briskPoint.x + key_x;
364 const float yf = briskPoint.y + key_y;
365 const int x = int(xf);
366 const int y = int(yf);
367 const int& imagecols = image.cols;
370 const float sigma_half = briskPoint.sigma;
371 const float area = 4.0f * sigma_half * sigma_half;
375 if (sigma_half < 0.5)
377 //interpolation multipliers:
378 const int r_x = (int)((xf - x) * 1024);
379 const int r_y = (int)((yf - y) * 1024);
380 const int r_x_1 = (1024 - r_x);
381 const int r_y_1 = (1024 - r_y);
382 const uchar* ptr = &image.at<uchar>(y, x);
383 size_t step = image.step;
385 ret_val = r_x_1 * r_y_1 * ptr[0] + r_x * r_y_1 * ptr[1] +
386 r_x * r_y * ptr[step] + r_x_1 * r_y * ptr[step+1];
387 return (ret_val + 512) / 1024;
390 // this is the standard case (simple, not speed optimized yet):
393 const int scaling = (int)(4194304.0 / area);
394 const int scaling2 = int(float(scaling) * area / 1024.0);
396 // the integral image is larger:
397 const int integralcols = imagecols + 1;
400 const float x_1 = xf - sigma_half;
401 const float x1 = xf + sigma_half;
402 const float y_1 = yf - sigma_half;
403 const float y1 = yf + sigma_half;
405 const int x_left = int(x_1 + 0.5);
406 const int y_top = int(y_1 + 0.5);
407 const int x_right = int(x1 + 0.5);
408 const int y_bottom = int(y1 + 0.5);
410 // overlap area - multiplication factors:
411 const float r_x_1 = float(x_left) - x_1 + 0.5f;
412 const float r_y_1 = float(y_top) - y_1 + 0.5f;
413 const float r_x1 = x1 - float(x_right) + 0.5f;
414 const float r_y1 = y1 - float(y_bottom) + 0.5f;
415 const int dx = x_right - x_left - 1;
416 const int dy = y_bottom - y_top - 1;
417 const int A = (int)((r_x_1 * r_y_1) * scaling);
418 const int B = (int)((r_x1 * r_y_1) * scaling);
419 const int C = (int)((r_x1 * r_y1) * scaling);
420 const int D = (int)((r_x_1 * r_y1) * scaling);
421 const int r_x_1_i = (int)(r_x_1 * scaling);
422 const int r_y_1_i = (int)(r_y_1 * scaling);
423 const int r_x1_i = (int)(r_x1 * scaling);
424 const int r_y1_i = (int)(r_y1 * scaling);
428 // now the calculation:
429 uchar* ptr = image.data + x_left + imagecols * y_top;
430 // first the corners:
431 ret_val = A * int(*ptr);
433 ret_val += B * int(*ptr);
434 ptr += dy * imagecols + 1;
435 ret_val += C * int(*ptr);
437 ret_val += D * int(*ptr);
440 int* ptr_integral = (int*) integral.data + x_left + integralcols * y_top + 1;
441 // find a simple path through the different surface corners
442 const int tmp1 = (*ptr_integral);
444 const int tmp2 = (*ptr_integral);
445 ptr_integral += integralcols;
446 const int tmp3 = (*ptr_integral);
448 const int tmp4 = (*ptr_integral);
449 ptr_integral += dy * integralcols;
450 const int tmp5 = (*ptr_integral);
452 const int tmp6 = (*ptr_integral);
453 ptr_integral += integralcols;
454 const int tmp7 = (*ptr_integral);
456 const int tmp8 = (*ptr_integral);
457 ptr_integral -= integralcols;
458 const int tmp9 = (*ptr_integral);
460 const int tmp10 = (*ptr_integral);
461 ptr_integral -= dy * integralcols;
462 const int tmp11 = (*ptr_integral);
464 const int tmp12 = (*ptr_integral);
466 // assign the weighted surface integrals:
467 const int upper = (tmp3 - tmp2 + tmp1 - tmp12) * r_y_1_i;
468 const int middle = (tmp6 - tmp3 + tmp12 - tmp9) * scaling;
469 const int left = (tmp9 - tmp12 + tmp11 - tmp10) * r_x_1_i;
470 const int right = (tmp5 - tmp4 + tmp3 - tmp6) * r_x1_i;
471 const int bottom = (tmp7 - tmp6 + tmp9 - tmp8) * r_y1_i;
473 return (ret_val + upper + middle + left + right + bottom + scaling2 / 2) / scaling2;
476 // now the calculation:
477 uchar* ptr = image.data + x_left + imagecols * y_top;
479 ret_val = A * int(*ptr);
481 const uchar* end1 = ptr + dx;
482 for (; ptr < end1; ptr++)
484 ret_val += r_y_1_i * int(*ptr);
486 ret_val += B * int(*ptr);
488 ptr += imagecols - dx - 1;
489 uchar* end_j = ptr + dy * imagecols;
490 for (; ptr < end_j; ptr += imagecols - dx - 1)
492 ret_val += r_x_1_i * int(*ptr);
494 const uchar* end2 = ptr + dx;
495 for (; ptr < end2; ptr++)
497 ret_val += int(*ptr) * scaling;
499 ret_val += r_x1_i * int(*ptr);
502 ret_val += D * int(*ptr);
504 const uchar* end3 = ptr + dx;
505 for (; ptr < end3; ptr++)
507 ret_val += r_y1_i * int(*ptr);
509 ret_val += C * int(*ptr);
511 return (ret_val + scaling2 / 2) / scaling2;
515 RoiPredicate(const float minX, const float minY, const float maxX, const float maxY, const KeyPoint& keyPt)
517 const Point2f& pt = keyPt.pt;
518 return (pt.x < minX) || (pt.x >= maxX) || (pt.y < minY) || (pt.y >= maxY);
521 // computes the descriptor
523 BRISK::operator()( InputArray _image, InputArray _mask, vector<KeyPoint>& keypoints,
524 OutputArray _descriptors, bool useProvidedKeypoints) const
526 bool doOrientation=true;
527 if (useProvidedKeypoints)
528 doOrientation = false;
529 computeDescriptorsAndOrOrientation(_image, _mask, keypoints, _descriptors, true, doOrientation,
530 useProvidedKeypoints);
534 BRISK::computeDescriptorsAndOrOrientation(InputArray _image, InputArray _mask, vector<KeyPoint>& keypoints,
535 OutputArray _descriptors, bool doDescriptors, bool doOrientation,
536 bool useProvidedKeypoints) const
538 Mat image = _image.getMat(), mask = _mask.getMat();
539 if( image.type() != CV_8UC1 )
540 cvtColor(image, image, CV_BGR2GRAY);
542 if (!useProvidedKeypoints)
544 doOrientation = true;
545 computeKeypointsNoOrientation(_image, _mask, keypoints);
548 //Remove keypoints very close to the border
549 size_t ksize = keypoints.size();
550 std::vector<int> kscales; // remember the scale per keypoint
551 kscales.resize(ksize);
552 static const float log2 = 0.693147180559945f;
553 static const float lb_scalerange = (float)(log(scalerange_) / (log2));
554 std::vector<cv::KeyPoint>::iterator beginning = keypoints.begin();
555 std::vector<int>::iterator beginningkscales = kscales.begin();
556 static const float basicSize06 = basicSize_ * 0.6f;
557 for (size_t k = 0; k < ksize; k++)
560 scale = std::max((int) (scales_ / lb_scalerange * (log(keypoints[k].size / (basicSize06)) / log2) + 0.5), 0);
562 if (scale >= scales_)
565 const int border = sizeList_[scale];
566 const int border_x = image.cols - border;
567 const int border_y = image.rows - border;
568 if (RoiPredicate((float)border, (float)border, (float)border_x, (float)border_y, keypoints[k]))
570 keypoints.erase(beginning + k);
571 kscales.erase(beginningkscales + k);
574 beginning = keypoints.begin();
575 beginningkscales = kscales.begin();
582 // first, calculate the integral image over the whole image:
583 // current integral image
584 cv::Mat _integral; // the integral image
585 cv::integral(image, _integral);
587 int* _values = new int[points_]; // for temporary use
589 // resize the descriptors:
593 _descriptors.create((int)ksize, strings_, CV_8U);
594 descriptors = _descriptors.getMat();
595 descriptors.setTo(0);
598 // now do the extraction for all keypoints:
600 // temporary variables containing gray values at sample points:
604 // the feature orientation
605 uchar* ptr = descriptors.data;
606 for (size_t k = 0; k < ksize; k++)
608 cv::KeyPoint& kp = keypoints[k];
609 const int& scale = kscales[k];
610 int* pvalues = _values;
611 const float& x = kp.pt.x;
612 const float& y = kp.pt.y;
616 // get the gray values in the unrotated pattern
617 for (unsigned int i = 0; i < points_; i++)
619 *(pvalues++) = smoothedIntensity(image, _integral, x, y, scale, 0, i);
624 // now iterate through the long pairings
625 const BriskLongPair* max = longPairs_ + noLongPairs_;
626 for (BriskLongPair* iter = longPairs_; iter < max; ++iter)
628 t1 = *(_values + iter->i);
629 t2 = *(_values + iter->j);
630 const int delta_t = (t1 - t2);
631 // update the direction:
632 const int tmp0 = delta_t * (iter->weighted_dx) / 1024;
633 const int tmp1 = delta_t * (iter->weighted_dy) / 1024;
637 kp.angle = (float)(atan2((float) direction1, (float) direction0) / CV_PI * 180.0);
648 // don't compute the gradient direction, just assign a rotation of 0°
653 theta = (int) (n_rot_ * (kp.angle / (360.0)) + 0.5);
656 if (theta >= int(n_rot_))
660 // now also extract the stuff for the actual direction:
661 // let us compute the smoothed values
664 //unsigned int mean=0;
666 // get the gray values in the rotated pattern
667 for (unsigned int i = 0; i < points_; i++)
669 *(pvalues++) = smoothedIntensity(image, _integral, x, y, scale, theta, i);
672 // now iterate through all the pairings
673 unsigned int* ptr2 = (unsigned int*) ptr;
674 const BriskShortPair* max = shortPairs_ + noShortPairs_;
675 for (BriskShortPair* iter = shortPairs_; iter < max; ++iter)
677 t1 = *(_values + iter->i);
678 t2 = *(_values + iter->j);
681 *ptr2 |= ((1) << shifter);
683 } // else already initialized with zero
684 // take care of the iterators:
701 BRISK::descriptorSize() const
707 BRISK::descriptorType() const
714 delete[] patternPoints_;
715 delete[] shortPairs_;
722 BRISK::operator()(InputArray image, InputArray mask, vector<KeyPoint>& keypoints) const
724 computeKeypointsNoOrientation(image, mask, keypoints);
725 computeDescriptorsAndOrOrientation(image, mask, keypoints, cv::noArray(), false, true, true);
729 BRISK::computeKeypointsNoOrientation(InputArray _image, InputArray _mask, vector<KeyPoint>& keypoints) const
731 Mat image = _image.getMat(), mask = _mask.getMat();
732 if( image.type() != CV_8UC1 )
733 cvtColor(_image, image, CV_BGR2GRAY);
735 BriskScaleSpace briskScaleSpace(octaves);
736 briskScaleSpace.constructPyramid(image);
737 briskScaleSpace.getKeypoints(threshold, keypoints);
739 // remove invalid points
740 removeInvalidPoints(mask, keypoints);
745 BRISK::detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask) const
747 (*this)(image, mask, keypoints);
751 BRISK::computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors) const
753 (*this)(image, Mat(), keypoints, descriptors, true);
756 // construct telling the octaves number:
757 BriskScaleSpace::BriskScaleSpace(int _octaves)
762 layers_ = 2 * _octaves;
764 BriskScaleSpace::~BriskScaleSpace()
768 // construct the image pyramids
770 BriskScaleSpace::constructPyramid(const cv::Mat& image)
777 pyramid_.push_back(BriskLayer(image.clone()));
780 pyramid_.push_back(BriskLayer(pyramid_.back(), BriskLayer::CommonParams::TWOTHIRDSAMPLE));
782 const int octaves2 = layers_;
784 for (uchar i = 2; i < octaves2; i += 2)
786 pyramid_.push_back(BriskLayer(pyramid_[i - 2], BriskLayer::CommonParams::HALFSAMPLE));
787 pyramid_.push_back(BriskLayer(pyramid_[i - 1], BriskLayer::CommonParams::HALFSAMPLE));
792 BriskScaleSpace::getKeypoints(const int threshold_, std::vector<cv::KeyPoint>& keypoints)
794 // make sure keypoints is empty
796 keypoints.reserve(2000);
799 int safeThreshold_ = (int)(threshold_ * safetyFactor_);
800 std::vector<std::vector<cv::KeyPoint> > agastPoints;
801 agastPoints.resize(layers_);
803 // go through the octaves and intra layers and calculate fast corner scores:
804 for (int i = 0; i < layers_; i++)
806 // call OAST16_9 without nms
807 BriskLayer& l = pyramid_[i];
808 l.getAgastPoints(safeThreshold_, agastPoints[i]);
813 // just do a simple 2d subpixel refinement...
814 const size_t num = agastPoints[0].size();
815 for (size_t n = 0; n < num; n++)
817 const cv::Point2f& point = agastPoints.at(0)[n].pt;
818 // first check if it is a maximum:
819 if (!isMax2D(0, (int)point.x, (int)point.y))
822 // let's do the subpixel and float scale refinement:
823 BriskLayer& l = pyramid_[0];
824 int s_0_0 = l.getAgastScore(point.x - 1, point.y - 1, 1);
825 int s_1_0 = l.getAgastScore(point.x, point.y - 1, 1);
826 int s_2_0 = l.getAgastScore(point.x + 1, point.y - 1, 1);
827 int s_2_1 = l.getAgastScore(point.x + 1, point.y, 1);
828 int s_1_1 = l.getAgastScore(point.x, point.y, 1);
829 int s_0_1 = l.getAgastScore(point.x - 1, point.y, 1);
830 int s_0_2 = l.getAgastScore(point.x - 1, point.y + 1, 1);
831 int s_1_2 = l.getAgastScore(point.x, point.y + 1, 1);
832 int s_2_2 = l.getAgastScore(point.x + 1, point.y + 1, 1);
833 float delta_x, delta_y;
834 float max = subpixel2D(s_0_0, s_0_1, s_0_2, s_1_0, s_1_1, s_1_2, s_2_0, s_2_1, s_2_2, delta_x, delta_y);
837 keypoints.push_back(cv::KeyPoint(float(point.x) + delta_x, float(point.y) + delta_y, basicSize_, -1, max, 0));
844 float x, y, scale, score;
845 for (int i = 0; i < layers_; i++)
847 BriskLayer& l = pyramid_[i];
848 const size_t num = agastPoints[i].size();
849 if (i == layers_ - 1)
851 for (size_t n = 0; n < num; n++)
853 const cv::Point2f& point = agastPoints.at(i)[n].pt;
854 // consider only 2D maxima...
855 if (!isMax2D(i, (int)point.x, (int)point.y))
860 getScoreMaxBelow(i, (int)point.x, (int)point.y, l.getAgastScore(point.x, point.y, safeThreshold_), ismax, dx, dy);
864 // get the patch on this layer:
865 int s_0_0 = l.getAgastScore(point.x - 1, point.y - 1, 1);
866 int s_1_0 = l.getAgastScore(point.x, point.y - 1, 1);
867 int s_2_0 = l.getAgastScore(point.x + 1, point.y - 1, 1);
868 int s_2_1 = l.getAgastScore(point.x + 1, point.y, 1);
869 int s_1_1 = l.getAgastScore(point.x, point.y, 1);
870 int s_0_1 = l.getAgastScore(point.x - 1, point.y, 1);
871 int s_0_2 = l.getAgastScore(point.x - 1, point.y + 1, 1);
872 int s_1_2 = l.getAgastScore(point.x, point.y + 1, 1);
873 int s_2_2 = l.getAgastScore(point.x + 1, point.y + 1, 1);
874 float delta_x, delta_y;
875 float max = subpixel2D(s_0_0, s_0_1, s_0_2, s_1_0, s_1_1, s_1_2, s_2_0, s_2_1, s_2_2, delta_x, delta_y);
879 cv::KeyPoint((float(point.x) + delta_x) * l.scale() + l.offset(),
880 (float(point.y) + delta_y) * l.scale() + l.offset(), basicSize_ * l.scale(), -1, max, i));
885 // not the last layer:
886 for (size_t n = 0; n < num; n++)
888 const cv::Point2f& point = agastPoints.at(i)[n].pt;
890 // first check if it is a maximum:
891 if (!isMax2D(i, (int)point.x, (int)point.y))
894 // let's do the subpixel and float scale refinement:
896 score = refine3D(i, (int)point.x, (int)point.y, x, y, scale, ismax);
902 // finally store the detected keypoint:
903 if (score > float(threshold_))
905 keypoints.push_back(cv::KeyPoint(x, y, basicSize_ * scale, -1, score, i));
912 // interpolated score access with recalculation when needed:
914 BriskScaleSpace::getScoreAbove(const int layer, const int x_layer, const int y_layer) const
916 assert(layer<layers_-1);
917 const BriskLayer& l = pyramid_[layer + 1];
920 const int sixths_x = 4 * x_layer - 1;
921 const int x_above = sixths_x / 6;
922 const int sixths_y = 4 * y_layer - 1;
923 const int y_above = sixths_y / 6;
924 const int r_x = (sixths_x % 6);
925 const int r_x_1 = 6 - r_x;
926 const int r_y = (sixths_y % 6);
927 const int r_y_1 = 6 - r_y;
929 & ((r_x_1 * r_y_1 * l.getAgastScore(x_above, y_above, 1) + r_x * r_y_1
930 * l.getAgastScore(x_above + 1, y_above, 1)
931 + r_x_1 * r_y * l.getAgastScore(x_above, y_above + 1, 1)
932 + r_x * r_y * l.getAgastScore(x_above + 1, y_above + 1, 1) + 18)
939 const int eighths_x = 6 * x_layer - 1;
940 const int x_above = eighths_x / 8;
941 const int eighths_y = 6 * y_layer - 1;
942 const int y_above = eighths_y / 8;
943 const int r_x = (eighths_x % 8);
944 const int r_x_1 = 8 - r_x;
945 const int r_y = (eighths_y % 8);
946 const int r_y_1 = 8 - r_y;
948 & ((r_x_1 * r_y_1 * l.getAgastScore(x_above, y_above, 1) + r_x * r_y_1
949 * l.getAgastScore(x_above + 1, y_above, 1)
950 + r_x_1 * r_y * l.getAgastScore(x_above, y_above + 1, 1)
951 + r_x * r_y * l.getAgastScore(x_above + 1, y_above + 1, 1) + 32)
957 BriskScaleSpace::getScoreBelow(const int layer, const int x_layer, const int y_layer) const
960 const BriskLayer& l = pyramid_[layer - 1];
976 sixth_x = 8 * x_layer + 1;
977 xf = float(sixth_x) / 6.0f;
978 sixth_y = 8 * y_layer + 1;
979 yf = float(sixth_y) / 6.0f;
983 area = 4.0f * offs * offs;
984 scaling = (int)(4194304.0 / area);
985 scaling2 = (int)(float(scaling) * area);
989 quarter_x = 6 * x_layer + 1;
990 xf = float(quarter_x) / 4.0f;
991 quarter_y = 6 * y_layer + 1;
992 yf = float(quarter_y) / 4.0f;
996 area = 4.0f * offs * offs;
997 scaling = (int)(4194304.0 / area);
998 scaling2 = (int)(float(scaling) * area);
1001 // calculate borders
1002 const float x_1 = xf - offs;
1003 const float x1 = xf + offs;
1004 const float y_1 = yf - offs;
1005 const float y1 = yf + offs;
1007 const int x_left = int(x_1 + 0.5);
1008 const int y_top = int(y_1 + 0.5);
1009 const int x_right = int(x1 + 0.5);
1010 const int y_bottom = int(y1 + 0.5);
1012 // overlap area - multiplication factors:
1013 const float r_x_1 = float(x_left) - x_1 + 0.5f;
1014 const float r_y_1 = float(y_top) - y_1 + 0.5f;
1015 const float r_x1 = x1 - float(x_right) + 0.5f;
1016 const float r_y1 = y1 - float(y_bottom) + 0.5f;
1017 const int dx = x_right - x_left - 1;
1018 const int dy = y_bottom - y_top - 1;
1019 const int A = (int)((r_x_1 * r_y_1) * scaling);
1020 const int B = (int)((r_x1 * r_y_1) * scaling);
1021 const int C = (int)((r_x1 * r_y1) * scaling);
1022 const int D = (int)((r_x_1 * r_y1) * scaling);
1023 const int r_x_1_i = (int)(r_x_1 * scaling);
1024 const int r_y_1_i = (int)(r_y_1 * scaling);
1025 const int r_x1_i = (int)(r_x1 * scaling);
1026 const int r_y1_i = (int)(r_y1 * scaling);
1029 int ret_val = A * int(l.getAgastScore(x_left, y_top, 1));
1030 for (int X = 1; X <= dx; X++)
1032 ret_val += r_y_1_i * int(l.getAgastScore(x_left + X, y_top, 1));
1034 ret_val += B * int(l.getAgastScore(x_left + dx + 1, y_top, 1));
1036 for (int Y = 1; Y <= dy; Y++)
1038 ret_val += r_x_1_i * int(l.getAgastScore(x_left, y_top + Y, 1));
1040 for (int X = 1; X <= dx; X++)
1042 ret_val += int(l.getAgastScore(x_left + X, y_top + Y, 1)) * scaling;
1044 ret_val += r_x1_i * int(l.getAgastScore(x_left + dx + 1, y_top + Y, 1));
1047 ret_val += D * int(l.getAgastScore(x_left, y_top + dy + 1, 1));
1048 for (int X = 1; X <= dx; X++)
1050 ret_val += r_y1_i * int(l.getAgastScore(x_left + X, y_top + dy + 1, 1));
1052 ret_val += C * int(l.getAgastScore(x_left + dx + 1, y_top + dy + 1, 1));
1054 return ((ret_val + scaling2 / 2) / scaling2);
1058 BriskScaleSpace::isMax2D(const int layer, const int x_layer, const int y_layer)
1060 const cv::Mat& scores = pyramid_[layer].scores();
1061 const int scorescols = scores.cols;
1062 uchar* data = scores.data + y_layer * scorescols + x_layer;
1064 const uchar center = (*data);
1066 const uchar s_10 = *data;
1070 const uchar s10 = *data;
1073 data -= (scorescols + 1);
1074 const uchar s0_1 = *data;
1077 data += 2 * scorescols;
1078 const uchar s01 = *data;
1082 const uchar s_11 = *data;
1086 const uchar s11 = *data;
1089 data -= 2 * scorescols;
1090 const uchar s1_1 = *data;
1094 const uchar s_1_1 = *data;
1098 // reject neighbor maxima
1099 std::vector<int> delta;
1100 // put together a list of 2d-offsets to where the maximum is also reached
1101 if (center == s_1_1)
1103 delta.push_back(-1);
1104 delta.push_back(-1);
1109 delta.push_back(-1);
1114 delta.push_back(-1);
1118 delta.push_back(-1);
1128 delta.push_back(-1);
1141 const unsigned int deltasize = (unsigned int)delta.size();
1144 // in this case, we have to analyze the situation more carefully:
1145 // the values are gaussian blurred and then we really decide
1146 data = scores.data + y_layer * scorescols + x_layer;
1147 int smoothedcenter = 4 * center + 2 * (s_10 + s10 + s0_1 + s01) + s_1_1 + s1_1 + s_11 + s11;
1148 for (unsigned int i = 0; i < deltasize; i += 2)
1150 data = scores.data + (y_layer - 1 + delta[i + 1]) * scorescols + x_layer + delta[i] - 1;
1151 int othercenter = *data;
1153 othercenter += 2 * (*data);
1155 othercenter += *data;
1157 othercenter += 2 * (*data);
1159 othercenter += 4 * (*data);
1161 othercenter += 2 * (*data);
1163 othercenter += *data;
1165 othercenter += 2 * (*data);
1167 othercenter += *data;
1168 if (othercenter > smoothedcenter)
1175 // 3D maximum refinement centered around (x_layer,y_layer)
1177 BriskScaleSpace::refine3D(const int layer, const int x_layer, const int y_layer, float& x, float& y, float& scale,
1181 const BriskLayer& thisLayer = pyramid_[layer];
1182 const int center = thisLayer.getAgastScore(x_layer, y_layer, 1);
1184 // check and get above maximum:
1185 float delta_x_above = 0, delta_y_above = 0;
1186 float max_above = getScoreMaxAbove(layer, x_layer, y_layer, center, ismax, delta_x_above, delta_y_above);
1191 float max; // to be returned
1195 // treat the patch below:
1196 float delta_x_below, delta_y_below;
1197 float max_below_float;
1201 // guess the lower intra octave...
1202 const BriskLayer& l = pyramid_[0];
1203 int s_0_0 = l.getAgastScore_5_8(x_layer - 1, y_layer - 1, 1);
1205 int s_1_0 = l.getAgastScore_5_8(x_layer, y_layer - 1, 1);
1206 max_below = std::max(s_1_0, max_below);
1207 int s_2_0 = l.getAgastScore_5_8(x_layer + 1, y_layer - 1, 1);
1208 max_below = std::max(s_2_0, max_below);
1209 int s_2_1 = l.getAgastScore_5_8(x_layer + 1, y_layer, 1);
1210 max_below = std::max(s_2_1, max_below);
1211 int s_1_1 = l.getAgastScore_5_8(x_layer, y_layer, 1);
1212 max_below = std::max(s_1_1, max_below);
1213 int s_0_1 = l.getAgastScore_5_8(x_layer - 1, y_layer, 1);
1214 max_below = std::max(s_0_1, max_below);
1215 int s_0_2 = l.getAgastScore_5_8(x_layer - 1, y_layer + 1, 1);
1216 max_below = std::max(s_0_2, max_below);
1217 int s_1_2 = l.getAgastScore_5_8(x_layer, y_layer + 1, 1);
1218 max_below = std::max(s_1_2, max_below);
1219 int s_2_2 = l.getAgastScore_5_8(x_layer + 1, y_layer + 1, 1);
1220 max_below = std::max(s_2_2, max_below);
1222 max_below_float = subpixel2D(s_0_0, s_0_1, s_0_2, s_1_0, s_1_1, s_1_2, s_2_0, s_2_1, s_2_2, delta_x_below,
1224 max_below_float = (float)max_below;
1228 max_below_float = getScoreMaxBelow(layer, x_layer, y_layer, center, ismax, delta_x_below, delta_y_below);
1233 // get the patch on this layer:
1234 int s_0_0 = thisLayer.getAgastScore(x_layer - 1, y_layer - 1, 1);
1235 int s_1_0 = thisLayer.getAgastScore(x_layer, y_layer - 1, 1);
1236 int s_2_0 = thisLayer.getAgastScore(x_layer + 1, y_layer - 1, 1);
1237 int s_2_1 = thisLayer.getAgastScore(x_layer + 1, y_layer, 1);
1238 int s_1_1 = thisLayer.getAgastScore(x_layer, y_layer, 1);
1239 int s_0_1 = thisLayer.getAgastScore(x_layer - 1, y_layer, 1);
1240 int s_0_2 = thisLayer.getAgastScore(x_layer - 1, y_layer + 1, 1);
1241 int s_1_2 = thisLayer.getAgastScore(x_layer, y_layer + 1, 1);
1242 int s_2_2 = thisLayer.getAgastScore(x_layer + 1, y_layer + 1, 1);
1243 float delta_x_layer, delta_y_layer;
1244 float max_layer = subpixel2D(s_0_0, s_0_1, s_0_2, s_1_0, s_1_1, s_1_2, s_2_0, s_2_1, s_2_2, delta_x_layer,
1247 // calculate the relative scale (1D maximum):
1250 scale = refine1D_2(max_below_float, std::max(float(center), max_layer), max_above, max);
1253 scale = refine1D(max_below_float, std::max(float(center), max_layer), max_above, max);
1257 // interpolate the position:
1258 const float r0 = (1.5f - scale) / .5f;
1259 const float r1 = 1.0f - r0;
1260 x = (r0 * delta_x_layer + r1 * delta_x_above + float(x_layer)) * thisLayer.scale() + thisLayer.offset();
1261 y = (r0 * delta_y_layer + r1 * delta_y_above + float(y_layer)) * thisLayer.scale() + thisLayer.offset();
1267 // interpolate the position:
1268 const float r0 = (scale - 0.5f) / 0.5f;
1269 const float r_1 = 1.0f - r0;
1270 x = r0 * delta_x_layer + r_1 * delta_x_below + float(x_layer);
1271 y = r0 * delta_y_layer + r_1 * delta_y_below + float(y_layer);
1275 // interpolate the position:
1276 const float r0 = (scale - 0.75f) / 0.25f;
1277 const float r_1 = 1.0f - r0;
1278 x = (r0 * delta_x_layer + r_1 * delta_x_below + float(x_layer)) * thisLayer.scale() + thisLayer.offset();
1279 y = (r0 * delta_y_layer + r_1 * delta_y_below + float(y_layer)) * thisLayer.scale() + thisLayer.offset();
1286 // check the patch below:
1287 float delta_x_below, delta_y_below;
1288 float max_below = getScoreMaxBelow(layer, x_layer, y_layer, center, ismax, delta_x_below, delta_y_below);
1292 // get the patch on this layer:
1293 int s_0_0 = thisLayer.getAgastScore(x_layer - 1, y_layer - 1, 1);
1294 int s_1_0 = thisLayer.getAgastScore(x_layer, y_layer - 1, 1);
1295 int s_2_0 = thisLayer.getAgastScore(x_layer + 1, y_layer - 1, 1);
1296 int s_2_1 = thisLayer.getAgastScore(x_layer + 1, y_layer, 1);
1297 int s_1_1 = thisLayer.getAgastScore(x_layer, y_layer, 1);
1298 int s_0_1 = thisLayer.getAgastScore(x_layer - 1, y_layer, 1);
1299 int s_0_2 = thisLayer.getAgastScore(x_layer - 1, y_layer + 1, 1);
1300 int s_1_2 = thisLayer.getAgastScore(x_layer, y_layer + 1, 1);
1301 int s_2_2 = thisLayer.getAgastScore(x_layer + 1, y_layer + 1, 1);
1302 float delta_x_layer, delta_y_layer;
1303 float max_layer = subpixel2D(s_0_0, s_0_1, s_0_2, s_1_0, s_1_1, s_1_2, s_2_0, s_2_1, s_2_2, delta_x_layer,
1306 // calculate the relative scale (1D maximum):
1307 scale = refine1D_1(max_below, std::max(float(center), max_layer), max_above, max);
1310 // interpolate the position:
1311 const float r0 = 4.0f - scale * 3.0f;
1312 const float r1 = 1.0f - r0;
1313 x = (r0 * delta_x_layer + r1 * delta_x_above + float(x_layer)) * thisLayer.scale() + thisLayer.offset();
1314 y = (r0 * delta_y_layer + r1 * delta_y_above + float(y_layer)) * thisLayer.scale() + thisLayer.offset();
1318 // interpolate the position:
1319 const float r0 = scale * 3.0f - 2.0f;
1320 const float r_1 = 1.0f - r0;
1321 x = (r0 * delta_x_layer + r_1 * delta_x_below + float(x_layer)) * thisLayer.scale() + thisLayer.offset();
1322 y = (r0 * delta_y_layer + r_1 * delta_y_below + float(y_layer)) * thisLayer.scale() + thisLayer.offset();
1326 // calculate the absolute scale:
1327 scale *= thisLayer.scale();
1329 // that's it, return the refined maximum:
1333 // return the maximum of score patches above or below
1335 BriskScaleSpace::getScoreMaxAbove(const int layer, const int x_layer, const int y_layer, const int threshold,
1336 bool& ismax, float& dx, float& dy) const
1340 // relevant floating point coordinates
1347 assert(layer+1<layers_);
1348 const BriskLayer& layerAbove = pyramid_[layer + 1];
1353 x_1 = float(4 * (x_layer) - 1 - 2) / 6.0f;
1354 x1 = float(4 * (x_layer) - 1 + 2) / 6.0f;
1355 y_1 = float(4 * (y_layer) - 1 - 2) / 6.0f;
1356 y1 = float(4 * (y_layer) - 1 + 2) / 6.0f;
1361 x_1 = float(6 * (x_layer) - 1 - 3) / 8.0f;
1362 x1 = float(6 * (x_layer) - 1 + 3) / 8.0f;
1363 y_1 = float(6 * (y_layer) - 1 - 3) / 8.0f;
1364 y1 = float(6 * (y_layer) - 1 + 3) / 8.0f;
1367 // check the first row
1368 int max_x = (int)x_1 + 1;
1369 int max_y = (int)y_1 + 1;
1371 float maxval = (float)layerAbove.getAgastScore(x_1, y_1, 1);
1372 if (maxval > threshold)
1374 for (int x = (int)x_1 + 1; x <= int(x1); x++)
1376 tmp_max = (float)layerAbove.getAgastScore(float(x), y_1, 1);
1377 if (tmp_max > threshold)
1379 if (tmp_max > maxval)
1385 tmp_max = (float)layerAbove.getAgastScore(x1, y_1, 1);
1386 if (tmp_max > threshold)
1388 if (tmp_max > maxval)
1395 for (int y = (int)y_1 + 1; y <= int(y1); y++)
1397 tmp_max = (float)layerAbove.getAgastScore(x_1, float(y), 1);
1398 if (tmp_max > threshold)
1400 if (tmp_max > maxval)
1403 max_x = int(x_1 + 1);
1406 for (int x = (int)x_1 + 1; x <= int(x1); x++)
1408 tmp_max = (float)layerAbove.getAgastScore(x, y, 1);
1409 if (tmp_max > threshold)
1411 if (tmp_max > maxval)
1418 tmp_max = (float)layerAbove.getAgastScore(x1, float(y), 1);
1419 if (tmp_max > threshold)
1421 if (tmp_max > maxval)
1430 tmp_max = (float)layerAbove.getAgastScore(x_1, y1, 1);
1431 if (tmp_max > maxval)
1434 max_x = int(x_1 + 1);
1437 for (int x = (int)x_1 + 1; x <= int(x1); x++)
1439 tmp_max = (float)layerAbove.getAgastScore(float(x), y1, 1);
1440 if (tmp_max > maxval)
1447 tmp_max = (float)layerAbove.getAgastScore(x1, y1, 1);
1448 if (tmp_max > maxval)
1456 int s_0_0 = layerAbove.getAgastScore(max_x - 1, max_y - 1, 1);
1457 int s_1_0 = layerAbove.getAgastScore(max_x, max_y - 1, 1);
1458 int s_2_0 = layerAbove.getAgastScore(max_x + 1, max_y - 1, 1);
1459 int s_2_1 = layerAbove.getAgastScore(max_x + 1, max_y, 1);
1460 int s_1_1 = layerAbove.getAgastScore(max_x, max_y, 1);
1461 int s_0_1 = layerAbove.getAgastScore(max_x - 1, max_y, 1);
1462 int s_0_2 = layerAbove.getAgastScore(max_x - 1, max_y + 1, 1);
1463 int s_1_2 = layerAbove.getAgastScore(max_x, max_y + 1, 1);
1464 int s_2_2 = layerAbove.getAgastScore(max_x + 1, max_y + 1, 1);
1466 float refined_max = subpixel2D(s_0_0, s_0_1, s_0_2, s_1_0, s_1_1, s_1_2, s_2_0, s_2_1, s_2_2, dx_1, dy_1);
1468 // calculate dx/dy in above coordinates
1469 float real_x = float(max_x) + dx_1;
1470 float real_y = float(max_y) + dy_1;
1471 bool returnrefined = true;
1474 dx = (real_x * 6.0f + 1.0f) / 4.0f - float(x_layer);
1475 dy = (real_y * 6.0f + 1.0f) / 4.0f - float(y_layer);
1479 dx = (real_x * 8.0f + 1.0f) / 6.0f - float(x_layer);
1480 dy = (real_y * 8.0f + 1.0f) / 6.0f - float(y_layer);
1487 returnrefined = false;
1492 returnrefined = false;
1497 returnrefined = false;
1502 returnrefined = false;
1509 return std::max(refined_max, maxval);
1515 BriskScaleSpace::getScoreMaxBelow(const int layer, const int x_layer, const int y_layer, const int threshold,
1516 bool& ismax, float& dx, float& dy) const
1520 // relevant floating point coordinates
1529 x_1 = float(8 * (x_layer) + 1 - 4) / 6.0f;
1530 x1 = float(8 * (x_layer) + 1 + 4) / 6.0f;
1531 y_1 = float(8 * (y_layer) + 1 - 4) / 6.0f;
1532 y1 = float(8 * (y_layer) + 1 + 4) / 6.0f;
1536 x_1 = float(6 * (x_layer) + 1 - 3) / 4.0f;
1537 x1 = float(6 * (x_layer) + 1 + 3) / 4.0f;
1538 y_1 = float(6 * (y_layer) + 1 - 3) / 4.0f;
1539 y1 = float(6 * (y_layer) + 1 + 3) / 4.0f;
1544 const BriskLayer& layerBelow = pyramid_[layer - 1];
1546 // check the first row
1547 int max_x = (int)x_1 + 1;
1548 int max_y = (int)y_1 + 1;
1550 float max = (float)layerBelow.getAgastScore(x_1, y_1, 1);
1551 if (max > threshold)
1553 for (int x = (int)x_1 + 1; x <= int(x1); x++)
1555 tmp_max = (float)layerBelow.getAgastScore(float(x), y_1, 1);
1556 if (tmp_max > threshold)
1564 tmp_max = (float)layerBelow.getAgastScore(x1, y_1, 1);
1565 if (tmp_max > threshold)
1574 for (int y = (int)y_1 + 1; y <= int(y1); y++)
1576 tmp_max = (float)layerBelow.getAgastScore(x_1, float(y), 1);
1577 if (tmp_max > threshold)
1582 max_x = int(x_1 + 1);
1585 for (int x = (int)x_1 + 1; x <= int(x1); x++)
1587 tmp_max = (float)layerBelow.getAgastScore(x, y, 1);
1588 if (tmp_max > threshold)
1593 * (layerBelow.getAgastScore(x - 1, y, 1) + layerBelow.getAgastScore(x + 1, y, 1)
1594 + layerBelow.getAgastScore(x, y + 1, 1) + layerBelow.getAgastScore(x, y - 1, 1))
1595 + (layerBelow.getAgastScore(x + 1, y + 1, 1) + layerBelow.getAgastScore(x - 1, y + 1, 1)
1596 + layerBelow.getAgastScore(x + 1, y - 1, 1) + layerBelow.getAgastScore(x - 1, y - 1, 1));
1598 * (layerBelow.getAgastScore(max_x - 1, max_y, 1) + layerBelow.getAgastScore(max_x + 1, max_y, 1)
1599 + layerBelow.getAgastScore(max_x, max_y + 1, 1) + layerBelow.getAgastScore(max_x, max_y - 1, 1))
1600 + (layerBelow.getAgastScore(max_x + 1, max_y + 1, 1) + layerBelow.getAgastScore(max_x - 1,
1602 + layerBelow.getAgastScore(max_x + 1, max_y - 1, 1)
1603 + layerBelow.getAgastScore(max_x - 1, max_y - 1, 1));
1617 tmp_max = (float)layerBelow.getAgastScore(x1, float(y), 1);
1618 if (tmp_max > threshold)
1629 tmp_max = (float)layerBelow.getAgastScore(x_1, y1, 1);
1633 max_x = int(x_1 + 1);
1636 for (int x = (int)x_1 + 1; x <= int(x1); x++)
1638 tmp_max = (float)layerBelow.getAgastScore(float(x), y1, 1);
1646 tmp_max = (float)layerBelow.getAgastScore(x1, y1, 1);
1655 int s_0_0 = layerBelow.getAgastScore(max_x - 1, max_y - 1, 1);
1656 int s_1_0 = layerBelow.getAgastScore(max_x, max_y - 1, 1);
1657 int s_2_0 = layerBelow.getAgastScore(max_x + 1, max_y - 1, 1);
1658 int s_2_1 = layerBelow.getAgastScore(max_x + 1, max_y, 1);
1659 int s_1_1 = layerBelow.getAgastScore(max_x, max_y, 1);
1660 int s_0_1 = layerBelow.getAgastScore(max_x - 1, max_y, 1);
1661 int s_0_2 = layerBelow.getAgastScore(max_x - 1, max_y + 1, 1);
1662 int s_1_2 = layerBelow.getAgastScore(max_x, max_y + 1, 1);
1663 int s_2_2 = layerBelow.getAgastScore(max_x + 1, max_y + 1, 1);
1665 float refined_max = subpixel2D(s_0_0, s_0_1, s_0_2, s_1_0, s_1_1, s_1_2, s_2_0, s_2_1, s_2_2, dx_1, dy_1);
1667 // calculate dx/dy in above coordinates
1668 float real_x = float(max_x) + dx_1;
1669 float real_y = float(max_y) + dy_1;
1670 bool returnrefined = true;
1673 dx = (float)((real_x * 6.0 + 1.0) / 8.0) - float(x_layer);
1674 dy = (float)((real_y * 6.0 + 1.0) / 8.0) - float(y_layer);
1678 dx = (float)((real_x * 4.0 - 1.0) / 6.0) - float(x_layer);
1679 dy = (float)((real_y * 4.0 - 1.0) / 6.0) - float(y_layer);
1686 returnrefined = false;
1691 returnrefined = false;
1696 returnrefined = false;
1701 returnrefined = false;
1708 return std::max(refined_max, max);
1714 BriskScaleSpace::refine1D(const float s_05, const float s0, const float s05, float& max) const
1716 int i_05 = int(1024.0 * s_05 + 0.5);
1717 int i0 = int(1024.0 * s0 + 0.5);
1718 int i05 = int(1024.0 * s05 + 0.5);
1720 // 16.0000 -24.0000 8.0000
1721 // -40.0000 54.0000 -14.0000
1722 // 24.0000 -27.0000 6.0000
1724 int three_a = 16 * i_05 - 24 * i0 + 8 * i05;
1725 // second derivative must be negative:
1728 if (s0 >= s_05 && s0 >= s05)
1733 if (s_05 >= s0 && s_05 >= s05)
1738 if (s05 >= s0 && s05 >= s_05)
1745 int three_b = -40 * i_05 + 54 * i0 - 14 * i05;
1746 // calculate max location:
1747 float ret_val = -float(three_b) / float(2 * three_a);
1748 // saturate and return
1751 else if (ret_val > 1.5)
1752 ret_val = 1.5; // allow to be slightly off bounds ...?
1753 int three_c = +24 * i_05 - 27 * i0 + 6 * i05;
1754 max = float(three_c) + float(three_a) * ret_val * ret_val + float(three_b) * ret_val;
1760 BriskScaleSpace::refine1D_1(const float s_05, const float s0, const float s05, float& max) const
1762 int i_05 = int(1024.0 * s_05 + 0.5);
1763 int i0 = int(1024.0 * s0 + 0.5);
1764 int i05 = int(1024.0 * s05 + 0.5);
1766 // 4.5000 -9.0000 4.5000
1767 //-10.5000 18.0000 -7.5000
1768 // 6.0000 -8.0000 3.0000
1770 int two_a = 9 * i_05 - 18 * i0 + 9 * i05;
1771 // second derivative must be negative:
1774 if (s0 >= s_05 && s0 >= s05)
1779 if (s_05 >= s0 && s_05 >= s05)
1782 return 0.6666666666666666666666666667f;
1784 if (s05 >= s0 && s05 >= s_05)
1787 return 1.3333333333333333333333333333f;
1791 int two_b = -21 * i_05 + 36 * i0 - 15 * i05;
1792 // calculate max location:
1793 float ret_val = -float(two_b) / float(2 * two_a);
1794 // saturate and return
1795 if (ret_val < 0.6666666666666666666666666667f)
1796 ret_val = 0.666666666666666666666666667f;
1797 else if (ret_val > 1.33333333333333333333333333f)
1798 ret_val = 1.333333333333333333333333333f;
1799 int two_c = +12 * i_05 - 16 * i0 + 6 * i05;
1800 max = float(two_c) + float(two_a) * ret_val * ret_val + float(two_b) * ret_val;
1806 BriskScaleSpace::refine1D_2(const float s_05, const float s0, const float s05, float& max) const
1808 int i_05 = int(1024.0 * s_05 + 0.5);
1809 int i0 = int(1024.0 * s0 + 0.5);
1810 int i05 = int(1024.0 * s05 + 0.5);
1812 // 18.0000 -30.0000 12.0000
1813 // -45.0000 65.0000 -20.0000
1814 // 27.0000 -30.0000 8.0000
1816 int a = 2 * i_05 - 4 * i0 + 2 * i05;
1817 // second derivative must be negative:
1820 if (s0 >= s_05 && s0 >= s05)
1825 if (s_05 >= s0 && s_05 >= s05)
1830 if (s05 >= s0 && s05 >= s_05)
1837 int b = -5 * i_05 + 8 * i0 - 3 * i05;
1838 // calculate max location:
1839 float ret_val = -float(b) / float(2 * a);
1840 // saturate and return
1843 else if (ret_val > 1.5f)
1844 ret_val = 1.5f; // allow to be slightly off bounds ...?
1845 int c = +3 * i_05 - 3 * i0 + 1 * i05;
1846 max = float(c) + float(a) * ret_val * ret_val + float(b) * ret_val;
1852 BriskScaleSpace::subpixel2D(const int s_0_0, const int s_0_1, const int s_0_2, const int s_1_0, const int s_1_1,
1853 const int s_1_2, const int s_2_0, const int s_2_1, const int s_2_2, float& delta_x,
1854 float& delta_y) const
1857 // the coefficients of the 2d quadratic function least-squares fit:
1858 int tmp1 = s_0_0 + s_0_2 - 2 * s_1_1 + s_2_0 + s_2_2;
1859 int coeff1 = 3 * (tmp1 + s_0_1 - ((s_1_0 + s_1_2) << 1) + s_2_1);
1860 int coeff2 = 3 * (tmp1 - ((s_0_1 + s_2_1) << 1) + s_1_0 + s_1_2);
1861 int tmp2 = s_0_2 - s_2_0;
1862 int tmp3 = (s_0_0 + tmp2 - s_2_2);
1863 int tmp4 = tmp3 - 2 * tmp2;
1864 int coeff3 = -3 * (tmp3 + s_0_1 - s_2_1);
1865 int coeff4 = -3 * (tmp4 + s_1_0 - s_1_2);
1866 int coeff5 = (s_0_0 - s_0_2 - s_2_0 + s_2_2) << 2;
1867 int coeff6 = -(s_0_0 + s_0_2 - ((s_1_0 + s_0_1 + s_1_2 + s_2_1) << 1) - 5 * s_1_1 + s_2_0 + s_2_2) << 1;
1869 // 2nd derivative test:
1870 int H_det = 4 * coeff1 * coeff2 - coeff5 * coeff5;
1876 return float(coeff6) / 18.0f;
1879 if (!(H_det > 0 && coeff1 < 0))
1881 // The maximum must be at the one of the 4 patch corners.
1882 int tmp_max = coeff3 + coeff4 + coeff5;
1886 int tmp = -coeff3 + coeff4 - coeff5;
1893 tmp = coeff3 - coeff4 - coeff5;
1900 tmp = -coeff3 - coeff4 + coeff5;
1907 return float(tmp_max + coeff1 + coeff2 + coeff6) / 18.0f;
1910 // this is hopefully the normal outcome of the Hessian test
1911 delta_x = float(2 * coeff2 * coeff3 - coeff4 * coeff5) / float(-H_det);
1912 delta_y = float(2 * coeff1 * coeff4 - coeff3 * coeff5) / float(-H_det);
1913 // TODO: this is not correct, but easy, so perform a real boundary maximum search:
1920 else if (delta_x < -1.0)
1927 if (tx || tx_ || ty || ty_)
1929 // get two candidates:
1930 float delta_x1 = 0.0f, delta_x2 = 0.0f, delta_y1 = 0.0f, delta_y2 = 0.0f;
1934 delta_y1 = -float(coeff4 + coeff5) / float(2 * coeff2);
1935 if (delta_y1 > 1.0f)
1937 else if (delta_y1 < -1.0f)
1943 delta_y1 = -float(coeff4 - coeff5) / float(2 * coeff2);
1944 if (delta_y1 > 1.0f)
1946 else if (delta_y1 < -1.0)
1952 delta_x2 = -float(coeff3 + coeff5) / float(2 * coeff1);
1953 if (delta_x2 > 1.0f)
1955 else if (delta_x2 < -1.0f)
1961 delta_x2 = -float(coeff3 - coeff5) / float(2 * coeff1);
1962 if (delta_x2 > 1.0f)
1964 else if (delta_x2 < -1.0f)
1967 // insert both options for evaluation which to pick
1968 float max1 = (coeff1 * delta_x1 * delta_x1 + coeff2 * delta_y1 * delta_y1 + coeff3 * delta_x1 + coeff4 * delta_y1
1969 + coeff5 * delta_x1 * delta_y1 + coeff6)
1971 float max2 = (coeff1 * delta_x2 * delta_x2 + coeff2 * delta_y2 * delta_y2 + coeff3 * delta_x2 + coeff4 * delta_y2
1972 + coeff5 * delta_x2 * delta_y2 + coeff6)
1988 // this is the case of the maximum inside the boundaries:
1989 return (coeff1 * delta_x * delta_x + coeff2 * delta_y * delta_y + coeff3 * delta_x + coeff4 * delta_y
1990 + coeff5 * delta_x * delta_y + coeff6)
1994 // construct a layer
1995 BriskLayer::BriskLayer(const cv::Mat& img_in, float scale_in, float offset_in)
1998 scores_ = cv::Mat_<uchar>::zeros(img_in.rows, img_in.cols);
1999 // attention: this means that the passed image reference must point to persistent memory
2001 offset_ = offset_in;
2002 // create an agast detector
2003 fast_9_16_ = new FastFeatureDetector(1, true, FastFeatureDetector::TYPE_9_16);
2004 makeOffsets(pixel_5_8_, (int)img_.step, 8);
2005 makeOffsets(pixel_9_16_, (int)img_.step, 16);
2008 BriskLayer::BriskLayer(const BriskLayer& layer, int mode)
2010 if (mode == CommonParams::HALFSAMPLE)
2012 img_.create(layer.img().rows / 2, layer.img().cols / 2, CV_8U);
2013 halfsample(layer.img(), img_);
2014 scale_ = layer.scale() * 2;
2015 offset_ = 0.5f * scale_ - 0.5f;
2019 img_.create(2 * (layer.img().rows / 3), 2 * (layer.img().cols / 3), CV_8U);
2020 twothirdsample(layer.img(), img_);
2021 scale_ = layer.scale() * 1.5f;
2022 offset_ = 0.5f * scale_ - 0.5f;
2024 scores_ = cv::Mat::zeros(img_.rows, img_.cols, CV_8U);
2025 fast_9_16_ = new FastFeatureDetector(1, false, FastFeatureDetector::TYPE_9_16);
2026 makeOffsets(pixel_5_8_, (int)img_.step, 8);
2027 makeOffsets(pixel_9_16_, (int)img_.step, 16);
2031 // wraps the agast class
2033 BriskLayer::getAgastPoints(int threshold, std::vector<KeyPoint>& keypoints)
2035 fast_9_16_->set("threshold", threshold);
2036 fast_9_16_->detect(img_, keypoints);
2038 // also write scores
2039 const size_t num = keypoints.size();
2041 for (size_t i = 0; i < num; i++)
2042 scores_((int)keypoints[i].pt.y, (int)keypoints[i].pt.x) = saturate_cast<uchar>(keypoints[i].response);
2046 BriskLayer::getAgastScore(int x, int y, int threshold) const
2050 if (x >= img_.cols - 3 || y >= img_.rows - 3)
2052 uchar& score = (uchar&)scores_(y, x);
2057 score = (uchar)cornerScore<16>(&img_.at<uchar>(y, x), pixel_9_16_, threshold - 1);
2058 if (score < threshold)
2064 BriskLayer::getAgastScore_5_8(int x, int y, int threshold) const
2068 if (x >= img_.cols - 2 || y >= img_.rows - 2)
2070 int score = cornerScore<8>(&img_.at<uchar>(y, x), pixel_5_8_, threshold - 1);
2071 if (score < threshold)
2077 BriskLayer::getAgastScore(float xf, float yf, int threshold_in, float scale_in) const
2079 if (scale_in <= 1.0f)
2081 // just do an interpolation inside the layer
2082 const int x = int(xf);
2083 const float rx1 = xf - float(x);
2084 const float rx = 1.0f - rx1;
2085 const int y = int(yf);
2086 const float ry1 = yf - float(y);
2087 const float ry = 1.0f - ry1;
2089 return (uchar)(rx * ry * getAgastScore(x, y, threshold_in) + rx1 * ry * getAgastScore(x + 1, y, threshold_in)
2090 + rx * ry1 * getAgastScore(x, y + 1, threshold_in) + rx1 * ry1 * getAgastScore(x + 1, y + 1, threshold_in));
2094 // this means we overlap area smoothing
2095 const float halfscale = scale_in / 2.0f;
2096 // get the scores first:
2097 for (int x = int(xf - halfscale); x <= int(xf + halfscale + 1.0f); x++)
2099 for (int y = int(yf - halfscale); y <= int(yf + halfscale + 1.0f); y++)
2101 getAgastScore(x, y, threshold_in);
2104 // get the smoothed value
2105 return value(scores_, xf, yf, scale_in);
2109 // access gray values (smoothed/interpolated)
2111 BriskLayer::value(const cv::Mat& mat, float xf, float yf, float scale_in) const
2113 assert(!mat.empty());
2115 const int x = cvFloor(xf);
2116 const int y = cvFloor(yf);
2117 const cv::Mat& image = mat;
2118 const int& imagecols = image.cols;
2120 // get the sigma_half:
2121 const float sigma_half = scale_in / 2;
2122 const float area = 4.0f * sigma_half * sigma_half;
2123 // calculate output:
2125 if (sigma_half < 0.5)
2127 //interpolation multipliers:
2128 const int r_x = (int)((xf - x) * 1024);
2129 const int r_y = (int)((yf - y) * 1024);
2130 const int r_x_1 = (1024 - r_x);
2131 const int r_y_1 = (1024 - r_y);
2132 uchar* ptr = image.data + x + y * imagecols;
2133 // just interpolate:
2134 ret_val = (r_x_1 * r_y_1 * int(*ptr));
2136 ret_val += (r_x * r_y_1 * int(*ptr));
2138 ret_val += (r_x * r_y * int(*ptr));
2140 ret_val += (r_x_1 * r_y * int(*ptr));
2141 return 0xFF & ((ret_val + 512) / 1024 / 1024);
2144 // this is the standard case (simple, not speed optimized yet):
2147 const int scaling = (int)(4194304.0f / area);
2148 const int scaling2 = (int)(float(scaling) * area / 1024.0f);
2150 // calculate borders
2151 const float x_1 = xf - sigma_half;
2152 const float x1 = xf + sigma_half;
2153 const float y_1 = yf - sigma_half;
2154 const float y1 = yf + sigma_half;
2156 const int x_left = int(x_1 + 0.5);
2157 const int y_top = int(y_1 + 0.5);
2158 const int x_right = int(x1 + 0.5);
2159 const int y_bottom = int(y1 + 0.5);
2161 // overlap area - multiplication factors:
2162 const float r_x_1 = float(x_left) - x_1 + 0.5f;
2163 const float r_y_1 = float(y_top) - y_1 + 0.5f;
2164 const float r_x1 = x1 - float(x_right) + 0.5f;
2165 const float r_y1 = y1 - float(y_bottom) + 0.5f;
2166 const int dx = x_right - x_left - 1;
2167 const int dy = y_bottom - y_top - 1;
2168 const int A = (int)((r_x_1 * r_y_1) * scaling);
2169 const int B = (int)((r_x1 * r_y_1) * scaling);
2170 const int C = (int)((r_x1 * r_y1) * scaling);
2171 const int D = (int)((r_x_1 * r_y1) * scaling);
2172 const int r_x_1_i = (int)(r_x_1 * scaling);
2173 const int r_y_1_i = (int)(r_y_1 * scaling);
2174 const int r_x1_i = (int)(r_x1 * scaling);
2175 const int r_y1_i = (int)(r_y1 * scaling);
2177 // now the calculation:
2178 uchar* ptr = image.data + x_left + imagecols * y_top;
2180 ret_val = A * int(*ptr);
2182 const uchar* end1 = ptr + dx;
2183 for (; ptr < end1; ptr++)
2185 ret_val += r_y_1_i * int(*ptr);
2187 ret_val += B * int(*ptr);
2189 ptr += imagecols - dx - 1;
2190 uchar* end_j = ptr + dy * imagecols;
2191 for (; ptr < end_j; ptr += imagecols - dx - 1)
2193 ret_val += r_x_1_i * int(*ptr);
2195 const uchar* end2 = ptr + dx;
2196 for (; ptr < end2; ptr++)
2198 ret_val += int(*ptr) * scaling;
2200 ret_val += r_x1_i * int(*ptr);
2203 ret_val += D * int(*ptr);
2205 const uchar* end3 = ptr + dx;
2206 for (; ptr < end3; ptr++)
2208 ret_val += r_y1_i * int(*ptr);
2210 ret_val += C * int(*ptr);
2212 return 0xFF & ((ret_val + scaling2 / 2) / scaling2 / 1024);
2217 BriskLayer::halfsample(const cv::Mat& srcimg, cv::Mat& dstimg)
2219 // make sure the destination image is of the right size:
2220 assert(srcimg.cols/2==dstimg.cols);
2221 assert(srcimg.rows/2==dstimg.rows);
2223 // handle non-SSE case
2224 resize(srcimg, dstimg, dstimg.size(), 0, 0, INTER_AREA);
2228 BriskLayer::twothirdsample(const cv::Mat& srcimg, cv::Mat& dstimg)
2230 // make sure the destination image is of the right size:
2231 assert((srcimg.cols/3)*2==dstimg.cols);
2232 assert((srcimg.rows/3)*2==dstimg.rows);
2234 resize(srcimg, dstimg, dstimg.size(), 0, 0, INTER_AREA);