-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmsImageProcessor.cpp
More file actions
4711 lines (3939 loc) · 138 KB
/
Copy pathmsImageProcessor.cpp
File metadata and controls
4711 lines (3939 loc) · 138 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.
Mean Shift Image Processor Class:
================================
The following class inherits from the mean shift library
in order to perform the specialized tasks of image
segmentation and filtering.
The definition of the Mean Shift Image Processor Class
is provided below. Its prototype is provided in
'msImageProcessor.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
********************************************************/
// Uncomment this definition to restore file to original state.
// I commented it to get rid of compilation/linking errors. -- STB 7/16/05
// #define USE_MSSYS_PROGRESS 1
//include image processor class prototype
#include "msImageProcessor.h"
//include needed libraries
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ PUBLIC METHODS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*/\/\/\/\/\/\/\/\/\/\/\/\*/
/* Constructor/Destructor */
/*\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Class Constructor */
/*******************************************************/
/*Post: */
/* The msImageProcessor class has been properly */
/* initialized. */
/*******************************************************/
msImageProcessor::msImageProcessor( void )
{
//intialize basin of attraction structure
//used by the filtering algorithm
modeTable = NULL;
pointList = NULL;
pointCount = 0;
//initialize region list
regionList = NULL;
//initialize output structures...
msRawData = NULL;
labels = NULL;
modes = NULL;
modePointCounts = NULL;
regionCount = 0;
//intialize temporary buffers used for
//performing connected components
indexTable = NULL;
LUV_data = NULL;
//initialize region adjacency matrix
raList = NULL;
freeRAList = NULL;
raPool = NULL;
//intialize visit table to having NULL entries
visitTable = NULL;
//initialize epsilon such that transitive closure
//does not take edge strength into consideration when
//fusing regions of similar color
epsilon = 1.0;
//initialize class state to indicate that
//an output data structure has not yet been
//created...
class_state.OUTPUT_DEFINED = false;
LUV_treshold = 1.0;
}
/*******************************************************/
/*Class Destructor */
/*******************************************************/
/*Post: */
/* The msImageProcessor class has been properly */
/* destroyed. */
/*******************************************************/
msImageProcessor::~msImageProcessor( void )
{
//de-allocate memory
if(class_state.OUTPUT_DEFINED) DestroyOutput();
if(regionList) delete regionList;
regionList = NULL;
//done.
}
/*/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/* Input Image Declaration */
/*\/\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Define Image */
/*******************************************************/
/*Uploads an image into the image segmenter class to */
/*be segmented. */
/*******************************************************/
/*Pre: */
/* - data_ is a one dimensional array of unsigned */
/* char RGB vectors */
/* - type is the type of the image: COLOR or */
/* GREYSCALE */
/* - height_ and width_ define the dimension of */
/* the image */
/* - if the image is of type GREYSCALE then */
/* data containes only one number per pixel */
/* location, where a pixel location is defined */
/* by the index into the data array */
/*Post: */
/* - the image specified has been uploaded into */
/* the image segmenter class to be segmented. */
/*******************************************************/
void msImageProcessor::DefineImage(byte *data_, imageType type, int height_, int width_)
{
//obtain image dimension from image type
int dim;
if(type == COLOR)
dim = 3;
else
dim = 1;
//perfor rgb to luv conversion
int i;
float *luv = new float [height_*width_*dim];
if(dim == 1)
{
for(i = 0; i < height_*width_; i++)
luv[i] = (float)(data_[i]);
}
else
{
for(i = 0; i < height_*width_; i++)
RGBtoLUV(&data_[dim*i], &luv[dim*i]);
}
//define input defined on a lattice using mean shift base class
DefineLInput(luv, height_, width_, dim);
//Define a default kernel if it has not been already
//defined by user
if(!h)
{
//define default kernel paramerters...
kernelType k[2] = {Uniform, Uniform};
int P[2] = {2, N};
float tempH[2] = {1.0 , 1.0};
//define default kernel in mean shift base class
DefineKernel(k, tempH, P, 2);
}
//de-allocate memory
delete [] luv;
//done.
return;
}
void msImageProcessor::DefineBgImage(byte* data_, imageType type, int height_, int width_)
{
//obtain image dimension from image type
int dim;
if(type == COLOR)
dim = 3;
else
dim = 1;
//perform texton classification
int i;
float *luv = new float [height_*width_*dim];
if(dim == 1)
{
for(i = 0; i < height_*width_; i++)
luv[i] = (float)(data_[i]);
}
else
{
for(i = 0; i < height_*width_; i++)
RGBtoLUV(&data_[dim*i], &luv[dim*i]);
}
//define input defined on a lattice using mean shift base class
DefineLInput(luv, height_, width_, dim);
//Define a default kernel if it has not been already
//defined by user
if(!h)
{
//define default kernel paramerters...
kernelType k[2] = {Uniform, Uniform};
int P[2] = {2, N};
float tempH[2] = {1.0 , 1.0};
//define default kernel in mean shift base class
DefineKernel(k, tempH, P, 2);
}
//de-allocate memory
delete [] luv;
//done.
return;
}
/*/\/\/\/\/\/\/\/\*/
/* Weight Map */
/*\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Set Weight Map */
/*******************************************************/
/*Populates the weight map with specified edge */
/*strengths. */
/*******************************************************/
/*Pre: */
/* - wm is a floating point array of size */
/* (height x width) specifying for each pixel */
/* edge strength. */
/* - eps is a threshold used to fuse similar */
/* regions during transitive closure. */
/*Post: */
/* - wm has been used to populate the weight */
/* map. */
/* - the threshold used during transitive closure */
/* is taken as eps. */
/*******************************************************/
void msImageProcessor::SetWeightMap(float *wm, float eps)
{
//initlaize confmap using wm
SetLatticeWeightMap(wm);
//set threshold value
if((epsilon = eps) < 0)
ErrorHandler("msImageProcessor", "SetWeightMap", "Threshold is negative.");
//done.
return;
}
/*******************************************************/
/*Remove Weight Map */
/*******************************************************/
/*Removes the weight map. */
/*******************************************************/
/*Post: */
/* - the weight map has been removed. */
/* - if a weight map did not exist NO error */
/* is flagged. */
/*******************************************************/
void msImageProcessor::RemoveWeightMap( void )
{
//remove confmap
RemoveLatticeWeightMap();
//set threshold value to zero
epsilon = 0;
//done.
return;
}
/*/\/\/\/\/\/\/\/\/\*/
/* Image Filtering */
/*\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Filter */
/*******************************************************/
/*Performs mean shift filtering on the specified input */
/*image using a user defined kernel. */
/*******************************************************/
/*Pre: */
/* - the user defined kernel used to apply mean */
/* shift filtering to the defined input image */
/* has spatial bandwidth sigmaS and range band- */
/* width sigmaR */
/* - speedUpLevel determines whether or not the */
/* filtering should be optimized for faster */
/* execution: a value of NO_SPEEDUP turns this */
/* optimization off and a value SPEEDUP turns */
/* this optimization on */
/* - a data set has been defined */
/* - the height and width of the lattice has been */
/* specified using method DefineLattice() */
/*Post: */
/* - mean shift filtering has been applied to the */
/* input image using a user defined kernel */
/* - the filtered image is stored in the private */
/* data members of the msImageProcessor class. */
/*******************************************************/
void msImageProcessor::Filter(int sigmaS, float sigmaR, SpeedUpLevel speedUpLevel)
{
//Check Class consistency...
//check:
// (1) if this operation is consistent
// (2) if kernel was created
// (3) if data set is defined
// (4) if the dimension of the kernel agrees with that
// of the defined data set
// if not ... flag an error!
classConsistencyCheck(N+2, true);
if(ErrorStatus == EL_ERROR)
return;
#ifdef USE_MSSYS_PROGRESS
//If the algorithm has been halted, then exit
if((ErrorStatus = msSys.Progress((float)(0.0))) == EL_HALT)
{
return;
}
#endif
//If the image has just been read then allocate memory
//for and initialize output data structure used to store
//image modes and their corresponding regions...
if(class_state.OUTPUT_DEFINED == false)
{
InitializeOutput();
//check for errors...
if(ErrorStatus == EL_ERROR)
return;
}
//****************** Allocate Memory ******************
//Allocate memory for basin of attraction mode structure...
if((!(modeTable = new unsigned char [L]))||(!(pointList = new int [L])))
{
ErrorHandler("msImageProcessor", "Allocate", "Not enough memory.");
return;
}
//start timer
#ifdef PROMPT
double timer;
msSys.StartTimer();
#endif
//*****************************************************
//filter image according to speedup level...
switch(speedUpLevel)
{
//no speedup...
case NO_SPEEDUP:
//NonOptimizedFilter((float)(sigmaS), sigmaR); break;
NewNonOptimizedFilter((float)(sigmaS), sigmaR); break;
//medium speedup
case MED_SPEEDUP:
//OptimizedFilter1((float)(sigmaS), sigmaR); break;
NewOptimizedFilter1((float)(sigmaS), sigmaR); break;
//high speedup
case HIGH_SPEEDUP:
//OptimizedFilter2((float)(sigmaS), sigmaR); break;
NewOptimizedFilter2((float)(sigmaS), sigmaR); break;
// new speedup
}
//****************** Deallocate Memory ******************
//de-allocate memory used by basin of attraction mode structure
delete [] modeTable;
delete [] pointList;
//re-initialize structure
modeTable = NULL;
pointList = NULL;
pointCount = 0;
//*******************************************************
//If the algorithm has been halted, then de-allocate the output
//and exit
#ifdef USE_MSSYS_PROGRESS
if((ErrorStatus = msSys.Progress((float)(0.8))) == EL_HALT)
{
DestroyOutput();
return;
}
#endif
//Label image regions, also if segmentation is not to be
//performed use the resulting classification structure to
//calculate the image boundaries...
/*
//copy msRawData into LUV_data, rounding each component of each
//LUV value stored by msRawData to the nearest integer
int i;
for(i = 0; i < L*N; i++)
{
if(msRawData[i] < 0)
LUV_data[i] = (int)(msRawData[i] - 0.5);
else
LUV_data[i] = (int)(msRawData[i] + 0.5);
}
*/
int i;
for (i=0; i<L*N; i++)
{
LUV_data[i] = msRawData[i];
}
#ifdef PROMPT
timer = msSys.ElapsedTime();
msSys.Prompt("(%6.2f sec)\nConnecting regions ...", timer);
msSys.StartTimer();
#endif
//Perform connecting (label image regions) using LUV_data
Connect();
#ifdef PROMPT
timer = msSys.ElapsedTime();
msSys.Prompt("done. (%6.2f seconds, numRegions = %6d)\n", timer, regionCount);
msSys.StartTimer();
#endif
//done.
return;
}
/*/\/\/\/\/\/\/\/\/\/\/\*/
/* Image Region Fusing */
/*\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Fuse Regions */
/*******************************************************/
/*Fuses the regions of a filtered image. */
/*******************************************************/
/*Pre: */
/* - the range radius is specified by sigmaR */
/* - minRegion is the minimum point density that */
/* a region may have in the resulting segment- */
/* ed image */
/* - a data set has been defined */
/* - the height and width of the lattice has been */
/* specified using method DefineLattice() */
/*Post: */
/* - the image regions have been fused. */
/* - if an result is stored by this class then */
/* this result is used as input to this method. */
/* - if no result is stored by this class, */
/* the input image defined by calling the */
/* method DefineImage is used. */
/*******************************************************/
void msImageProcessor::FuseRegions(float sigmaS, int minRegion)
{
//Check Class consistency...
//check:
// (1) if this operation is consistent
// (2) if kernel was created
// (3) if data set is defined
// (4) if the dimension of the kernel agrees with that
// of the defined data set
// if not ... flag an error!
classConsistencyCheck(N+2, true);
if(ErrorStatus == EL_ERROR)
return;
//Check to see if the algorithm is to be halted, if so then
//destroy output and exit
#ifdef USE_MSSYS_PROGRESS
if((ErrorStatus = msSys.Progress((float)(0.8))) == EL_HALT)
{
if(class_state.OUTPUT_DEFINED) DestroyOutput();
return;
}
#endif
//obtain sigmaS (make sure it is not zero or negative, if not
//flag an error)
if((h[1] = sigmaS) <= 0)
{
ErrorHandler("msImageProcessor", "FuseRegions", "The feature radius must be greater than or equal to zero.");
return;
}
//if output has not yet been generated then classify the input
//image regions to be fused...
if(!(class_state.OUTPUT_DEFINED))
{
//Initialize output data structure used to store
//image modes and their corresponding regions...
InitializeOutput();
//check for errors...
if(ErrorStatus == EL_ERROR)
return;
//copy data into LUV_data used to classify
//image regions
/*
int i;
for(i = 0; i < L*N; i++)
{
if(data[i] < 0)
LUV_data[i] = (int)(data[i] - 0.5);
else
LUV_data[i] = (int)(data[i] + 0.5);
}
*/
int i;
for (i=0; i<L*N; i++)
{
LUV_data[i] = data[i];
}
#ifdef PROMPT
msSys.Prompt("Connecting regions ...");
msSys.StartTimer();
#endif
//Perform connecting (label image regions) using LUV_data
Connect();
//check for errors
if(ErrorStatus == EL_ERROR)
return;
#ifdef PROMPT
double timer = msSys.ElapsedTime();
msSys.Prompt("done. (%6.2f seconds, numRegions = %6d)\n", timer, regionCount);
#endif
}
#ifdef USE_MSSYS_PROGRESS
//Check to see if the algorithm is to be halted, if so then
//destroy output and exit
if((ErrorStatus = msSys.Progress((float)(0.85))) == EL_HALT)
{
DestroyOutput();
return;
}
#endif
#ifdef PROMPT
msSys.Prompt("Applying transitive closure...");
msSys.StartTimer();
#endif
//allocate memory visit table
visitTable = new unsigned char [L];
//Apply transitive closure iteratively to the regions classified
//by the RAM updating labels and modes until the color of each neighboring
//region is within sqrt(rR2) of one another.
rR2 = (float)(h[1]*h[1]*0.25);
TransitiveClosure();
int oldRC = regionCount;
int deltaRC, counter = 0;
do {
TransitiveClosure();
deltaRC = oldRC-regionCount;
oldRC = regionCount;
counter++;
} while ((deltaRC <= 0)&&(counter < 10));
//de-allocate memory for visit table
delete [] visitTable;
visitTable = NULL;
#ifdef USE_MSSYS_PROGRESS
//Check to see if the algorithm is to be halted, if so then
//destroy output and region adjacency matrix and exit
if((ErrorStatus = msSys.Progress((float)(1.0))) == EL_HALT)
{
DestroyRAM();
DestroyOutput();
return;
}
#endif
#ifdef PROMPT
double timer = msSys.ElapsedTime();
msSys.Prompt("done. (%6.2f seconds, numRegions = %6d)\nPruning spurious regions ...", timer, regionCount);
msSys.StartTimer();
#endif
//Prune spurious regions (regions whose area is under
//minRegion) using RAM
Prune(minRegion);
#ifdef PROMPT
timer = msSys.ElapsedTime();
msSys.Prompt("done. (%6.2f seconds, numRegions = %6d)\n", timer, regionCount);
msSys.StartTimer();
#endif
#ifdef USE_MSSYS_PROGRESS
//Check to see if the algorithm is to be halted, if so then
//destroy output and region adjacency matrix and exit
if((ErrorStatus = msSys.Progress((float)(1.0))) == EL_HALT)
{
DestroyRAM();
DestroyOutput();
return;
}
#endif
//de-allocate memory for region adjacency matrix
DestroyRAM();
//output to msRawData
int i, j, label;
for(i = 0; i < L; i++)
{
label = labels[i];
for(j = 0; j < N; j++)
{
msRawData[N*i+j] = modes[N*label+j];
}
}
//done.
return;
}
/*/\/\/\/\/\/\/\/\/\/\*/
/* Image Segmentation */
/*\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Segment */
/*******************************************************/
/*Segments the defined image. */
/*******************************************************/
/*Pre: */
/* - sigmaS and sigmaR are the spatial and range */
/* radii of the search window respectively */
/* - minRegion is the minimum point density that */
/* a region may have in the resulting segment- */
/* ed image */
/* - speedUpLevel determines whether or not the */
/* filtering should be optimized for faster */
/* execution: a value of NO_SPEEDUP turns this */
/* optimization off and a value SPEEDUP turns */
/* this optimization on */
/*Post: */
/* - the defined image is segmented and the */
/* resulting segmented image is stored in the */
/* private data members of the image segmenter */
/* class. */
/* - any regions whose point densities are less */
/* than or equal to minRegion have been pruned */
/* from the segmented image. */
/*******************************************************/
void msImageProcessor::Segment(int sigmaS, float sigmaR, int minRegion, SpeedUpLevel speedUpLevel)
{
//make sure kernel is properly defined...
if((!h)||(kp < 2))
{
ErrorHandler("msImageProcessor", "Segment", "Kernel corrupt or undefined.");
return;
}
//Apply mean shift to data set using sigmaS and sigmaR...
Filter(sigmaS, sigmaR, speedUpLevel);
//check for errors
if(ErrorStatus == EL_ERROR)
return;
//check to see if the system has been halted, if so exit
if(ErrorStatus == EL_HALT)
return;
#ifdef USE_MSSYS_PROGRESS
//Check to see if the algorithm is to be halted, if so then
//destroy output and exit
if((ErrorStatus = msSys.Progress((float)(0.85))) == EL_HALT)
{
DestroyOutput();
return;
}
#endif
#ifdef PROMPT
msSys.Prompt("Applying transitive closure...");
msSys.StartTimer();
#endif
//allocate memory visit table
visitTable = new unsigned char [L];
//Apply transitive closure iteratively to the regions classified
//by the RAM updating labels and modes until the color of each neighboring
//region is within sqrt(rR2) of one another.
rR2 = (float)(h[1]*h[1]*0.25);
TransitiveClosure();
int oldRC = regionCount;
int deltaRC, counter = 0;
do {
TransitiveClosure();
deltaRC = oldRC-regionCount;
oldRC = regionCount;
counter++;
} while ((deltaRC <= 0)&&(counter < 10));
//de-allocate memory for visit table
delete [] visitTable;
visitTable = NULL;
#ifdef USE_MSSYS_PROGRESS
//Check to see if the algorithm is to be halted, if so then
//destroy output and regions adjacency matrix and exit
if((ErrorStatus = msSys.Progress((float)(0.95))) == EL_HALT)
{
DestroyRAM();
DestroyOutput();
return;
}
#endif
#ifdef PROMPT
double timer = msSys.ElapsedTime();
msSys.Prompt("done. (%6.2f seconds, numRegions = %6d).\nPruning spurious regions\t... ", timer, regionCount);
msSys.StartTimer();
#endif
//Prune spurious regions (regions whose area is under
//minRegion) using RAM
Prune(minRegion);
#ifdef PROMPT
timer = msSys.ElapsedTime();
msSys.Prompt("done. (%6.2f seconds, numRegions = %6d)\nPruning spurious regions ...", timer, regionCount);
msSys.StartTimer();
#endif
#ifdef USE_MSSYS_PROGRESS
//Check to see if the algorithm is to be halted, if so then
//destroy output and regions adjacency matrix and exit
if((ErrorStatus = msSys.Progress(1.0)) == EL_HALT)
{
DestroyRAM();
DestroyOutput();
return;
}
#endif
//de-allocate memory for region adjacency matrix
DestroyRAM();
//output to msRawData
int j, i, label;
for(i = 0; i < L; i++)
{
label = labels[i];
for(j = 0; j < N; j++)
{
msRawData[N*i+j] = modes[N*label+j];
}
}
//done.
return;
}
/*/\/\/\/\/\/\/\/\/\/\/\/\*/
/* Data Space Conversion */
/*\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*RGB To LUV */
/*******************************************************/
/*Converts an RGB vector to LUV. */
/* */
/*See: */
/* G. Wyszecki and W.S. Stiles: Color Science: */
/* Concepts and Methods, Quantitative Data and */
/* Formulae, Wiley, New York, 1982. */
/*******************************************************/
/*Pre: */
/* - rgbVal is an unsigned char array containing */
/* the RGB vector */
/* - luvVal is a floating point array containing */
/* the resulting LUV vector */
/*Post: */
/* - rgbVal has been converted to LUV and the */
/* result has been stored in luvVal. */
/*******************************************************/
void msImageProcessor::RGBtoLUV(byte *rgbVal, float *luvVal)
{
//delcare variables
double x, y, z, L0, u_prime, v_prime, constant;
//convert RGB to XYZ...
x = XYZ[0][0]*rgbVal[0] + XYZ[0][1]*rgbVal[1] + XYZ[0][2]*rgbVal[2];
y = XYZ[1][0]*rgbVal[0] + XYZ[1][1]*rgbVal[1] + XYZ[1][2]*rgbVal[2];
z = XYZ[2][0]*rgbVal[0] + XYZ[2][1]*rgbVal[1] + XYZ[2][2]*rgbVal[2];
//convert XYZ to LUV...
//compute L*
L0 = y / (255.0 * Yn);
if(L0 > Lt)
luvVal[0] = (float)(116.0 * (pow(L0, 1.0/3.0)) - 16.0);
else
luvVal[0] = (float)(903.3 * L0);
//compute u_prime and v_prime
constant = x + 15 * y + 3 * z;
if(constant != 0)
{
u_prime = (4 * x) / constant;
v_prime = (9 * y) / constant;
}
else
{
u_prime = 4.0;
v_prime = 9.0/15.0;
}
//compute u* and v*
luvVal[1] = (float) (13 * luvVal[0] * (u_prime - Un_prime));
luvVal[2] = (float) (13 * luvVal[0] * (v_prime - Vn_prime));
//done.
return;
}
/*******************************************************/
/*LUV To RGB */
/*******************************************************/
/*Converts an LUV vector to RGB. */
/*******************************************************/
/*Pre: */
/* - luvVal is a floating point array containing */
/* the LUV vector */
/* - rgbVal is an unsigned char array containing */
/* the resulting RGB vector */
/*Post: */
/* - luvVal has been converted to RGB and the */
/* result has been stored in rgbVal. */
/*******************************************************/
//define inline rounding function...
inline int my_round(double in_x)
{
if (in_x < 0)
return (int)(in_x - 0.5);
else
return (int)(in_x + 0.5);
}
void msImageProcessor::LUVtoRGB(float *luvVal, byte *rgbVal)
{
//declare variables...
int r, g, b;
double x, y, z, u_prime, v_prime;
//perform conversion
if(luvVal[0] < 0.1)
r = g = b = 0;
else
{
//convert luv to xyz...
if(luvVal[0] < 8.0)
y = Yn * luvVal[0] / 903.3;
else
{
y = (luvVal[0] + 16.0) / 116.0;
y *= Yn * y * y;
}
u_prime = luvVal[1] / (13 * luvVal[0]) + Un_prime;
v_prime = luvVal[2] / (13 * luvVal[0]) + Vn_prime;
x = 9 * u_prime * y / (4 * v_prime);
z = (12 - 3 * u_prime - 20 * v_prime) * y / (4 * v_prime);
//convert xyz to rgb...
//[r, g, b] = RGB*[x, y, z]*255.0
r = my_round((RGB[0][0]*x + RGB[0][1]*y + RGB[0][2]*z)*255.0);
g = my_round((RGB[1][0]*x + RGB[1][1]*y + RGB[1][2]*z)*255.0);
b = my_round((RGB[2][0]*x + RGB[2][1]*y + RGB[2][2]*z)*255.0);
//check bounds...
if(r < 0) r = 0; if(r > 255) r = 255;
if(g < 0) g = 0; if(g > 255) g = 255;
if(b < 0) b = 0; if(b > 255) b = 255;
}
//assign rgb values to rgb vector rgbVal
rgbVal[0] = r;
rgbVal[1] = g;
rgbVal[2] = b;
//done.
return;
}
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/* Filtered and Segmented Image Output */
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
/*******************************************************/
/*Get Raw Data */
/*******************************************************/
/*The output image data is returned. */
/*******************************************************/
/*Pre: */
/* - outputImageData is a pre-allocated floating */
/* point array used to store the filtered or */
/* segmented image pixels. */
/*Post: */
/* - the filtered or segmented image data is */
/* stored by outputImageData. */
/*******************************************************/
void msImageProcessor::GetRawData(float *outputImageData)
{
//make sure that outputImageData is not NULL
if(!outputImageData)
{
ErrorHandler("msImageProcessor", "GetRawData", "Output image data buffer is NULL.");
return;
}
//copy msRawData to outputImageData