rlist.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 List Class:
  9. =================
  10. During segmentation, data regions are defined. The
  11. RegionList class provides a mechanism for doing so, as
  12. well as defines some basic operations, such as region
  13. growing or small region pruning, on the defined regions.
  14. It is defined below. Its prototype is given in (char *)"region.h".
  15. The theory is described in the papers:
  16. D. Comaniciu, P. Meer: Mean Shift: A robust approach toward feature
  17. space analysis.
  18. C. Christoudias, B. Georgescu, P. Meer: Synergism in low level vision.
  19. and they are is available at:
  20. http://www.caip.rutgers.edu/riul/research/papers/
  21. Implemented by Chris M. Christoudias, Bogdan Georgescu
  22. ********************************************************/
  23. #include "rlist.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  27. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  28. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ PUBLIC METHODS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  29. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  30. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  31. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  32. /*** Class Constructor and Destructor ***/
  33. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  34. /*******************************************************/
  35. /*Constructor */
  36. /*******************************************************/
  37. /*Constructor */
  38. /*******************************************************/
  39. /*Pre: */
  40. /* - modesPtr is a pointer to an array of modes */
  41. /* - maxRegions_ is the maximum number of regions */
  42. /* that can be defined */
  43. /* - L_ is the number of data points being class- */
  44. /* ified by the region list class */
  45. /* - N is the dimension of the data set being cl- */
  46. /* assified by the region list class */
  47. /*Post: */
  48. /* - a region list object has been properly init- */
  49. /* ialized. */
  50. /*******************************************************/
  51. RegionList::RegionList(int maxRegions_, int L_, int N_)
  52. {
  53. //Obtain maximum number of regions that can be
  54. //defined by user
  55. if((maxRegions = maxRegions_) <= 0)
  56. ErrorHandler((char *)"RegionList", (char *)"Maximum number of regions is zero or negative.", FATAL);
  57. //Obtain dimension of data set being classified by
  58. //region list class
  59. if((N = N_) <= 0)
  60. ErrorHandler((char *)"RegionList", (char *)"Dimension is zero or negative.", FATAL);
  61. //Obtain length of input data set...
  62. if((L = L_) <= 0)
  63. ErrorHandler((char *)"RegionList", (char *)"Length of data set is zero or negative.", FATAL);
  64. //Allocate memory for index table
  65. if(!(indexTable = new int [L]))
  66. ErrorHandler((char *)"RegionList", (char *)"Not enough memory.", FATAL);
  67. //Allocate memory for region list array
  68. if(!(regionList = new REGION [maxRegions]))
  69. ErrorHandler((char *)"RegionList", (char *)"Not enough memory.", FATAL);
  70. //Initialize region list...
  71. numRegions = freeRegion = 0;
  72. //Initialize indexTable
  73. freeBlockLoc = 0;
  74. //done.
  75. return;
  76. }
  77. /*******************************************************/
  78. /*Destructor */
  79. /*******************************************************/
  80. /*Destroys region list object. */
  81. /*******************************************************/
  82. /*Post: */
  83. /* - region list object has been properly dest- */
  84. /* oyed. */
  85. /*******************************************************/
  86. RegionList::~RegionList( void )
  87. {
  88. //de-allocate memory...
  89. delete [] regionList;
  90. delete [] indexTable;
  91. //done.
  92. return;
  93. }
  94. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  95. /*** Region List Manipulation ***/
  96. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  97. /*******************************************************/
  98. /*Add Region */
  99. /*******************************************************/
  100. /*Adds a region to the region list. */
  101. /*******************************************************/
  102. /*Pre: */
  103. /* - label is a positive integer used to uniquely */
  104. /* identify a region */
  105. /* - pointCount is the number of N-dimensional */
  106. /* data points that exist in the region being */
  107. /* classified. */
  108. /* - indeces is a set of indeces specifying the */
  109. /* data points contained within this region */
  110. /* - pointCount must be > 0 */
  111. /*Post: */
  112. /* - a new region labeled using label and contai- */
  113. /* ning pointCount number of points has been */
  114. /* added to the region list. */
  115. /*******************************************************/
  116. void RegionList::AddRegion(int label, int pointCount, int *indeces)
  117. {
  118. //make sure that there is enough room for this new region
  119. //in the region list array...
  120. if(numRegions >= maxRegions)
  121. ErrorHandler((char *)"AddRegion", (char *)"Not enough memory allocated.", FATAL);
  122. //make sure that label is positive and point Count > 0...
  123. if((label < 0)||(pointCount <= 0))
  124. ErrorHandler((char *)"AddRegion", (char *)"Label is negative or number of points in region is invalid.", FATAL);
  125. //make sure that there is enough memory in the indexTable
  126. //for this region...
  127. if((freeBlockLoc + pointCount) > L)
  128. ErrorHandler((char *)"AddRegion", (char *)"Adding more points than what is contained in data set.", FATAL);
  129. //place new region into region list array using
  130. //freeRegion index
  131. regionList[freeRegion].label = label;
  132. regionList[freeRegion].pointCount = pointCount;
  133. regionList[freeRegion].region = freeBlockLoc;
  134. //copy indeces into indexTable using freeBlock...
  135. int i;
  136. for(i = 0; i < pointCount; i++)
  137. indexTable[freeBlockLoc+i] = indeces[i];
  138. //increment freeBlock to point to the next free
  139. //block
  140. freeBlockLoc += pointCount;
  141. //increment freeRegion to point to the next free region
  142. //also, increment numRegions to indicate that another
  143. //region has been added to the region list
  144. freeRegion++;
  145. numRegions++;
  146. //done.
  147. return;
  148. }
  149. /*******************************************************/
  150. /*Reset */
  151. /*******************************************************/
  152. /*Resets the region list. */
  153. /*******************************************************/
  154. /*Post: */
  155. /* - the region list has been reset. */
  156. /*******************************************************/
  157. void RegionList::Reset( void )
  158. {
  159. //reset region list
  160. freeRegion = numRegions = freeBlockLoc = 0;
  161. //done.
  162. return;
  163. }
  164. /*/\/\/\/\/\/\/\/\/\/\*/
  165. /* Query Region List */
  166. /*\/\/\/\/\/\/\/\/\/\/*/
  167. /*******************************************************/
  168. /*Get Number Regions */
  169. /*******************************************************/
  170. /*Returns the number of regions stored by region list. */
  171. /*******************************************************/
  172. /*Post: */
  173. /* - the number of regions stored by the region */
  174. /* list is returned. */
  175. /*******************************************************/
  176. int RegionList::GetNumRegions( void )
  177. {
  178. // return region count
  179. return numRegions;
  180. }
  181. /*******************************************************/
  182. /*Get Label */
  183. /*******************************************************/
  184. /*Returns the label of a specified region. */
  185. /*******************************************************/
  186. /*Pre: */
  187. /* - regionNum is an index into the region list */
  188. /* array. */
  189. /*Post: */
  190. /* - the label of the region having region index */
  191. /* specified by regionNum has been returned. */
  192. /*******************************************************/
  193. int RegionList::GetLabel(int regionNum)
  194. {
  195. //return the label of a specified region
  196. return regionList[regionNum].label;
  197. }
  198. /*******************************************************/
  199. /*Get Region Count */
  200. /*******************************************************/
  201. /*Returns the point count of a specified region. */
  202. /*******************************************************/
  203. /*Pre: */
  204. /* - regionNum is an index into the region list */
  205. /* array. */
  206. /*Post: */
  207. /* - the number of points that classify the */
  208. /* region whose index is specified by regionNum */
  209. /* is returned. */
  210. /*******************************************************/
  211. int RegionList::GetRegionCount(int regionNum)
  212. {
  213. //return the region count of a specified region
  214. return regionList[regionNum].pointCount;
  215. }
  216. /*******************************************************/
  217. /*Get Region Indeces */
  218. /*******************************************************/
  219. /*Returns the point indeces specifying a region. */
  220. /*******************************************************/
  221. /*Pre: */
  222. /* - regionNum is an index into the region list */
  223. /* array. */
  224. /*Post: */
  225. /* - the region indeces specifying the points */
  226. /* contained by the region specified by region- */
  227. /* Num are returned. */
  228. /*******************************************************/
  229. int *RegionList::GetRegionIndeces(int regionNum)
  230. {
  231. //return point indeces using regionNum
  232. return &indexTable[regionList[regionNum].region];
  233. }
  234. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  235. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  236. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ PRIVATE METHODS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  237. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  238. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  239. /*/\/\/\/\/\/\/\/\/\/\/\*/
  240. /* Class Error Handler */
  241. /*\/\/\/\/\/\/\/\/\/\/\/*/
  242. /*******************************************************/
  243. /*Error Handler */
  244. /*******************************************************/
  245. /*Class error handler. */
  246. /*******************************************************/
  247. /*Pre: */
  248. /* - functName is the name of the function that */
  249. /* caused an error */
  250. /* - errmsg is the error message given by the */
  251. /* calling function */
  252. /* - status is the error status: FATAL or NON- */
  253. /* FATAL */
  254. /*Post: */
  255. /* - the error message errmsg is flagged on beh- */
  256. /* ave of function functName. */
  257. /* - if the error status is FATAL then the program*/
  258. /* is halted, otherwise execution is continued, */
  259. /* error recovery is assumed to be handled by */
  260. /* the calling function. */
  261. /*******************************************************/
  262. void RegionList::ErrorHandler(char *functName, char* errmsg, ErrorType status)
  263. {
  264. //flag error message on behalf of calling function, error format
  265. //specified by the error status...
  266. if(status == NONFATAL)
  267. fprintf(stderr, (char *)"\n%s Error: %s\n", functName, errmsg);
  268. else
  269. {
  270. fprintf(stderr, (char *)"\n%s Fatal Error: %s\n\nAborting Program.\n\n", functName, errmsg);
  271. exit(1);
  272. }
  273. }
  274. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  275. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  276. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ END OF CLASS DEFINITION @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  277. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
  278. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/