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