RAList.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*******************************************************
  2. Mean Shift Analysis Library
  3. =============================================
  4. The mean shift library is a collection of routines
  5. that use the mean shift algorithm. Using this algorithm,
  6. the necessary output will be generated needed
  7. to analyze a given input set of data.
  8. Region Adjacency List:
  9. =====================
  10. The Region Adjacency List class is used by the Image
  11. Processor class in the construction of a Region Adjacency
  12. Matrix, used by this class to applying transitive closure
  13. and to prune spurious regions during image segmentation.
  14. The definition of the RAList class is provided below. Its
  15. prototype is provided in "RAList.h".
  16. The theory is described in the papers:
  17. D. Comaniciu, P. Meer: Mean Shift: A robust approach toward feature
  18. space analysis.
  19. C. Christoudias, B. Georgescu, P. Meer: Synergism in low level vision.
  20. and they are is available at:
  21. http://www.caip.rutgers.edu/riul/research/papers/
  22. Implemented by Chris M. Christoudias, Bogdan Georgescu
  23. ********************************************************/
  24. //include Region Adjacency List class prototype
  25. #include "RAList.h"
  26. //include needed libraries
  27. #include <stdio.h>
  28. #include <assert.h>
  29. #include <stdlib.h>
  30. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  31. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  32. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ PUBLIC METHODS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  33. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  34. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  35. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  36. /* Class Constructor and Destructor */
  37. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  38. /*******************************************************/
  39. /*Class Constructor */
  40. /*******************************************************/
  41. /*Constructs a RAList object. */
  42. /*******************************************************/
  43. /*Post: */
  44. /* - a RAlist object has been properly constru- */
  45. /* cted. */
  46. /*******************************************************/
  47. RAList::RAList( void )
  48. {
  49. //initialize label and link
  50. label = -1;
  51. next = NULL;
  52. //initialize edge strenght weight and count
  53. edgeStrength = 0;
  54. edgePixelCount = 0;
  55. }
  56. /*******************************************************/
  57. /*Class Destructor */
  58. /*******************************************************/
  59. /*Destructrs a RAList object. */
  60. /*******************************************************/
  61. /*Post: */
  62. /* - the RAList object has been properly dest- */
  63. /* ructed. */
  64. /*******************************************************/
  65. RAList::~RAList( void )
  66. {
  67. //do nothing
  68. }
  69. /*******************************************************/
  70. /*Insert */
  71. /*******************************************************/
  72. /*Insert a region node into the region adjacency list. */
  73. /*******************************************************/
  74. /*Pre: */
  75. /* - entry is a node representing a connected re- */
  76. /* gion */
  77. /*Post: */
  78. /* - entry has been inserted into the region adj- */
  79. /* acency list if it does not already exist */
  80. /* there. */
  81. /* - if the entry already exists in the region */
  82. /* adjacency list 1 is returned otherwise 0 is */
  83. /* returned. */
  84. /*******************************************************/
  85. int RAList::Insert(RAList *entry)
  86. {
  87. //if the list contains only one element
  88. //then insert this element into next
  89. if(!next)
  90. {
  91. //insert entry
  92. next = entry;
  93. entry->next = NULL;
  94. //done
  95. return 0;
  96. }
  97. //traverse the list until either:
  98. //(a) entry's label already exists - do nothing
  99. //(b) the list ends or the current label is
  100. // greater than entry's label, thus insert the entry
  101. // at this location
  102. //check first entry
  103. if(next->label > entry->label)
  104. {
  105. //insert entry into the list at this location
  106. entry->next = next;
  107. next = entry;
  108. //done
  109. return 0;
  110. }
  111. //check the rest of the list...
  112. exists = 0;
  113. cur = next;
  114. while(cur)
  115. {
  116. if(entry->label == cur->label)
  117. {
  118. //node already exists
  119. exists = 1;
  120. break;
  121. }
  122. else if((!(cur->next))||(cur->next->label > entry->label))
  123. {
  124. //insert entry into the list at this location
  125. entry->next = cur->next;
  126. cur->next = entry;
  127. break;
  128. }
  129. //traverse the region adjacency list
  130. cur = cur->next;
  131. }
  132. //done. Return exists indicating whether or not a new node was
  133. // actually inserted into the region adjacency list.
  134. return (int)(exists);
  135. }
  136. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  137. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  138. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ END OF CLASS DEFINITION @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  139. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  140. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/