00001 #include <stdlib.h>
00002 #include "fast.h"
00003
00004
00005 #define Compare(X, Y) ((X)>=(Y))
00006
00007 xy* nonmax_suppression(const xy* corners, const int* scores, int num_corners, int* ret_num_nonmax)
00008 {
00009 int num_nonmax=0;
00010 int last_row;
00011 int* row_start;
00012 int i, j;
00013 xy* ret_nonmax;
00014 const int sz = (int)num_corners;
00015
00016
00017
00018 int point_above = 0;
00019 int point_below = 0;
00020
00021
00022 if(num_corners < 1)
00023 {
00024 *ret_num_nonmax = 0;
00025 return 0;
00026 }
00027
00028 ret_nonmax = (xy*)malloc(num_corners * sizeof(xy));
00029
00030
00031
00032
00033 last_row = corners[num_corners-1].y;
00034 row_start = (int*)malloc((last_row+1)*sizeof(int));
00035
00036 for(i=0; i < last_row+1; i++)
00037 row_start[i] = -1;
00038
00039 {
00040 int prev_row = -1;
00041 for(i=0; i< num_corners; i++)
00042 if(corners[i].y != prev_row)
00043 {
00044 row_start[corners[i].y] = i;
00045 prev_row = corners[i].y;
00046 }
00047 }
00048
00049
00050
00051 for(i=0; i < sz; i++)
00052 {
00053 int score = scores[i];
00054 xy pos = corners[i];
00055
00056
00057 if(i > 0)
00058 if(corners[i-1].x == pos.x-1 && corners[i-1].y == pos.y && Compare(scores[i-1], score))
00059 continue;
00060
00061
00062 if(i < (sz - 1))
00063 if(corners[i+1].x == pos.x+1 && corners[i+1].y == pos.y && Compare(scores[i+1], score))
00064 continue;
00065
00066
00067 if(pos.y != 0 && row_start[pos.y - 1] != -1)
00068 {
00069
00070
00071 if(corners[point_above].y < pos.y - 1)
00072 point_above = row_start[pos.y-1];
00073
00074
00075
00076 for(; corners[point_above].y < pos.y && corners[point_above].x < pos.x - 1; point_above++)
00077 {}
00078
00079
00080 for(j=point_above; corners[j].y < pos.y && corners[j].x <= pos.x + 1; j++)
00081 {
00082 int x = corners[j].x;
00083 if( (x == pos.x - 1 || x ==pos.x || x == pos.x+1) && Compare(scores[j], score))
00084 goto cont;
00085 }
00086
00087 }
00088
00089
00090 if(pos.y != last_row && row_start[pos.y + 1] != -1 && point_below < sz)
00091 {
00092 if(corners[point_below].y < pos.y + 1)
00093 point_below = row_start[pos.y+1];
00094
00095
00096
00097 for(; point_below < sz && corners[point_below].y == pos.y+1 && corners[point_below].x < pos.x - 1; point_below++)
00098 {}
00099
00100 for(j=point_below; j < sz && corners[j].y == pos.y+1 && corners[j].x <= pos.x + 1; j++)
00101 {
00102 int x = corners[j].x;
00103 if( (x == pos.x - 1 || x ==pos.x || x == pos.x+1) && Compare(scores[j],score))
00104 goto cont;
00105 }
00106 }
00107
00108 ret_nonmax[num_nonmax++] = corners[i];
00109 cont:
00110 ;
00111 }
00112
00113 free(row_start);
00114 *ret_num_nonmax = num_nonmax;
00115 return ret_nonmax;
00116 }
00117