-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathms.cpp
More file actions
2465 lines (2050 loc) · 74.7 KB
/
Copy pathms.cpp
File metadata and controls
2465 lines (2050 loc) · 74.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************
Mean Shift Analysis Library
=============================================
The mean shift library is a collection of routines
that use the mean shift algorithm. Using this algorithm,
the necessary output will be generated needed
to analyze a given input set of data.
MeanShift Base Class:
====================
The mean shift library of routines is realized
via the creation of a MeanShift base class. This class
provides a mechanism for calculating the mean shift vector
at a specified data point, using an arbitrary N-dimensional
data set, and a user-defined kernel.
For image processing the mean shift base class also allows
for the definition of a data set that is on a two-dimensional
lattice. The amount of time needed to compute the mean shift
vector using such a data set is much less than that of an
arbitrary one. Because images usually contain many data points,
defining the image input data points as being on a lattice
greatly improves computation time and makes algorithms such
as image filtering practical.
The definition of the MeanShift class is provided below. Its
prototype is provided in 'ms.h'.
The theory is described in the papers:
D. Comaniciu, P. Meer: Mean Shift: A robust approach toward feature
space analysis.
C. Christoudias, B. Georgescu, P. Meer: Synergism in low level vision.
and they are is available at:
http://www.caip.rutgers.edu/riul/research/papers/
Implemented by Chris M. Christoudias, Bogdan Georgescu
********************************************************/
//Include Needed Libraries
#include "ms.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ PUBLIC METHODS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*** Constructor/Destructor ***/
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Class Constructor */
/*******************************************************/
/*Post: */
/* The MeanShift class has been properly */
/* initialized. */
/*******************************************************/
MeanShift::MeanShift( void )
{
//intialize input data set parameters...
P = NULL;
L = 0;
N = 0;
kp = 0;
//initialize input data set storage structures...
data = NULL;
//initialize input data set kd-tree
root = NULL;
forest = NULL;
range = NULL;
//intialize lattice structure...
height = 0;
width = 0;
//intialize kernel strucuture...
h = NULL;
kernel = NULL;
w = NULL;
offset = NULL;
increment = NULL;
uniformKernel = false;
//initialize weight function linked list...
head = cur = NULL;
//intialize mean shift processing data structures...
uv = NULL;
//set lattice weight map to null
weightMap = NULL;
//indicate that the lattice weight map is undefined
weightMapDefined = false;
//allocate memory for error message buffer...
ErrorMessage = new char [256];
//initialize error status to OKAY
ErrorStatus = EL_OKAY;
//Initialize class state...
class_state.INPUT_DEFINED = false;
class_state.KERNEL_DEFINED = false;
class_state.LATTICE_DEFINED = false;
class_state.OUTPUT_DEFINED = false;
}
/*******************************************************/
/*Class Destructor */
/*******************************************************/
/*Post: */
/* The MeanShift class has been properly */
/* destroyed. */
/*******************************************************/
MeanShift::~MeanShift( void )
{
delete [] ErrorMessage;
if (weightMap)
{
delete [] weightMap;
}
//de-allocate memory used to store
//user defined weight functions
ClearWeightFunctions();
//de-allocate memory used for kernel
DestroyKernel();
//de-allocate memory used for input
ResetInput();
}
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*** Creation/Initialization of Mean Shift Kernel ***/
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Define Kernel */
/*******************************************************/
/*Creats custom user defined Kernel to be used by the */
/*mean shift procedure. */
/*******************************************************/
/*Pre: */
/* - kernel is an array of kernelTypes specifying */
/* the type of kernel to be used on each sub- */
/* space of the input data set x */
/* - h is the set of bandwidths used to define the*/
/* the search window */
/* - P is a one dimensional array of integers of */
/* size kp, that specifies the dimension of each*/
/* subspace of the input data set x */
/* - kp is the total number of subspaces used to */
/* the input data set x */
/*Post: */
/* - the custom kernel has been created for use */
/* by the mean shift procedure. */
/*******************************************************/
void MeanShift::DefineKernel(kernelType *kernel_, float *h_, int *P_, int kp_)
{
// Declare variables
int i, kN;
//if a kernel has already been created then destroy it
if(kp)
DestroyKernel();
//Obtain kp...
if((kp = kp_) <= 0)
{
ErrorHandler("MeanShift", "CreateKernel", "Subspace count (kp) is zero or negative.");
return;
}
//Allocate memory for h, P, kernel, offset, and increment
if((!(P = new int [kp]))||(!(h = new float [kp]))||(!(kernel = new kernelType [kp]))||
(!(offset = new float [kp]))||(!(increment = new double [kp])))
{
ErrorHandler("MeanShift", "CreateKernel", "Not enough memory available to create kernel.");
return;
}
//Populate h, P and kernel, also use P to calculate
//the dimension (N_) of the potential input data set x
kN = 0;
for(i = 0; i < kp; i++)
{
if((h[i] = h_[i]) <= 0)
{
ErrorHandler("MeanShift", "CreateKernel", "Negative or zero valued bandwidths are prohibited.");
return;
}
if((P[i] = P_[i]) <= 0)
{
ErrorHandler("MeanShift", "CreateKernel", "Negative or zero valued subspace dimensions are prohibited.");
return;
}
kernel[i] = kernel_[i];
kN += P[i];
}
//Allocate memory for range vector and uv using N_
if((!(range = new float [2*kN]))||(!(uv = new double [kN])))
{
ErrorHandler("MeanShift", "CreateKernel", "Not enough memory available to create kernel.");
return;
}
// Generate weight function lookup table
// using above information and user
// defined weight function list
generateLookupTable();
//check for errors
if(ErrorStatus == EL_ERROR)
return;
//indicate that the kernel has been defined
class_state.KERNEL_DEFINED = true;
//done.
return;
}
/*******************************************************/
/*Add Weight Function */
/*******************************************************/
/*Adds a weight function to the Mean Shift class to be */
/*used by the mean shift procedure */
/*******************************************************/
/*Pre: */
/* - g(u) is the normalized weight function with */
/* respect to u = (norm(x-xi))^2/h^2 */
/* - sampleNumber is the number of samples to be */
/* taken of g(u) over halfWindow interval */
/* - halfWindow is the radius of g(u) such that */
/* g(u) is defined for 0 <= u <= halfWindow */
/* - subspace is the subspace number for which */
/* g(u) is to be applied during the mean shift */
/* procedure. */
/*Post: */
/* - g(u) has been added to the Mean Shift class */
/* private data structure to be used by the */
/* mean shift procedure. */
/* - if a weight function has already been spec- */
/* ified for the specified subspace, the weight */
/* function for this subspace has been replaced.*/
/*******************************************************/
void MeanShift::AddWeightFunction(double g(double), float halfWindow, int sampleNumber, int subspace)
{
// Declare Variables
int i;
double increment;
// Search to see if a weight function has already been
// defined for specified subspace, if not then insert
// into the head of the weight function list, otherwise
// replace entry
// Perform Search
cur = head;
while((cur)&&(cur->subspace != subspace))
cur = cur->next;
// Entry Exists - Replace It!
// Otherwise insert at the head of the the weight functon list
if(cur)
delete cur->w;
else
{
cur = new userWeightFunct;
cur->next = head;
head = cur;
}
// Generate lookup table
increment = halfWindow/(double)(sampleNumber);
cur->w = new double [sampleNumber+1];
for(i = 0; i <= sampleNumber; i++)
cur->w[i] = g((double)(i*increment));
// Set weight function parameters
cur->halfWindow = halfWindow;
cur->sampleNumber = sampleNumber;
cur->subspace = subspace;
//done.
return;
}
/*******************************************************/
/*Clear Weight Functions */
/*******************************************************/
/*Clears user defined weight from the Mean Shift class */
/*private data structure. */
/*******************************************************/
/*Post: */
/* - all user defined weight functions ahve been */
/* cleared from the private data structure of */
/* the mean shift class. */
/*******************************************************/
void MeanShift::ClearWeightFunctions( void )
{
while(head)
{
delete head->w;
cur = head;
head = head->next;
delete cur;
}
}
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*** Input Data Set Declaration ***/
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Define Input */
/*******************************************************/
/*Uploads input data set x into the mean shift class. */
/*******************************************************/
/*Pre: */
/* - x is a one dimensional array of L N-dimen- */
/* ional data points. */
/*Post: */
/* - x has been uploaded into the mean shift */
/* class. */
/* - the height and width of a previous data set */
/* has been undefined. */
/*******************************************************/
void MeanShift::DefineInput(float *x, int L_, int N_)
{
//if input data is defined de-allocate memory, and
//re-initialize the input data structure
if((class_state.INPUT_DEFINED)||(class_state.LATTICE_DEFINED))
ResetInput();
//make sure x is not NULL...
if(!x)
{
ErrorHandler("MeanShift", "UploadInput", "Input data set is NULL.");
return;
}
//Obtain L and N
if(((L = L_) <= 0)||((N = N_) <= 0))
{
ErrorHandler("MeanShift", "UploadInput", "Input data set has negative or zero length or dimension.");
return;
}
//Allocate memory for data
if(!(data = new float [L*N]))
{
ErrorHandler("MeanShift", "UploadInput", "Not enough memory.");
return;
}
//Allocate memory for input data set, and copy
//x into the private data members of the mean
//shift class
InitializeInput(x);
//check for errors
if(ErrorStatus == EL_ERROR)
return;
// Load x into the MeanShift object using
// using a kd-tree, resulting in better
// range searching of the input data points
// x - also upload window centers into
// msRawData
CreateBST();
//indicate that the input has been recently defined
class_state.INPUT_DEFINED = true;
class_state.LATTICE_DEFINED = false;
class_state.OUTPUT_DEFINED = false;
//done.
return;
}
/*******************************************************/
/*Define Lattice */
/*******************************************************/
/*Defines the height and width of the input lattice. */
/*******************************************************/
/*Pre: */
/* - ht is the height of the lattice */
/* - wt is the width of the lattice */
/*Post: */
/* - the height and width of the lattice has been */
/* specified. */
/* - if a data set is presently loaded into the */
/* mean shift class, an error is flagged if the */
/* number of elements in that data set does not */
/* equal the product ht*wt. */
/*******************************************************/
void MeanShift::DefineLInput(float *x, int ht, int wt, int N_)
{
//if input data is defined de-allocate memory, and
//re-initialize the input data structure
if((class_state.INPUT_DEFINED)||(class_state.LATTICE_DEFINED))
ResetInput();
//Obtain lattice height and width
if(((height = ht) <= 0)||((width = wt) <= 0))
{
ErrorHandler("MeanShift", "DefineLInput", "Lattice defined using zero or negative height and/or width.");
return;
}
//Obtain input data dimension
if((N = N_) <= 0)
{
ErrorHandler("MeanShift", "DefineInput", "Input defined using zero or negative dimension.");
return;
}
//compute the data length, L, of input data set
//using height and width
L = height*width;
//Allocate memory for input data set, and copy
//x into the private data members of the mean
//shift class
InitializeInput(x);
//check for errors
if(ErrorStatus == EL_ERROR)
return;
//allocate memory for weight map
if(!(weightMap = new float [L]))
{
ErrorHandler("MeanShift", "InitializeInput", "Not enough memory.");
return;
}
//initialize weightMap to an array of zeros
memset(weightMap, 0, L*(sizeof(float)));
//Indicate that a lattice input has recently been
//defined
class_state.LATTICE_DEFINED = true;
class_state.INPUT_DEFINED = false;
class_state.OUTPUT_DEFINED = false;
//done.
return;
}
/*******************************************************/
/*Set Lattice Weight Map */
/*******************************************************/
/*Populates the lattice weight map with specified */
/*weight values. */
/*******************************************************/
/*Pre: */
/* - wm is a floating point array of size L */
/* specifying for each data point a weight */
/* value */
/*Post: */
/* - wm has been used to populate the lattice */
/* weight map. */
/*******************************************************/
void MeanShift::SetLatticeWeightMap(float *wm)
{
//make sure wm is not NULL
if(!wm)
{
ErrorHandler("MeanShift", "SetWeightMap", "Specified weight map is NULL.");
return;
}
//populate weightMap using wm
int i;
for(i = 0; i < L; i++)
weightMap[i] = wm[i];
//indicate that a lattice weight map has been specified
weightMapDefined = true;
//done.
return;
}
/*******************************************************/
/*Remove Lattice Weight Map */
/*******************************************************/
/*Removes the lattice weight map. */
/*******************************************************/
/*Post: */
/* - the lattice weight map has been removed. */
/* - if a weight map did not exist NO error is */
/* flagged. */
/*******************************************************/
void MeanShift::RemoveLatticeWeightMap(void)
{
//only remove weight map if it exists, otherwise
//do nothing...
if(weightMapDefined)
{
//set values of lattice weight map to zero
memset(weightMap, 0, L*sizeof(float));
//indicate that a lattice weight map is no longer
//defined
weightMapDefined = false;
}
//done.
return;
}
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*** Mean Shift Operations ***/
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Mean Shift Vector */
/*******************************************************/
/*Calculates the mean shift vector at a specified data */
/*point yk. */
/*******************************************************/
/*Pre: */
/* - a kernel has been created */
/* - a data set has been uploaded */
/* - Mh is an N dimensional mean shift vector */
/* - yk is an N dimensional data point */
/*Post: */
/* - the mean shift vector at yk has been */
/* calculated and stored in and returned by Mh. */
/*******************************************************/
void MeanShift::msVector(double *Mh, double *yk)
{
//make sure that Mh and/or yk are not NULL...
if((!Mh)||(!yk))
{
ErrorHandler("MeanShift", "msVector", "Invalid argument(s) passed to this method.");
return;
}
//make sure that a kernel has been created, data has
//been uploaded, and that they are consistent with one
//another...
classConsistencyCheck(N, false);
//calculate mean shift vector at yk using created kernel
//and uploaded data set
MSVector(Mh, yk);
//done.
return;
}
/*******************************************************/
/*Lattice Mean Shift Vector */
/*******************************************************/
/*Calculates the mean shift vector at a specified data */
/*point yk, assuming that the data set exhists on a */
/*height x width two dimensional lattice. */
/*******************************************************/
/*Pre: */
/* - a kernel has been created */
/* - a data set has been uploaded */
/* - the height and width of the lattice has been */
/* specified using method DefineLattice() */
/* - Mh is an N dimensional mean shift vector */
/* - yk is an N dimensional data point */
/*Post: */
/* - the mean shift vector at yk has been */
/* calculated and stored in and returned by Mh. */
/* - Mh was calculated using the defined input */
/* lattice. */
/*******************************************************/
void MeanShift::latticeMSVector(double *Mh, double *yk)
{
//make sure that Mh and/or yk are not NULL...
if((!Mh)||(!yk))
{
ErrorHandler("MeanShift", "lmsVector", "Invalid argument(s) passed to this method.");
return;
}
//make sure that a kernel has been created, data has
//been uploaded, and that they are consistent with one
//another...
classConsistencyCheck(N+2, true);
//calculate mean shift vector at yk using created kernel
//and uploaded data set
LatticeMSVector(Mh, yk);
//done.
return;
}
/*******************************************************/
/*Find Mode */
/*******************************************************/
/*Calculates the mode of a specified data point yk. */
/*******************************************************/
/*Pre: */
/* - a kernel has been created */
/* - a data set has been uploaded */
/* - mode is the N dimensional mode of the N-dim- */
/* ensional data point yk */
/*Post: */
/* - the mode of yk has been calculated and */
/* stored in mode. */
/*******************************************************/
void MeanShift::FindMode(double *mode, double *yk)
{
//make sure that mode and/or yk are not NULL...
if((!mode)||(!yk))
{
ErrorHandler("MeanShift", "FindMode", "Invalid argument(s) passed to this method.");
return;
}
//make sure that a kernel has been created, data has
//been uploaded, and that they are consistent with one
//another...
classConsistencyCheck(N, false);
//allocate memory for Mh
double *Mh = new double [N];
//copy yk into mode
int i;
for(i = 0; i < N; i++)
mode[i] = yk[i];
//calculate mean shift vector at yk
MSVector(Mh, yk);
//calculate mvAbs = |Mh|^2
double mvAbs = 0;
for(i = 0; i < N; i++)
mvAbs += Mh[i]*Mh[i];
//shift mode until convergence (mvAbs = 0)...
int iterationCount = 1;
while((mvAbs >= EPSILON)&&(iterationCount < LIMIT))
{
//shift mode...
for(i = 0; i < N; i++)
mode[i] += Mh[i];
//re-calculate mean shift vector at new
//window location have center defined by
//mode
MSVector(Mh, mode);
//calculate mvAbs = |Mh|^2
mvAbs = 0;
for(i = 0; i < N; i++)
mvAbs += Mh[i]*Mh[i];
//increment interation count...
iterationCount++;
}
//shift mode...
for(i = 0; i < N; i++)
mode[i] += Mh[i];
//de-allocate memory
delete [] Mh;
//done.
return;
}
/*******************************************************/
/*Find Lattice Mode */
/*******************************************************/
/*Calculates the mode of a specified data point yk, */
/*assuming that the data set exhists on a height x */
/*width two dimensional lattice. */
/*******************************************************/
/*Pre: */
/* - a kernel has been created */
/* - a data set has been uploaded */
/* - the height and width of the lattice has been */
/* specified using method DefineLattice() */
/* - mode is the N dimensional mode of the N-dim- */
/* ensional data point yk */
/*Post: */
/* - the mode of yk has been calculated and */
/* stored in mode. */
/* - mode was calculated using the defined input */
/* lattice. */
/*******************************************************/
void MeanShift::FindLMode(double *mode, double *yk)
{
//make sure that mode and/or yk are not NULL...
if((!mode)||(!yk))
{
ErrorHandler("MeanShift", "FindLMode", "Invalid argument(s) passed to this method.");
return;
}
//make sure the lattice height and width have been defined...
if(!height)
{
ErrorHandler("MeanShift", "FindLMode", "Lattice height and width is undefined.");
return;
}
//make sure that a kernel has been created, data has
//been uploaded, and that they are consistent with one
//another...
classConsistencyCheck(N+2, true);
//define gridN
int gridN = N+2;
//allocate memory for Mh
double *Mh = new double [gridN];
//copy yk into mode
int i;
for(i = 0; i < gridN; i++)
mode[i] = yk[i];
//calculate mean shift vector at yk
LatticeMSVector(Mh, mode);
//calculate mvAbs = |Mh|^2
double mvAbs = 0;
for(i = 0; i < gridN; i++)
mvAbs += Mh[i]*Mh[i];
//shift mode until convergence (mvAbs = 0)...
int iterationCount = 1;
while((mvAbs >= EPSILON)&&(iterationCount < LIMIT))
{
//shift mode...
for(i = 0; i < gridN; i++)
mode[i] += Mh[i];
//re-calculate mean shift vector at new
//window location have center defined by
//mode
LatticeMSVector(Mh, mode);
//calculate mvAbs = |Mh|^2
mvAbs = 0;
for(i = 0; i < gridN; i++)
mvAbs += Mh[i]*Mh[i];
//increment interation count...
iterationCount++;
}
//shift mode...
for(i = 0; i < gridN; i++)
mode[i] += Mh[i];
//de-allocate memory
delete [] Mh;
//done.
return;
}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ PROTECTED METHODS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/* Mean Shift: Using kd-Tree */
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Mean Shift Vector */
/*******************************************************/
/*Computes the mean shift vector at a window location */
/*yk using input data set x using a custom, user defin-*/
/*ed kernel. */
/*******************************************************/
/*Pre: */
/* - input data has been uploaded into the private*/
/* data members of the MeanShift class */
/* - a window center yk has been defined */
/* - uniformKernel indicates the which type of */
/* kernel to be used by this procedure: uniform */
/* or general */
/*Post: */
/* - the mean shift vector calculated at yk */
/* using a either a custom, user defined kernel */
/* or a uniform kernel is returned */
/*******************************************************/
void MeanShift::MSVector(double *Mh_ptr, double *yk_ptr)
{
// Declare Variables
int i,j;
// Initialize mean shift vector
for(i = 0; i < N; i++)
Mh_ptr[i] = 0;
// Initialize wsum to zero, the sum of the weights of each
// data point found to lie within the search window (sphere)
wsum = 0;
// Build Range Vector using h[i] and yk
int s = 0;
// The flag uniformKernel is used to determine which
// kernel function is to be used in the calculation
// of the mean shift vector
if(uniformKernel)
{
for(i = 0; i < kp; i++)
{
for(j = 0; j < P[i]; j++)
{
range[2*(s+j) ] = (float)(yk_ptr[s+j] - h[i]);
range[2*(s+j)+1] = (float)(yk_ptr[s+j] + h[i]);
}
s += P[i];
}
}
else
{
for(i = 0; i < kp; i++)
{
for(j = 0; j < P[i]; j++)
{
range[2*(s+j) ] = (float)(yk_ptr[s+j] - h[i]*float(sqrt(offset[i])));
range[2*(s+j)+1] = (float)(yk_ptr[s+j] + h[i]*float(sqrt(offset[i])));
}
s += P[i];
}
}
// Traverse through the data set x, performing the
// weighted sum of each point xi that lies within
// the search window (sphere) using a general,
// user defined kernel or uniform kernel depending
// on the uniformKernel flag
if(uniformKernel)
uniformSearch(root, 0, Mh_ptr, yk_ptr);
else
generalSearch(root, 0, Mh_ptr, yk_ptr);
// Calculate the mean shift vector using Mh and wsum
for(i = 0; i < N; i++)
{
// Divide Sum by wsum
Mh_ptr[i] /= wsum;
// Calculate mean shift vector: Mh(yk) = y(k+1) - y(k)
Mh_ptr[i] -= yk_ptr[i];
}
//done.
return;
}
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/* Mean Shift: Using Lattice */
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Lattice Mean Shift Vector */
/*******************************************************/
/*Computes the mean shift vector at a specfied window */
/*yk using the lattice data structure. */
/*******************************************************/
/*Pre: */
/* - Mh_ptr and yh_ptr are arrays of doubles con- */
/* aining N+2 elements */
/* - Mh_ptr is the mean shift vector calculated */
/* at window center yk_ptr */
/*Post: */
/* - the mean shift vector at the window center */
/* pointed to by yk_ptr has been calculated and */
/* stored in the memory location pointed to by */
/* Mh_ptr */
/*******************************************************/
void MeanShift::LatticeMSVector(double *Mh_ptr, double *yk_ptr)
{
// Initialize mean shift vector
register int i;
for(i = 0; i < N+2; i++)
Mh_ptr[i] = 0;
// Initialize wsum
wsum = 0;
// Perform lattice search summing
// all the points that lie within the search
// window defined using the kernel specified
//by uniformKernel
if(uniformKernel)
uniformLSearch(Mh_ptr, yk_ptr);
else
generalLSearch(Mh_ptr, yk_ptr);
// Compute mean shift vector using sum computed
// by lattice search, wsum, and yk_ptr:
// Mh = Mh/wsum - yk_ptr
if (wsum > 0)
{
for(i = 0; i < N+2; i++)
Mh_ptr[i] = Mh_ptr[i]/wsum - yk_ptr[i];
}
else
{
for(i = 0; i < N+2; i++)
Mh_ptr[i] = 0;
}
// done.
return;
}
/*******************************************************/
/*Optimized Lattice Mean Shift Vector */
/*******************************************************/
/*Computes the mean shift vector at a specfied window */
/*yk using the lattice data structure. Also the points */
/*that lie within the window are stored into the basin */
/*of attraction structure used by the optimized mean */