ms.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. MeanShift Base Class:
  9. ====================
  10. The mean shift library of routines is realized
  11. via the creation of a MeanShift base class. This class
  12. provides a mechanism for calculating the mean shift vector
  13. at a specified data point, using an arbitrary N-dimensional
  14. data set, and a user-defined kernel.
  15. For image processing the mean shift base class also allows
  16. for the definition of a data set that is on a two-dimensional
  17. lattice. The amount of time needed to compute the mean shift
  18. vector using such a data set is much less than that of an
  19. arbitrary one. Because images usually contain many data points,
  20. defining the image input data points as being on a lattice
  21. greatly improves computation time and makes algorithms such
  22. as image filtering practical.
  23. The MeanShift class prototype is provided below. Its
  24. definition is provided in 'ms.cc'.
  25. The theory is described in the papers:
  26. D. Comaniciu, P. Meer: Mean Shift: A robust approach toward feature
  27. space analysis.
  28. C. Christoudias, B. Georgescu, P. Meer: Synergism in low level vision.
  29. and they are is available at:
  30. http://www.caip.rutgers.edu/riul/research/papers/
  31. Implemented by Chris M. Christoudias, Bogdan Georgescu
  32. ********************************************************/
  33. #ifndef MS_H
  34. #define MS_H
  35. //Included needed libraries
  36. //Include type definitions
  37. #include "tdef.h"
  38. //include mean shift system used
  39. //for function timing and system output
  40. #include "msSys.h"
  41. //Include Debugging Constant
  42. //#define DEBUG
  43. //Define Prompt - Prompts user on progress of Mean Shift algorithm
  44. #undef PROMPT
  45. //Define Show Progress - Prompts user on percent complete of a given
  46. // mean shift algorithm
  47. #undef SHOW_PROGRESS
  48. //Define Progress Rate - Indicates the number of convergences before
  49. // checking progress
  50. #define PROGRESS_RATE 100
  51. // Define Macros
  52. #define SWAP(d_a, d_b) temp=(d_a);(d_a)=(d_b);(d_b)=temp;
  53. // Define Structures
  54. //k-Dimensional Binary Search Tree
  55. struct tree {
  56. float *x;
  57. tree *right;
  58. tree *left;
  59. tree *parent;
  60. };
  61. // User Defined Weight Function
  62. struct userWeightFunct {
  63. double *w;
  64. double halfWindow;
  65. int sampleNumber;
  66. int subspace;
  67. userWeightFunct *next;
  68. };
  69. //Define class state structure
  70. struct ClassStateStruct {
  71. bool KERNEL_DEFINED;
  72. bool INPUT_DEFINED;
  73. bool LATTICE_DEFINED;
  74. bool OUTPUT_DEFINED;
  75. };
  76. // Define Constants
  77. // Threshold
  78. const double EPSILON2 = 0.01; // define threshold (approx. Value of Mh at a peak or plateau)
  79. const double MU = 0.05; // define threshold required that window is near convergence
  80. const double TC_DIST_FACTOR = 0.5; // cluster search windows near convergence that are a distance
  81. // h[i]*TC_DIST_FACTOR of one another (transitive closure)
  82. const double SQ_TC_DFACTOR = 0.0625; // (TC_DIST_FACTOR)^2
  83. const int LIMIT = 100; // define max. # of iterations to find mode
  84. // Gaussian Lookup Table
  85. const int GAUSS_NUM_ELS = 16; // take 16 samples of exp(-u/2)
  86. const double GAUSS_LIMIT = 2.9; // GAUSS_LIMIT = c
  87. const double GAUSS_INCREMENT = GAUSS_LIMIT * GAUSS_LIMIT / GAUSS_NUM_ELS;
  88. // GAUSS_INCREMENT = (c^2)/(# of samples)
  89. // Numerical Analysis
  90. const double DELTA = 0.00001; // used for floating point to integer conversion
  91. //MeanShift Prototype
  92. class MeanShift {
  93. public:
  94. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  95. /* Class Constructor and Destructor */
  96. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  97. MeanShift( void ); //Default Constructor
  98. ~MeanShift( void ); //Class Destructor
  99. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  100. /* Creation/Initialization of Mean Shift Kernel */
  101. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  102. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  103. //<--------------------------------------------------->|//
  104. //| |//
  105. //| Method Name: |//
  106. //| ============ |//
  107. //| * Define Kernel * |//
  108. //| |//
  109. //<--------------------------------------------------->|//
  110. //| |//
  111. //| Description: |//
  112. //| ============ |//
  113. //| |//
  114. //| Uploads a custom kernel into the private data |//
  115. //| members of the mean shift class. This kernel is |//
  116. //| used by the mean shift class to perform mean |//
  117. //| shift. |//
  118. //| |//
  119. //| In order to create a valid kernel the following |//
  120. //| argumens must be provided this method: |//
  121. //| |//
  122. //| <* kernel *> |//
  123. //| A one dimensional array of type kernelType used |//
  124. //| to specify the kernel type (Uniform, Gaussian, |//
  125. //| or User Defined) of a given subspace of the input|//
  126. //| set. Entry i of kernel correlates to the i-th |//
  127. //| subspace of the input data set. |//
  128. //| |//
  129. //| <* h *> |//
  130. //| A one dimensional array of floating point numb- |//
  131. //| ers that are used to normalize the input data |//
  132. //| set, each bandwidth specifying the relative imp- |//
  133. //| ortance of a subspace of the input data set. |//
  134. //| |//
  135. //| <* kp *> |//
  136. //| An integer that specifies the number of sub- |//
  137. //| contained by the input data set. Both P and h |//
  138. //| therefore consist of kp entries. |//
  139. //| |//
  140. //<--------------------------------------------------->|//
  141. //| |//
  142. //| Usage: |//
  143. //| ====== |//
  144. //| DefineKernel(kernel, h, P, kp) |//
  145. //| |//
  146. //<--------------------------------------------------->|//
  147. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  148. void DefineKernel(kernelType*, float*, int*, int);
  149. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  150. //<--------------------------------------------------->|//
  151. //| |//
  152. //| Method Name: |//
  153. //| ============ |//
  154. //| * Add Weight Function * |//
  155. //| |//
  156. //<--------------------------------------------------->|//
  157. //| |//
  158. //| Description: |//
  159. //| ============ |//
  160. //| |//
  161. //| Each subspace specified as User Defined is un- |//
  162. //| quely defined by a correlating weight function |//
  163. //| which is user defined. |//
  164. //| |//
  165. //| A weight function w(u) exhibits the following |//
  166. //| properties: |//
  167. //| |//
  168. //| (1) w(u) = w(-u) |//
  169. //| (2) u = ((x_i-y_k)^2)/(h^2) (see docs) |//
  170. //| (3) w(u) = 0, for |u| >= halfWindow |//
  171. //| |//
  172. //| To add a weight function to the mean shift class |//
  173. //| the following must be specified: |//
  174. //| |//
  175. //| <* g() *> |//
  176. //| A pointer the weight function w(u) exhibiting |//
  177. //| the above properties. |//
  178. //| |//
  179. //| <* halfWindow *> |//
  180. //| A floating point number specifying where w(u) |//
  181. //| exists (is non zero). [See Property 3 Above] |//
  182. //| |//
  183. //| <* sampleNumber *> |//
  184. //| An integer used to specify the number of samples |//
  185. //| used to describe w(u). Linear interpolation is |//
  186. //| used during the mean shift calculation using the |//
  187. //| the samples of w(u) to determine the value of w |//
  188. //| at a location |u| < halfWindow. |//
  189. //| |//
  190. //| <* subspace *> |//
  191. //| An integer specifying which kernel w(u) defines. |//
  192. //| |//
  193. //| Weight functions are accounted for every time |//
  194. //| a new kernel is created. |//
  195. //| |//
  196. //| If a weight function is added to non-existing |//
  197. //| subspace of the input data set (example: the |//
  198. //| input data set containes 3 subspaces and this |//
  199. //| method is given subspace = 4) then the weight |//
  200. //| defintion will simply be ignored by the mean |//
  201. //| shift class. |//
  202. //| |//
  203. //| If a subspace is declared as kernel type User |//
  204. //| Defined and a weight function is not defined |//
  205. //| for that subspace a fatal error will occur. |//
  206. //| |//
  207. //<--------------------------------------------------->|//
  208. //| |//
  209. //| Usage: |//
  210. //| ====== |//
  211. //| AddWeightFunction(g(u) , halfWindow, |//
  212. //| sampleNumber, subspace); |//
  213. //| |//
  214. //<--------------------------------------------------->|//
  215. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  216. void AddWeightFunction(double g(double), float, int, int);
  217. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  218. //<--------------------------------------------------->|//
  219. //| |//
  220. //| Method Name: |//
  221. //| ============ |//
  222. //| * Clear Weight Functions * |//
  223. //| |//
  224. //<--------------------------------------------------->|//
  225. //| |//
  226. //| Description: |//
  227. //| ============ |//
  228. //| |//
  229. //| Removes all user defined weight functions added |//
  230. //| using method AddWeightFunction() from the |//
  231. //| private data members of the mean shift class. |//
  232. //| |//
  233. //<--------------------------------------------------->|//
  234. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  235. void ClearWeightFunctions( void );
  236. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  237. /* Input Data Set Declaration */
  238. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  239. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  240. //<--------------------------------------------------->|//
  241. //| |//
  242. //| Method Name: |//
  243. //| ============ |//
  244. //| * Define Input * |//
  245. //| |//
  246. //<--------------------------------------------------->|//
  247. //| |//
  248. //| Description: |//
  249. //| ============ |//
  250. //| |//
  251. //| Uploads a one dimensional array containing L |//
  252. //| N-dimensional data points into the mean shift |//
  253. //| class. |//
  254. //| |//
  255. //| An input data set is specified by: |//
  256. //| |//
  257. //| <* x *> |//
  258. //| A pointer to a floating point array. |//
  259. //| |//
  260. //| <* L *> |//
  261. //| The number of data points stored by x. |//
  262. //| |//
  263. //| <* N *> |//
  264. //| The dimension of the data points stored by x. |//
  265. //| |//
  266. //| The input x has the following format: |//
  267. //| |//
  268. //| x = <x11, x12,..., x1N,..., xL1, xL2,..., xLN> |//
  269. //| |//
  270. //<--------------------------------------------------->|//
  271. //| |//
  272. //| Usage: |//
  273. //| ====== |//
  274. //| DefineInput(x, L, N) |//
  275. //| |//
  276. //<--------------------------------------------------->|//
  277. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  278. void DefineInput(float*, int, int);
  279. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  280. //<--------------------------------------------------->|//
  281. //| |//
  282. //| Method Name: |//
  283. //| ============ |//
  284. //| * Define Lattice Input * |//
  285. //| |//
  286. //<--------------------------------------------------->|//
  287. //| |//
  288. //| Description: |//
  289. //| ============ |//
  290. //| |//
  291. //| Use this method to specify define an input data |//
  292. //| set defined on a lattice. |//
  293. //| |//
  294. //| The arguments of this method are: |//
  295. //| |//
  296. //| <* x *> |//
  297. //| A pointer to a floating point array containing |//
  298. //| height*width, N-dimensional data points. |//
  299. //| |//
  300. //| <* height *> |//
  301. //| An integer specifying the height of the lattice. |//
  302. //| |//
  303. //| <* width *> |//
  304. //| An integer specifying the width of the lattice. |//
  305. //| |//
  306. //| <* N *> |//
  307. //| The dimension of the data points stored by x. |//
  308. //| |//
  309. //<--------------------------------------------------->|//
  310. //| |//
  311. //| Usage: |//
  312. //| ====== |//
  313. //| DefineLInput(x, height, width, N) |//
  314. //| |//
  315. //<--------------------------------------------------->|//
  316. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  317. void DefineLInput(float*, int, int, int);
  318. /*/\/\/\/\/\/\/\/\/\/\/\*/
  319. /* Lattice Weight Map */
  320. /*\/\/\/\/\/\/\/\/\/\/\/*/
  321. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  322. //<--------------------------------------------------->|//
  323. //| |//
  324. //| Method Name: |//
  325. //| ============ |//
  326. //| * Set Lattice Weight Map * |//
  327. //| |//
  328. //<--------------------------------------------------->|//
  329. //| |//
  330. //| Description: |//
  331. //| ============ |//
  332. //| |//
  333. //| Uploads weight map specifying for each data |//
  334. //| point a value used to weight the uniform kernel |//
  335. //| when computing mean shift. |//
  336. //| |//
  337. //| The arguments to this method are: |//
  338. //| |//
  339. //| <* weightMap *> |//
  340. //| A floating point array of size L specifying for |//
  341. //| each data point a weight. |//
  342. //| |//
  343. //| Note: DefineLInput must be called prior to call- |//
  344. //| ing this method. DefineLInput is used to |//
  345. //| define the dimensions of the input data |//
  346. //| set. |//
  347. //| |//
  348. //| |//
  349. //| The weight map is used to weight the uniform |//
  350. //| kernel used to computing meanshift on a data |//
  351. //| point situated on a lattice. Alternatively, a |//
  352. //| weight function may defined, however, if speed |//
  353. //| is an issue, the lattice may be exploited to |//
  354. //| result in a faster implementation of a weighted |//
  355. //| kernel. |//
  356. //| |//
  357. //<--------------------------------------------------->|//
  358. //| |//
  359. //| Usage: |//
  360. //| ====== |//
  361. //| SetLatticeWeightMap(weightMap) |//
  362. //| |//
  363. //<--------------------------------------------------->|//
  364. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  365. void SetLatticeWeightMap(float*);
  366. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  367. //<--------------------------------------------------->|//
  368. //| |//
  369. //| Method Name: |//
  370. //| ============ |//
  371. //| * Remove Lattice Weight Map * |//
  372. //| |//
  373. //<--------------------------------------------------->|//
  374. //| |//
  375. //| Description: |//
  376. //| ============ |//
  377. //| |//
  378. //| Removes lattice weight map. An error is NOT |//
  379. //| flagged if a weight map was not defined prior |//
  380. //| to calling this method. |//
  381. //| |//
  382. //<--------------------------------------------------->|//
  383. //| |//
  384. //| Usage: |//
  385. //| ====== |//
  386. //| RemoveLatticeWeightMap(weightMap) |//
  387. //| |//
  388. //<--------------------------------------------------->|//
  389. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  390. void RemoveLatticeWeightMap(void);
  391. /*/\/\/\/\/\/\/\/\/\/\/\/\*/
  392. /* Mean Shift Operations */
  393. /*\/\/\/\/\/\/\/\/\/\/\/\/*/
  394. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  395. //<--------------------------------------------------->|//
  396. //| |//
  397. //| Method Name: |//
  398. //| ============ |//
  399. //| * Mean Shift Vector * |//
  400. //| |//
  401. //<--------------------------------------------------->|//
  402. //| |//
  403. //| Description: |//
  404. //| ============ |//
  405. //| |//
  406. //| If a kernel is created and input is uploaded, |//
  407. //| this method calcualtes the mean shift vector, |//
  408. //| Mh, at specific data point yk. |//
  409. //| |//
  410. //| The arguments of this method are: |//
  411. //| |//
  412. //| <* Mh *> |//
  413. //| An array of N doubles storing the N dimensional |//
  414. //| mean shift vector. |//
  415. //| |//
  416. //| <* yk *> |//
  417. //| An array of N doubles storing the N dimensional |//
  418. //| data point where the mean shift vector is to be |//
  419. //| calculate. |//
  420. //| |//
  421. //<--------------------------------------------------->|//
  422. //| |//
  423. //| Usage: |//
  424. //| ====== |//
  425. //| msVector(Mh, yk) |//
  426. //| |//
  427. //<--------------------------------------------------->|//
  428. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  429. void msVector(double*, double*);
  430. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  431. //<--------------------------------------------------->|//
  432. //| |//
  433. //| Method Name: |//
  434. //| ============ |//
  435. //| * Lattice Mean Shift Vector * |//
  436. //| |//
  437. //<--------------------------------------------------->|//
  438. //| |//
  439. //| Description: |//
  440. //| ============ |//
  441. //| |//
  442. //| If a kernel is created and input is uploaded, |//
  443. //| this method calcualtes the mean shift vector, |//
  444. //| Mh, at specific data point yk, assuming that the |//
  445. //| data set exhists on a height x width two dim- |//
  446. //| ensional lattice. |//
  447. //| |//
  448. //| The arguments of this method are: |//
  449. //| |//
  450. //| <* Mh *> |//
  451. //| An array of N doubles storing the N dimensional |//
  452. //| mean shift vector. |//
  453. //| |//
  454. //| <* yk *> |//
  455. //| An array of N doubles storing the N dimensional |//
  456. //| data point where the mean shift vector is to be |//
  457. //| calculate. |//
  458. //| |//
  459. //| The height and width of the lattice must be |//
  460. //| specified using DefineLattice() method. If this |//
  461. //| is not performed prior to calling this method a |//
  462. //| fatal error will be flagged. |//
  463. //| |//
  464. //<--------------------------------------------------->|//
  465. //| |//
  466. //| Usage: |//
  467. //| ====== |//
  468. //| latticeMSVector(Mh, yk) |//
  469. //| |//
  470. //<--------------------------------------------------->|//
  471. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  472. void latticeMSVector(double*, double*);
  473. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  474. //<--------------------------------------------------->|//
  475. //| |//
  476. //| Method Name: |//
  477. //| ============ |//
  478. //| * Find Mode * |//
  479. //| |//
  480. //<--------------------------------------------------->|//
  481. //| |//
  482. //| Description: |//
  483. //| ============ |//
  484. //| |//
  485. //| If a kernel is created and input is uploaded, |//
  486. //| this method calcualtes the mode of a specified |//
  487. //| data point yk. |//
  488. //| |//
  489. //| The arguments of this method are: |//
  490. //| |//
  491. //| <* mode *> |//
  492. //| An array of N doubles storing the N dimensional |//
  493. //| mode of yk. |//
  494. //| |//
  495. //| <* yk *> |//
  496. //| An array of N doubles storing the N dimensional |//
  497. //| data point where the mean shift vector is to be |//
  498. //| calculate. |//
  499. //| |//
  500. //<--------------------------------------------------->|//
  501. //| |//
  502. //| Usage: |//
  503. //| ====== |//
  504. //| FindMode(mode, yk) |//
  505. //| |//
  506. //<--------------------------------------------------->|//
  507. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  508. void FindMode(double*, double*);
  509. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  510. //<--------------------------------------------------->|//
  511. //| |//
  512. //| Method Name: |//
  513. //| ============ |//
  514. //| * Find Lattice Mode * |//
  515. //| |//
  516. //<--------------------------------------------------->|//
  517. //| |//
  518. //| Description: |//
  519. //| ============ |//
  520. //| |//
  521. //| If a kernel is created and input is uploaded, |//
  522. //| this method calcualtes the mode of a specified |//
  523. //| data point yk, assuming that the data set |//
  524. //| exhists on a height x width two dimensional |//
  525. //| lattice. |//
  526. //| |//
  527. //| The arguments of this method are: |//
  528. //| |//
  529. //| <* mode *> |//
  530. //| An array of N doubles storing the N dimensional |//
  531. //| mode of yk. |//
  532. //| |//
  533. //| <* yk *> |//
  534. //| An array of N doubles storing the N dimensional |//
  535. //| data point where the mean shift vector is to be |//
  536. //| calculate. |//
  537. //| |//
  538. //| The height and width of the lattice must be |//
  539. //| specified using DefineLattice() method. If this |//
  540. //| is not performed prior to calling this method a |//
  541. //| fatal error will be flagged. |//
  542. //| |//
  543. //<--------------------------------------------------->|//
  544. //| |//
  545. //| Usage: |//
  546. //| ====== |//
  547. //| FindLMode(mode, yk) |//
  548. //| |//
  549. //<--------------------------------------------------->|//
  550. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  551. void FindLMode(double*, double*);
  552. /*/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  553. /* Error Handler Mechanism */
  554. /*/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  555. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  556. //<--------------------------------------------------->|//
  557. //| |//
  558. //| Description: |//
  559. //| ============ |//
  560. //| |//
  561. //| ErrorMessage is an error message that is set by |//
  562. //| a mean shift library class when an error occurs. |//
  563. //| |//
  564. //<--------------------------------------------------->|//
  565. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  566. char *ErrorMessage;
  567. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  568. //<--------------------------------------------------->|//
  569. //| |//
  570. //| Description: |//
  571. //| ============ |//
  572. //| |//
  573. //| ErrorStatus indicates if an error has occured as |//
  574. //| a result of improper use of a mean shift library |//
  575. //| class method or because of insufficient resour- |//
  576. //| ces. ErrorStatus is set to EL_ERROR (ErrorStatus |//
  577. //| = 1) if an error has occured. If no error occur- |//
  578. //| ed when calling a particular method ErrorStatus |//
  579. //| is set to EL_OKAY (ErrorStatus = 0). |//
  580. //| |//
  581. //<--------------------------------------------------->|//
  582. //--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//--\\||//
  583. ErrorLevel ErrorStatus;
  584. protected:
  585. //==========================
  586. // *** Protected Methods ***
  587. //==========================
  588. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  589. /* Mean Shift: Using kd-Tree */
  590. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  591. /////////////////////////////////////////
  592. // <<*>> Usage: MSVector(Mh, yk) <<*>> //
  593. /////////////////////////////////////////
  594. void MSVector (double*, double*); // Computes the mean shift vector at a specified
  595. // window location yk in the data set x given
  596. // the vector yk
  597. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  598. /* Mean Shift: Using Lattice */
  599. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  600. ////////////////////////////////////////////////
  601. // <<*>> Usage: LatticeMSVector(Mh, yk) <<*>> //
  602. ////////////////////////////////////////////////
  603. void LatticeMSVector (double*, double*); // Uses the lattice defined by DefineLattice to compute the
  604. // mean shift vector at a specified window location yk
  605. ///////////////////////////////////////////////////
  606. // <<*>> Usage: OptLatticeMSVector(Mh, yk) <<*>> //
  607. ///////////////////////////////////////////////////
  608. void OptLatticeMSVector (double*, double*); // Uses the lattice defined by DefineLattice to compute the
  609. // mean shift vector at a specified window location yk using
  610. // the basin of attraction optimization for better performace
  611. // during mean shift filtering - used by a derived class
  612. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  613. /* Kernel-Input Data Consistency */
  614. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  615. /////////////////////////////////////////////////
  616. // <<*>> Usage: classConsistencyCheck(N) <<*>> //
  617. /////////////////////////////////////////////////
  618. void classConsistencyCheck(int, bool); // checks to see that a kernel is created and input defined, as
  619. // well as the specified dimension of the data set matches that of
  620. // the kernel, if not an error is flagged and the program is halted
  621. /*/\/\/\/\/\/\/\/\/\/\/\*/
  622. /* Class Error Handler */
  623. /*\/\/\/\/\/\/\/\/\/\/\/*/
  624. /////////////////////////////////////////////////////
  625. // <<*>> Usage: ErrorHandler( <<*>> //
  626. // className, functName, errMessage) //
  627. /////////////////////////////////////////////////////
  628. void ErrorHandler(char*, char*, char*); // flags an error and halts the system
  629. //===============================
  630. // *** Protected Data Members ***
  631. //===============================
  632. //##########################################
  633. //######### MEAN SHIFT SYSTEM ##########
  634. //##########################################
  635. msSystem msSys; // used for function timing and system output
  636. //##########################################
  637. //######### INPUT DATA PARAMETERS ##########
  638. //##########################################
  639. int L, N, kp, *P; // length, dimension, subspace number, and subspace dimensions
  640. //##########################################
  641. //######### INPUT DATA STORAGE ##########
  642. //##########################################
  643. ////////Linear Storage (used by lattice and bst)////////
  644. float *data; // memory allocated for data points stored by tree nodes
  645. // when used by the lattice data structure data does not store
  646. // the lattice information; format of data:
  647. // data = <x11, x12, ..., x1N,...,xL1, xL2, ..., xLN>
  648. // in the case of the lattice the i in data(i,j) corresponds
  649. //##########################################
  650. //######## LATTICE DATA STRUCTURE ##########
  651. //##########################################
  652. ////////Lattice Data Structure////////
  653. int height, width; // Height and width of lattice
  654. //##########################################
  655. //######### KERNEL DATA STRUCTURE ##########
  656. //##########################################
  657. float *h; // bandwidth vector
  658. float *offset; // defines bandwidth offset caused by the use of a Gaussian kernel
  659. // (for example)
  660. //##########################################
  661. //######### BASIN OF ATTRACTION ##########
  662. //##########################################
  663. unsigned char *modeTable; // Assigns a marking to each data point specifying whether
  664. // or not it has been assigned a mode. These labels are:
  665. // modeTable[i] = 0 - data point i is not associated with a mode
  666. // modeTable[i] = 1 - data point i is associated with a mode
  667. // modeTable[i] = 2 - data point i is associated with a mode
  668. // however its mode is yet to be determined
  669. int *pointList; // a list of data points that due to basin of attraction will
  670. // converge to the same mode as the mode that mean shift is
  671. // currently being applied to
  672. int pointCount; // the number of points stored by the point list
  673. //##########################################
  674. //######### WEIGHT MAP USED ##########
  675. //######### WHEN COMPUTING MEAN ##########
  676. //######### SHIFT ON A LATTICE ##########
  677. //##########################################
  678. float *weightMap; // weight map that may be used to weight the kernel
  679. // upon performing mean shift on a lattice
  680. bool weightMapDefined; // used to indicate if a lattice weight map has been
  681. // defined
  682. //##########################################
  683. //####### CLASS STATE ########
  684. //##########################################
  685. ClassStateStruct class_state; //specifies the state of the class(i.e if data has been loaded into
  686. //the class, if a kernel has been defined, etc.)
  687. private:
  688. //========================
  689. // *** Private Methods ***
  690. //========================
  691. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  692. /* Kernel Creation/Manipulation */
  693. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  694. void generateLookupTable ( void ); // Generates Weight Function Lookup Table
  695. void DestroyKernel ( void ); // Destroys mean shift kernel, re-initializes kernel
  696. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  697. /* Input Data Initialization/Destruction */
  698. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  699. void CreateBST ( void ); // Upload input into a kd-BST
  700. void InitializeInput (float*); // Allocates memory for and initializes the input data structure
  701. void ResetInput ( void ); // de-allocate memory for and re-initialize input data structure
  702. // and mode structure
  703. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  704. /* k-dimensional Binary Search Tree */
  705. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  706. ////////Data Search Tree/////////
  707. tree *BuildKDTree (tree*, int, int, tree* ); // Builds a kd tree given a subset of points initialized
  708. // at depth 0 (dimension 0) (for Tree Structure)
  709. void QuickMedian (tree*, int, int, int ); // Finds the median tree in a forest of trees using
  710. // dimension d, placing the median tree in the array of tree
  711. // nodes at L/2, in which all trees to the left of the median tree
  712. // have values less than that of the median tree in dimension d
  713. // and all trees having values greater than that of the median tree
  714. // in dimension d are placd to the right of this tree -
  715. // This algorithm is used by BuildKDTree to construct a balanced tree
  716. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  717. /* Mean Shift: Using kd-Tree */
  718. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  719. void uniformSearch (tree*, int, double*, double*); // uses kdbst to perform range search on input data,
  720. // computing the weighted sum of these points using
  721. // a uniform kernel and storing the result into Mh
  722. // (called by uniformMSVector)
  723. void generalSearch (tree*, int, double*, double*); // uses kdbst to perform range search on input data,
  724. // computing the weighted sum of these points using
  725. // a general kernel and storing the result into Mh
  726. // (called by generalMSVector)
  727. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  728. /* Mean Shift: Using Lattice */
  729. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
  730. void uniformLSearch (double *, double *); // given a center location and mean shift vector, a lattice
  731. // search is performed to compute the mean shift vector
  732. // using a uniform kernel
  733. void optUniformLSearch(double *, double *); // given a center location and mean shift vector, a lattice
  734. // search is performed to compute the mean shift vector
  735. // using a uniform kernel and the basin of attraction
  736. // optimization for better performance
  737. void generalLSearch (double *, double *); // given a center location and mean shift vector, a lattice
  738. // search is performed to compute the mean shift vector
  739. // using a general kernel
  740. void optGeneralLSearch(double *, double *); // given a center location and mean shift vector, a lattice
  741. // search is performed to compute the mean shift vector
  742. // using a general kernel and the basin of attraction
  743. // optimization for better performance
  744. //=============================
  745. // *** Private Data Members ***
  746. //=============================
  747. //##########################################
  748. //######### KERNEL DATA STRUCTURE ##########
  749. //##########################################
  750. kernelType *kernel; // kernel types for each subspace S[i]
  751. double **w; // weight function lookup table
  752. double *increment; // increment used by weight hashing function
  753. bool uniformKernel; // flag used to indicate if the kernel is uniform or not
  754. userWeightFunct *head, *cur; // user defined weight function linked list
  755. //##########################################
  756. //######### INPUT DATA STORAGE ##########
  757. //##########################################
  758. ////////Range Searching on General Input Data Set////////
  759. tree *root; // root of kdBST used to store input
  760. tree *forest; // memory allocated for tree nodes
  761. float *range; // range vector used to perform range search on kd tree, indexed
  762. // by dimension of input - format:
  763. // range = {Lower_Limit_1, Upper_Limit_1, ..., Lower_Limit_N, Upper_Limit_N}
  764. //##########################################
  765. //######### MEAN SHIFT PROCESSING ##########
  766. //######### DATA STRUCTURES ##########
  767. //##########################################
  768. double *uv; // stores normalized distance vector between
  769. // yk and xi
  770. double wsum; // sum of weights calculated at data points within the sphere
  771. //##########################################
  772. //######### LATTICE DATA STRUCTURE #########
  773. //##########################################
  774. ////////Lattice Data Structure////////
  775. int LowerBoundX, UpperBoundX; // Upper and lower bounds for lattice search window
  776. // in the x dimension
  777. int LowerBoundY, UpperBoundY; // Upper and lower bounds for lattice search window
  778. // in the y dimension
  779. };
  780. #endif