nonmax.c 4.6 KB

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