Path: blob/master/modules/features2d/src/fast_score.cpp
16337 views
/* This is FAST corner detector, contributed to OpenCV by the author, Edward Rosten.1Below is the original copyright and the references */23/*4Copyright (c) 2006, 2008 Edward Rosten5All rights reserved.67Redistribution and use in source and binary forms, with or without8modification, are permitted provided that the following conditions9are met:1011*Redistributions of source code must retain the above copyright12notice, this list of conditions and the following disclaimer.1314*Redistributions in binary form must reproduce the above copyright15notice, this list of conditions and the following disclaimer in the16documentation and/or other materials provided with the distribution.1718*Neither the name of the University of Cambridge nor the names of19its contributors may be used to endorse or promote products derived20from this software without specific prior written permission.2122THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS23"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT24LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR25A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR26CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,27EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR29PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF30LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS32SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.33*/3435/*36The references are:37* Machine learning for high-speed corner detection,38E. Rosten and T. Drummond, ECCV 200639* Faster and better: A machine learning approach to corner detection40E. Rosten, R. Porter and T. Drummond, PAMI, 200941*/4243#include "fast_score.hpp"44#include "opencv2/core/hal/intrin.hpp"45#define VERIFY_CORNERS 04647namespace cv {4849void makeOffsets(int pixel[25], int rowStride, int patternSize)50{51static const int offsets16[][2] =52{53{0, 3}, { 1, 3}, { 2, 2}, { 3, 1}, { 3, 0}, { 3, -1}, { 2, -2}, { 1, -3},54{0, -3}, {-1, -3}, {-2, -2}, {-3, -1}, {-3, 0}, {-3, 1}, {-2, 2}, {-1, 3}55};5657static const int offsets12[][2] =58{59{0, 2}, { 1, 2}, { 2, 1}, { 2, 0}, { 2, -1}, { 1, -2},60{0, -2}, {-1, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-1, 2}61};6263static const int offsets8[][2] =64{65{0, 1}, { 1, 1}, { 1, 0}, { 1, -1},66{0, -1}, {-1, -1}, {-1, 0}, {-1, 1}67};6869const int (*offsets)[2] = patternSize == 16 ? offsets16 :70patternSize == 12 ? offsets12 :71patternSize == 8 ? offsets8 : 0;7273CV_Assert(pixel && offsets);7475int k = 0;76for( ; k < patternSize; k++ )77pixel[k] = offsets[k][0] + offsets[k][1] * rowStride;78for( ; k < 25; k++ )79pixel[k] = pixel[k - patternSize];80}8182#if VERIFY_CORNERS83static void testCorner(const uchar* ptr, const int pixel[], int K, int N, int threshold) {84// check that with the computed "threshold" the pixel is still a corner85// and that with the increased-by-1 "threshold" the pixel is not a corner anymore86for( int delta = 0; delta <= 1; delta++ )87{88int v0 = std::min(ptr[0] + threshold + delta, 255);89int v1 = std::max(ptr[0] - threshold - delta, 0);90int c0 = 0, c1 = 0;9192for( int k = 0; k < N; k++ )93{94int x = ptr[pixel[k]];95if(x > v0)96{97if( ++c0 > K )98break;99c1 = 0;100}101else if( x < v1 )102{103if( ++c1 > K )104break;105c0 = 0;106}107else108{109c0 = c1 = 0;110}111}112CV_Assert( (delta == 0 && std::max(c0, c1) > K) ||113(delta == 1 && std::max(c0, c1) <= K) );114}115}116#endif117118template<>119int cornerScore<16>(const uchar* ptr, const int pixel[], int threshold)120{121const int K = 8, N = K*3 + 1;122int k, v = ptr[0];123short d[N];124for( k = 0; k < N; k++ )125d[k] = (short)(v - ptr[pixel[k]]);126127#if CV_SIMD128128if (hasSIMD128())129{130v_int16x8 q0 = v_setall_s16(-1000), q1 = v_setall_s16(1000);131for (k = 0; k < 16; k += 8)132{133v_int16x8 v0 = v_load(d + k + 1);134v_int16x8 v1 = v_load(d + k + 2);135v_int16x8 a = v_min(v0, v1);136v_int16x8 b = v_max(v0, v1);137v0 = v_load(d + k + 3);138a = v_min(a, v0);139b = v_max(b, v0);140v0 = v_load(d + k + 4);141a = v_min(a, v0);142b = v_max(b, v0);143v0 = v_load(d + k + 5);144a = v_min(a, v0);145b = v_max(b, v0);146v0 = v_load(d + k + 6);147a = v_min(a, v0);148b = v_max(b, v0);149v0 = v_load(d + k + 7);150a = v_min(a, v0);151b = v_max(b, v0);152v0 = v_load(d + k + 8);153a = v_min(a, v0);154b = v_max(b, v0);155v0 = v_load(d + k);156q0 = v_max(q0, v_min(a, v0));157q1 = v_min(q1, v_max(b, v0));158v0 = v_load(d + k + 9);159q0 = v_max(q0, v_min(a, v0));160q1 = v_min(q1, v_max(b, v0));161}162q0 = v_max(q0, v_setzero_s16() - q1);163threshold = v_reduce_max(q0) - 1;164}165else166#endif167{168169int a0 = threshold;170for( k = 0; k < 16; k += 2 )171{172int a = std::min((int)d[k+1], (int)d[k+2]);173a = std::min(a, (int)d[k+3]);174if( a <= a0 )175continue;176a = std::min(a, (int)d[k+4]);177a = std::min(a, (int)d[k+5]);178a = std::min(a, (int)d[k+6]);179a = std::min(a, (int)d[k+7]);180a = std::min(a, (int)d[k+8]);181a0 = std::max(a0, std::min(a, (int)d[k]));182a0 = std::max(a0, std::min(a, (int)d[k+9]));183}184185int b0 = -a0;186for( k = 0; k < 16; k += 2 )187{188int b = std::max((int)d[k+1], (int)d[k+2]);189b = std::max(b, (int)d[k+3]);190b = std::max(b, (int)d[k+4]);191b = std::max(b, (int)d[k+5]);192if( b >= b0 )193continue;194b = std::max(b, (int)d[k+6]);195b = std::max(b, (int)d[k+7]);196b = std::max(b, (int)d[k+8]);197198b0 = std::min(b0, std::max(b, (int)d[k]));199b0 = std::min(b0, std::max(b, (int)d[k+9]));200}201202threshold = -b0 - 1;203}204205#if VERIFY_CORNERS206testCorner(ptr, pixel, K, N, threshold);207#endif208return threshold;209}210211template<>212int cornerScore<12>(const uchar* ptr, const int pixel[], int threshold)213{214const int K = 6, N = K*3 + 1;215int k, v = ptr[0];216short d[N + 4];217for( k = 0; k < N; k++ )218d[k] = (short)(v - ptr[pixel[k]]);219#if CV_SIMD128220for( k = 0; k < 4; k++ )221d[N+k] = d[k];222#endif223224#if CV_SIMD128225if (hasSIMD128())226{227v_int16x8 q0 = v_setall_s16(-1000), q1 = v_setall_s16(1000);228for (k = 0; k < 16; k += 8)229{230v_int16x8 v0 = v_load(d + k + 1);231v_int16x8 v1 = v_load(d + k + 2);232v_int16x8 a = v_min(v0, v1);233v_int16x8 b = v_max(v0, v1);234v0 = v_load(d + k + 3);235a = v_min(a, v0);236b = v_max(b, v0);237v0 = v_load(d + k + 4);238a = v_min(a, v0);239b = v_max(b, v0);240v0 = v_load(d + k + 5);241a = v_min(a, v0);242b = v_max(b, v0);243v0 = v_load(d + k + 6);244a = v_min(a, v0);245b = v_max(b, v0);246v0 = v_load(d + k);247q0 = v_max(q0, v_min(a, v0));248q1 = v_min(q1, v_max(b, v0));249v0 = v_load(d + k + 7);250q0 = v_max(q0, v_min(a, v0));251q1 = v_min(q1, v_max(b, v0));252}253q0 = v_max(q0, v_setzero_s16() - q1);254threshold = v_reduce_max(q0) - 1;255}256else257#endif258{259int a0 = threshold;260for( k = 0; k < 12; k += 2 )261{262int a = std::min((int)d[k+1], (int)d[k+2]);263if( a <= a0 )264continue;265a = std::min(a, (int)d[k+3]);266a = std::min(a, (int)d[k+4]);267a = std::min(a, (int)d[k+5]);268a = std::min(a, (int)d[k+6]);269a0 = std::max(a0, std::min(a, (int)d[k]));270a0 = std::max(a0, std::min(a, (int)d[k+7]));271}272273int b0 = -a0;274for( k = 0; k < 12; k += 2 )275{276int b = std::max((int)d[k+1], (int)d[k+2]);277b = std::max(b, (int)d[k+3]);278b = std::max(b, (int)d[k+4]);279if( b >= b0 )280continue;281b = std::max(b, (int)d[k+5]);282b = std::max(b, (int)d[k+6]);283284b0 = std::min(b0, std::max(b, (int)d[k]));285b0 = std::min(b0, std::max(b, (int)d[k+7]));286}287288threshold = -b0-1;289}290#if VERIFY_CORNERS291testCorner(ptr, pixel, K, N, threshold);292#endif293return threshold;294}295296template<>297int cornerScore<8>(const uchar* ptr, const int pixel[], int threshold)298{299const int K = 4, N = K * 3 + 1;300int k, v = ptr[0];301short d[N];302for (k = 0; k < N; k++)303d[k] = (short)(v - ptr[pixel[k]]);304305#if CV_SIMD128306if (hasSIMD128())307{308v_int16x8 v0 = v_load(d + 1);309v_int16x8 v1 = v_load(d + 2);310v_int16x8 a = v_min(v0, v1);311v_int16x8 b = v_max(v0, v1);312v0 = v_load(d + 3);313a = v_min(a, v0);314b = v_max(b, v0);315v0 = v_load(d + 4);316a = v_min(a, v0);317b = v_max(b, v0);318v0 = v_load(d);319v_int16x8 q0 = v_min(a, v0);320v_int16x8 q1 = v_max(b, v0);321v0 = v_load(d + 5);322q0 = v_max(q0, v_min(a, v0));323q1 = v_min(q1, v_max(b, v0));324q0 = v_max(q0, v_setzero_s16() - q1);325threshold = v_reduce_max(q0) - 1;326}327else328#endif329{330int a0 = threshold;331for( k = 0; k < 8; k += 2 )332{333int a = std::min((int)d[k+1], (int)d[k+2]);334if( a <= a0 )335continue;336a = std::min(a, (int)d[k+3]);337a = std::min(a, (int)d[k+4]);338a0 = std::max(a0, std::min(a, (int)d[k]));339a0 = std::max(a0, std::min(a, (int)d[k+5]));340}341342int b0 = -a0;343for( k = 0; k < 8; k += 2 )344{345int b = std::max((int)d[k+1], (int)d[k+2]);346b = std::max(b, (int)d[k+3]);347if( b >= b0 )348continue;349b = std::max(b, (int)d[k+4]);350351b0 = std::min(b0, std::max(b, (int)d[k]));352b0 = std::min(b0, std::max(b, (int)d[k+5]));353}354355threshold = -b0-1;356}357358#if VERIFY_CORNERS359testCorner(ptr, pixel, K, N, threshold);360#endif361return threshold;362}363364} // namespace cv365366367