1*77c1e3ccSAndroid Build Coastguard Worker // Copyright (c) 2006, 2008 Edward Rosten
2*77c1e3ccSAndroid Build Coastguard Worker // All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker //
4*77c1e3ccSAndroid Build Coastguard Worker // Redistribution and use in source and binary forms, with or without
5*77c1e3ccSAndroid Build Coastguard Worker // modification, are permitted provided that the following conditions
6*77c1e3ccSAndroid Build Coastguard Worker // are met:
7*77c1e3ccSAndroid Build Coastguard Worker //
8*77c1e3ccSAndroid Build Coastguard Worker // *Redistributions of source code must retain the above copyright
9*77c1e3ccSAndroid Build Coastguard Worker // notice, this list of conditions and the following disclaimer.
10*77c1e3ccSAndroid Build Coastguard Worker //
11*77c1e3ccSAndroid Build Coastguard Worker // *Redistributions in binary form must reproduce the above copyright
12*77c1e3ccSAndroid Build Coastguard Worker // notice, this list of conditions and the following disclaimer in the
13*77c1e3ccSAndroid Build Coastguard Worker // documentation and/or other materials provided with the distribution.
14*77c1e3ccSAndroid Build Coastguard Worker //
15*77c1e3ccSAndroid Build Coastguard Worker // *Neither the name of the University of Cambridge nor the names of
16*77c1e3ccSAndroid Build Coastguard Worker // its contributors may be used to endorse or promote products derived
17*77c1e3ccSAndroid Build Coastguard Worker // from this software without specific prior written permission.
18*77c1e3ccSAndroid Build Coastguard Worker //
19*77c1e3ccSAndroid Build Coastguard Worker // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*77c1e3ccSAndroid Build Coastguard Worker // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*77c1e3ccSAndroid Build Coastguard Worker // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22*77c1e3ccSAndroid Build Coastguard Worker // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23*77c1e3ccSAndroid Build Coastguard Worker // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24*77c1e3ccSAndroid Build Coastguard Worker // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25*77c1e3ccSAndroid Build Coastguard Worker // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26*77c1e3ccSAndroid Build Coastguard Worker // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27*77c1e3ccSAndroid Build Coastguard Worker // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28*77c1e3ccSAndroid Build Coastguard Worker // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29*77c1e3ccSAndroid Build Coastguard Worker // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*77c1e3ccSAndroid Build Coastguard Worker
31*77c1e3ccSAndroid Build Coastguard Worker // clang-format off
32*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
33*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
34*77c1e3ccSAndroid Build Coastguard Worker #include "fast.h"
35*77c1e3ccSAndroid Build Coastguard Worker
36*77c1e3ccSAndroid Build Coastguard Worker
37*77c1e3ccSAndroid Build Coastguard Worker #define Compare(X, Y) ((X)>=(Y))
38*77c1e3ccSAndroid Build Coastguard Worker
aom_nonmax_suppression(const xy * corners,const int * scores,int num_corners,int ** ret_scores,int * ret_num_nonmax)39*77c1e3ccSAndroid Build Coastguard Worker xy* aom_nonmax_suppression(const xy* corners, const int* scores, int num_corners,
40*77c1e3ccSAndroid Build Coastguard Worker int** ret_scores, int* ret_num_nonmax)
41*77c1e3ccSAndroid Build Coastguard Worker {
42*77c1e3ccSAndroid Build Coastguard Worker int num_nonmax=0;
43*77c1e3ccSAndroid Build Coastguard Worker int last_row;
44*77c1e3ccSAndroid Build Coastguard Worker int* row_start;
45*77c1e3ccSAndroid Build Coastguard Worker int i, j;
46*77c1e3ccSAndroid Build Coastguard Worker xy* ret_nonmax;
47*77c1e3ccSAndroid Build Coastguard Worker int* nonmax_scores;
48*77c1e3ccSAndroid Build Coastguard Worker const int sz = (int)num_corners;
49*77c1e3ccSAndroid Build Coastguard Worker
50*77c1e3ccSAndroid Build Coastguard Worker /*Point above points (roughly) to the pixel above the one of interest, if there
51*77c1e3ccSAndroid Build Coastguard Worker is a feature there.*/
52*77c1e3ccSAndroid Build Coastguard Worker int point_above = 0;
53*77c1e3ccSAndroid Build Coastguard Worker int point_below = 0;
54*77c1e3ccSAndroid Build Coastguard Worker
55*77c1e3ccSAndroid Build Coastguard Worker *ret_scores = 0;
56*77c1e3ccSAndroid Build Coastguard Worker *ret_num_nonmax = -1;
57*77c1e3ccSAndroid Build Coastguard Worker if(!(corners && scores) || num_corners < 1)
58*77c1e3ccSAndroid Build Coastguard Worker {
59*77c1e3ccSAndroid Build Coastguard Worker *ret_num_nonmax = 0;
60*77c1e3ccSAndroid Build Coastguard Worker return 0;
61*77c1e3ccSAndroid Build Coastguard Worker }
62*77c1e3ccSAndroid Build Coastguard Worker
63*77c1e3ccSAndroid Build Coastguard Worker ret_nonmax = (xy*)malloc(num_corners * sizeof(xy));
64*77c1e3ccSAndroid Build Coastguard Worker if(!ret_nonmax)
65*77c1e3ccSAndroid Build Coastguard Worker {
66*77c1e3ccSAndroid Build Coastguard Worker return 0;
67*77c1e3ccSAndroid Build Coastguard Worker }
68*77c1e3ccSAndroid Build Coastguard Worker
69*77c1e3ccSAndroid Build Coastguard Worker nonmax_scores = (int*)malloc(num_corners * sizeof(*nonmax_scores));
70*77c1e3ccSAndroid Build Coastguard Worker if (!nonmax_scores)
71*77c1e3ccSAndroid Build Coastguard Worker {
72*77c1e3ccSAndroid Build Coastguard Worker free(ret_nonmax);
73*77c1e3ccSAndroid Build Coastguard Worker return 0;
74*77c1e3ccSAndroid Build Coastguard Worker }
75*77c1e3ccSAndroid Build Coastguard Worker
76*77c1e3ccSAndroid Build Coastguard Worker /* Find where each row begins
77*77c1e3ccSAndroid Build Coastguard Worker (the corners are output in raster scan order). A beginning of -1 signifies
78*77c1e3ccSAndroid Build Coastguard Worker that there are no corners on that row. */
79*77c1e3ccSAndroid Build Coastguard Worker last_row = corners[num_corners-1].y;
80*77c1e3ccSAndroid Build Coastguard Worker row_start = (int*)malloc((last_row+1)*sizeof(int));
81*77c1e3ccSAndroid Build Coastguard Worker if(!row_start)
82*77c1e3ccSAndroid Build Coastguard Worker {
83*77c1e3ccSAndroid Build Coastguard Worker free(ret_nonmax);
84*77c1e3ccSAndroid Build Coastguard Worker free(nonmax_scores);
85*77c1e3ccSAndroid Build Coastguard Worker return 0;
86*77c1e3ccSAndroid Build Coastguard Worker }
87*77c1e3ccSAndroid Build Coastguard Worker
88*77c1e3ccSAndroid Build Coastguard Worker for(i=0; i < last_row+1; i++)
89*77c1e3ccSAndroid Build Coastguard Worker row_start[i] = -1;
90*77c1e3ccSAndroid Build Coastguard Worker
91*77c1e3ccSAndroid Build Coastguard Worker {
92*77c1e3ccSAndroid Build Coastguard Worker int prev_row = -1;
93*77c1e3ccSAndroid Build Coastguard Worker for(i=0; i< num_corners; i++)
94*77c1e3ccSAndroid Build Coastguard Worker if(corners[i].y != prev_row)
95*77c1e3ccSAndroid Build Coastguard Worker {
96*77c1e3ccSAndroid Build Coastguard Worker row_start[corners[i].y] = i;
97*77c1e3ccSAndroid Build Coastguard Worker prev_row = corners[i].y;
98*77c1e3ccSAndroid Build Coastguard Worker }
99*77c1e3ccSAndroid Build Coastguard Worker }
100*77c1e3ccSAndroid Build Coastguard Worker
101*77c1e3ccSAndroid Build Coastguard Worker
102*77c1e3ccSAndroid Build Coastguard Worker
103*77c1e3ccSAndroid Build Coastguard Worker for(i=0; i < sz; i++)
104*77c1e3ccSAndroid Build Coastguard Worker {
105*77c1e3ccSAndroid Build Coastguard Worker int score = scores[i];
106*77c1e3ccSAndroid Build Coastguard Worker xy pos = corners[i];
107*77c1e3ccSAndroid Build Coastguard Worker assert(pos.y <= last_row);
108*77c1e3ccSAndroid Build Coastguard Worker
109*77c1e3ccSAndroid Build Coastguard Worker /*Check left */
110*77c1e3ccSAndroid Build Coastguard Worker if(i > 0)
111*77c1e3ccSAndroid Build Coastguard Worker if(corners[i-1].x == pos.x-1 && corners[i-1].y == pos.y && Compare(scores[i-1], score))
112*77c1e3ccSAndroid Build Coastguard Worker continue;
113*77c1e3ccSAndroid Build Coastguard Worker
114*77c1e3ccSAndroid Build Coastguard Worker /*Check right*/
115*77c1e3ccSAndroid Build Coastguard Worker if(i < (sz - 1))
116*77c1e3ccSAndroid Build Coastguard Worker if(corners[i+1].x == pos.x+1 && corners[i+1].y == pos.y && Compare(scores[i+1], score))
117*77c1e3ccSAndroid Build Coastguard Worker continue;
118*77c1e3ccSAndroid Build Coastguard Worker
119*77c1e3ccSAndroid Build Coastguard Worker /*Check above (if there is a valid row above)*/
120*77c1e3ccSAndroid Build Coastguard Worker if(pos.y > 0 && row_start[pos.y - 1] != -1)
121*77c1e3ccSAndroid Build Coastguard Worker {
122*77c1e3ccSAndroid Build Coastguard Worker /*Make sure that current point_above is one
123*77c1e3ccSAndroid Build Coastguard Worker row above.*/
124*77c1e3ccSAndroid Build Coastguard Worker if(corners[point_above].y < pos.y - 1)
125*77c1e3ccSAndroid Build Coastguard Worker point_above = row_start[pos.y-1];
126*77c1e3ccSAndroid Build Coastguard Worker
127*77c1e3ccSAndroid Build Coastguard Worker /*Make point_above point to the first of the pixels above the current point,
128*77c1e3ccSAndroid Build Coastguard Worker if it exists.*/
129*77c1e3ccSAndroid Build Coastguard Worker for(; corners[point_above].y < pos.y && corners[point_above].x < pos.x - 1; point_above++)
130*77c1e3ccSAndroid Build Coastguard Worker {}
131*77c1e3ccSAndroid Build Coastguard Worker
132*77c1e3ccSAndroid Build Coastguard Worker
133*77c1e3ccSAndroid Build Coastguard Worker for(j=point_above; corners[j].y < pos.y && corners[j].x <= pos.x + 1; j++)
134*77c1e3ccSAndroid Build Coastguard Worker {
135*77c1e3ccSAndroid Build Coastguard Worker int x = corners[j].x;
136*77c1e3ccSAndroid Build Coastguard Worker if( (x == pos.x - 1 || x ==pos.x || x == pos.x+1) && Compare(scores[j], score))
137*77c1e3ccSAndroid Build Coastguard Worker goto cont;
138*77c1e3ccSAndroid Build Coastguard Worker }
139*77c1e3ccSAndroid Build Coastguard Worker
140*77c1e3ccSAndroid Build Coastguard Worker }
141*77c1e3ccSAndroid Build Coastguard Worker
142*77c1e3ccSAndroid Build Coastguard Worker /*Check below (if there is anything below)*/
143*77c1e3ccSAndroid Build Coastguard Worker if (pos.y + 1 < last_row+1 && row_start[pos.y + 1] != -1 && point_below < sz) /*Nothing below*/
144*77c1e3ccSAndroid Build Coastguard Worker {
145*77c1e3ccSAndroid Build Coastguard Worker if(corners[point_below].y < pos.y + 1)
146*77c1e3ccSAndroid Build Coastguard Worker point_below = row_start[pos.y+1];
147*77c1e3ccSAndroid Build Coastguard Worker
148*77c1e3ccSAndroid Build Coastguard Worker /* Make point below point to one of the pixels belowthe current point, if it
149*77c1e3ccSAndroid Build Coastguard Worker exists.*/
150*77c1e3ccSAndroid Build Coastguard Worker for(; point_below < sz && corners[point_below].y == pos.y+1 && corners[point_below].x < pos.x - 1; point_below++)
151*77c1e3ccSAndroid Build Coastguard Worker {}
152*77c1e3ccSAndroid Build Coastguard Worker
153*77c1e3ccSAndroid Build Coastguard Worker for(j=point_below; j < sz && corners[j].y == pos.y+1 && corners[j].x <= pos.x + 1; j++)
154*77c1e3ccSAndroid Build Coastguard Worker {
155*77c1e3ccSAndroid Build Coastguard Worker int x = corners[j].x;
156*77c1e3ccSAndroid Build Coastguard Worker if( (x == pos.x - 1 || x ==pos.x || x == pos.x+1) && Compare(scores[j],score))
157*77c1e3ccSAndroid Build Coastguard Worker goto cont;
158*77c1e3ccSAndroid Build Coastguard Worker }
159*77c1e3ccSAndroid Build Coastguard Worker }
160*77c1e3ccSAndroid Build Coastguard Worker
161*77c1e3ccSAndroid Build Coastguard Worker ret_nonmax[num_nonmax] = corners[i];
162*77c1e3ccSAndroid Build Coastguard Worker nonmax_scores[num_nonmax] = scores[i];
163*77c1e3ccSAndroid Build Coastguard Worker num_nonmax++;
164*77c1e3ccSAndroid Build Coastguard Worker cont:
165*77c1e3ccSAndroid Build Coastguard Worker ;
166*77c1e3ccSAndroid Build Coastguard Worker }
167*77c1e3ccSAndroid Build Coastguard Worker
168*77c1e3ccSAndroid Build Coastguard Worker free(row_start);
169*77c1e3ccSAndroid Build Coastguard Worker *ret_scores = nonmax_scores;
170*77c1e3ccSAndroid Build Coastguard Worker *ret_num_nonmax = num_nonmax;
171*77c1e3ccSAndroid Build Coastguard Worker return ret_nonmax;
172*77c1e3ccSAndroid Build Coastguard Worker }
173*77c1e3ccSAndroid Build Coastguard Worker
174*77c1e3ccSAndroid Build Coastguard Worker // clang-format on
175