-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvImage.cpp
2460 lines (2054 loc) · 58.8 KB
/
AdvImage.cpp
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
/* This file is part of Tomato Analyzer.
Tomato Analyzer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Tomato Analyzer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tomato Analyzer. If not, see <http://www.gnu.org/licenses/>. */
// AdvImage.cpp: implementation of the CAdvImage class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
//#include "BinaryObjects.h"
#include "BinObjs.h"
#include "BoundingBoxList.h"
#include "AdvImage.h"
#include "Histogram.h"
#include "Slice.h" // for findHue()
#include <stdlib.h>
#include <utility>
#include <assert.h>
#include <math.h>
#include <float.h>
#include "ImgTool.h"
#include <io.h>
#include <iostream>
#include <fstream>
#include "new_img_function.h"
#include <CVIPcolor.h>
#include "CVIPdecls.h"
#include <vector>
#include <string>
#include <algorithm>
#define pi 3.1415926535897932
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
using namespace ipt;
using namespace std;
int compare(const void * a, const void *b);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAdvImage::CAdvImage()
{
mImage = NULL;
mUpdate = true;
mScale = 0.35f;
mChangeForegroundColor = true;
}
CAdvImage::~CAdvImage()
{
}
void CAdvImage::Serialize(CArchive &ar)
{
if(ar.IsStoring())
{
unsigned char ** ppix;
int imgh = GetHeight();
int imgw = GetWidth();
int nBand= GetNumBands();
ar << imgw << imgh << nBand;
for(int iBand=0; iBand<nBand; iBand++)
{
ppix = GetBand(iBand);
for(int iLine=0; iLine<imgh; iLine++)
ar.Write(ppix[iLine], imgw);
}
}
else
{
unsigned char ** ppix;
int imgw, imgh, nBand;
ar >> imgw >> imgh >> nBand;
if(nBand>1)
CreateRGB(imgw, imgh);
else
CreateGrayScale(imgw, imgh);
if(mImage != NULL)
{
for(int iBand=0; iBand<nBand; iBand++)
{
ppix = GetBand(iBand);
for(int iLine=0; iLine<imgh; iLine++)
ar.Read(ppix[iLine], imgw);
}
}
}
}
CAdvImage& CAdvImage::operator = (CAdvImage& other)
{
if(mImage)
{
::delete_Image(mImage);
mImage = NULL;
}
int imgh = other.GetHeight();
int imgw = other.GetWidth();
CreateRGB(imgw, imgh);
unsigned char ** r = GetBand(0);
unsigned char ** g = GetBand(1);
unsigned char ** b = GetBand(2);
unsigned char ** rA = other.GetBand(0);
unsigned char ** gA = other.GetBand(1);
unsigned char ** bA = other.GetBand(2);
for(int i=0; i<imgh; i++)
{
for(int j=0; j<imgw; j++)
{
r[i][j] = rA[i][j];
g[i][j] = gA[i][j];
b[i][j] = bA[i][j];
}
}
return *this;
}
CAdvImage& CAdvImage::YUV(CAdvImage& other)
{
if(mImage)
{
::delete_Image(mImage);
mImage = NULL;
}
int imgh = other.GetHeight();
int imgw = other.GetWidth();
CreateRGB(imgw, imgh);
unsigned char ** r = GetBand(0);
unsigned char ** g = GetBand(1);
unsigned char ** b = GetBand(2);
unsigned char ** rA = other.GetBand(0);
unsigned char ** gA = other.GetBand(1);
unsigned char ** bA = other.GetBand(2);
for(int i=0; i<imgh; i++)
{
for(int j=0; j<imgw; j++)
{
r[i][j] = (rA[i][j] + gA[i][j] + bA[i][j])/3;
g[i][j] = (rA[i][j] + gA[i][j])/2;
double maxCol = max(rA[i][j], max(gA[i][j],bA[i][j]) );
double minCol = min(rA[i][j], min(gA[i][j],bA[i][j]) );
b[i][j] = (minCol == maxCol ? 0 : 100 * double(maxCol - minCol) / double(maxCol)); // saturation
}
}
return *this;
}
CAdvImage * CAdvImage::GetCopy()
{
CAdvImage * newImage = new CAdvImage();
newImage->mImage = duplicate_Image(mImage);
newImage->SetScale(this->GetScale());
return newImage;
}
CAdvImage& CAdvImage::fromIptImage(const CIptBwImage& bwImg)
{
CIptBwImage::const_iterator iter = bwImg.begin();
if(mImage)
{
::delete_Image(mImage);
mImage=0;
}
CreateRGB(bwImg.width(), bwImg.height());
unsigned char** destR = (unsigned char**)mImage->image_ptr[0]->rptr;
unsigned char** destG = (unsigned char**)mImage->image_ptr[1]->rptr;
unsigned char** destB = (unsigned char**)mImage->image_ptr[2]->rptr;
for(int y=0; y<GetHeight(); y++)
{
for(int x=0; x<GetWidth(); x++)
{
BYTE tmp = (*iter) * 255;
destR[y][x] = tmp;
destG[y][x] = tmp;
destB[y][x] = tmp;
++iter;
}
}
return *this;
}
std::vector<IPTPOINT>* CAdvImage::toPointLst(int iChannel, IPTPOINT ctr)
{
std::vector<IPTPOINT>* ret = new std::vector<IPTPOINT>;
IPTPOINT pt;
int w = GetWidth();
int h = GetHeight();
unsigned char** dest = (unsigned char**)mImage->image_ptr[iChannel]->rptr;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
if(255==dest[i][j])
{
pt.x = j - ctr.x;
pt.y = ctr.y - i;
ret->push_back(pt);
}
}
}
return ret;
}
void CAdvImage::MakeBinary()
{
//Extracting the three bands for easy manipulation
unsigned char** rV = (unsigned char**)mImage->image_ptr[0]->rptr;
unsigned char** gV = (unsigned char**)mImage->image_ptr[1]->rptr;
unsigned char** bV = (unsigned char**)mImage->image_ptr[2]->rptr;
for(unsigned int y=0; y<getNoOfRows_Image(mImage); y++)
for(unsigned int x=0; x<getNoOfCols_Image(mImage); x++)
{
if(!((rV[y][x] == 0) && (gV[y][x] == 0) && (bV[y][x] == 0)))
{
rV[y][x] = 255;
gV[y][x] = 255;
bV[y][x] = 255;
}
}
}
/// Not used?
CAdvImage * CAdvImage::Threshold(float inRMax, float inGMax, float inBMax,
float inRMin, float inGMin, float inBMin, bool inverse)
{
//Thresholding the CAdvImage and returning a thresholded copy.
//if(inclusive) all values between {max,min} are white else black
// else all values between {max,min} are black else white
//Quick test if image can be thresholded
if(GetNumBands() != 3)
return NULL;
//Creating a return CAdvImage object
// and an underlying baseImg structure.
CAdvImage * thrImg = new CAdvImage();
thrImg->mImage = duplicate_Image(mImage);
//Extracting the three bands for easy manipulation
unsigned char** origR = (unsigned char**)mImage->image_ptr[0]->rptr;
unsigned char** origG = (unsigned char**)mImage->image_ptr[1]->rptr;
unsigned char** origB = (unsigned char**)mImage->image_ptr[2]->rptr;
unsigned char** binValue = (unsigned char**)thrImg->mImage->image_ptr[0]->rptr;
unsigned char** gV = (unsigned char **)thrImg->mImage->image_ptr[1]->rptr;
unsigned char** bV = (unsigned char **)thrImg->mImage->image_ptr[2]->rptr;
bool black;
for(unsigned int y=0; y<getNoOfRows_Image(mImage); y++)
for(unsigned int x=0; x<getNoOfCols_Image(mImage); x++)
{
if(inverse == false)
{
black = true;
if((origR[y][x] < inRMin) || (origR[y][x] > inRMax))
black = false;
if((origG[y][x] < inGMin) || (origG[y][x] > inGMax))
black = false;
if((origB[y][x] < inBMin) || (origB[y][x] > inBMax))
black = false;
}
else
{
black = false;
if((origR[y][x] < inRMin) || (origR[y][x] > inRMax))
black = true;
if((origG[y][x] < inGMin) || (origG[y][x] > inGMax))
black = true;
if((origB[y][x] < inBMin) || (origB[y][x] > inBMax))
black = true;
}
if(black)
{
binValue[y][x] = 0;
gV[y][x] = 0;
bV[y][x] = 0;
}
else
{
binValue[y][x] = 255;
gV[y][x] = 255;
bV[y][x] = 255;
}
}
return thrImg;
}
/// Not used?
CAdvImage * CAdvImage::ThresholdSpecial()
{
//Thresholding the CAdvImage and returning a thresholded copy.
//if(inclusive) all values between {max,min} are white else black
// else all values between {max,min} are black else white
//Quick test if image can be thresholded
if(GetNumBands() != 3)
return NULL;
//Creating a return CAdvImage object
// and an underlying baseImg structure.
CAdvImage * thrImg = new CAdvImage();
thrImg->mImage = duplicate_Image(mImage);
thrImg->SetScale(GetScale());
//Extracting the three bands for easy manipulation
unsigned char** origR = (unsigned char**)mImage->image_ptr[0]->rptr;
unsigned char** origG = (unsigned char**)mImage->image_ptr[1]->rptr;
unsigned char** origB = (unsigned char**)mImage->image_ptr[2]->rptr;
unsigned char** binValue = (unsigned char**)thrImg->mImage->image_ptr[0]->rptr;
unsigned char** gV = (unsigned char **)thrImg->mImage->image_ptr[1]->rptr;
unsigned char** bV = (unsigned char **)thrImg->mImage->image_ptr[2]->rptr;
//bool black;
for(unsigned int y=0; y<getNoOfRows_Image(mImage); y++)
for(unsigned int x=0; x<getNoOfCols_Image(mImage); x++)
{
int diff = origR[y][x] - origG[y][x];
diff = (diff < 0 ? -diff : diff);
if(diff <= 15)
{
binValue[y][x] = 255;
gV[y][x] = 255;
bV[y][x] = 255;
}
else
{
binValue[y][x] = 0;
gV[y][x] = 0;
bV[y][x] = 0;
}
}
return thrImg;
}
/// Not used?
//Thresholding based on (hue, sat, val) tuples.
CAdvImage * CAdvImage::ThresholdHSV( float inh1, float ins1, float inv1,
float inh2, float ins2, float inv2,
bool inKeep)
{
unsigned int i, j, numRows, numCols;
CAdvImage * hsvImg = new CAdvImage();
CAdvImage * outImg = new CAdvImage();
float norm[3] = {255.0f, 255.0f, 255.0f};
//Made change 4_26_00 chack that this works.
// May have been a mem leak.
hsvImg->mImage = duplicate_Image(mImage);
outImg->mImage = duplicate_Image(mImage);
hsvImg->mImage = colorxform(hsvImg->mImage, HSV, norm, NULL, 1);
//Setting up easy accessors
CVIP_TYPE datatyp = hsvImg->mImage->image_ptr[0]->data_type;
float ** hV = (float **)hsvImg->mImage->image_ptr[0]->rptr;
float ** sV = (float **)hsvImg->mImage->image_ptr[1]->rptr;
float ** vV = (float **)hsvImg->mImage->image_ptr[2]->rptr;
CVIP_TYPE rgbType = mImage->image_ptr[0]->data_type;
unsigned char ** rV = (unsigned char **)outImg->mImage->image_ptr[0]->rptr;
unsigned char ** gV = (unsigned char **)outImg->mImage->image_ptr[1]->rptr;
unsigned char ** bV = (unsigned char **)outImg->mImage->image_ptr[2]->rptr;
//More initializations
numRows = getNoOfRows_Image(hsvImg->mImage);
numCols = getNoOfCols_Image(hsvImg->mImage);
//hsvImg is now in HSV space all are normalized (0.0...255.0, 0.0...255.0, 0.0...255.0)
// For traditional rep, h of hsvImg should be /255 * 360.
// s and v /255 each.
for(i=0; i< numRows; i++)
for(j=0; j<numCols; j++)
{
float tradH, tradS, tradV;
bool white = true;
tradH = (hV[i][j] / 255.0f) * 360.0f;
tradS = (sV[i][j] / 255.0f) * 100.0f;
tradV = (vV[i][j] / 255.0f) * 100.0f;
if(inKeep == true)
{
white = true;
if((tradH > inh1) && (tradH < inh2))
white = false;
if((tradS > ins1) && (tradS < ins2))
white = false;
if((tradV > inv1) && (tradV < inv2))
white = false;
}
else
{
white = true;
if((tradH < inh1) && (tradH > inh2))
white = false;
if((tradS < ins1) && (tradS > ins2))
white = false;
if((tradV < inv1) && (tradV > inv2))
white = false;
}
if(!white)
{
rV[i][j] = 0;
gV[i][j] = 0;
bV[i][j] = 0;
}
else
{
rV[i][j] = 255;
gV[i][j] = 255;
bV[i][j] = 255;
}
}
delete hsvImg;
return outImg;
}
/**
* A locally based thresholding algorithm. This algorithm will divide a copy of this image
* into inHDivs * inWDivs equal sized images, and threshold each subimage. It returns the copy, which has been
* converted to black (background) and white (foreground).
* It sets nNew to the calculated threshold for the last subimage.
*
* The following parameters are now ignored: inChannel, inHistoPercent, nDesired, nBlue.
*/
CAdvImage * CAdvImage::ThresholdLocally(int inWDivs, int inHDivs, int inChannel, float inHistoPercent, int nDesired, int& nNew, int nBlue)
{
//Setup the resulting image.
CAdvImage * outImage = new CAdvImage();
outImage->mImage = duplicate_Image(this->mImage);
CRect subimgrect(0, 0, 1, 1);
int x1,y1, x2, y2;
int imgw, imgh, wdiv, hdiv;
imgw = this->GetWidth();
imgh = this->GetHeight();
assert(inWDivs != 0);
assert(inHDivs != 0);
wdiv = imgw/inWDivs;
hdiv = imgh/inHDivs;
for(int i = 0; i < inWDivs; i++)
{
for(int j = 0; j < inHDivs; j++)
{
x1 = i * wdiv;
y1 = j * hdiv;
x2 = (i+1 == inWDivs ? imgw : (i+1) * wdiv);
y2 = (j+1 == inHDivs ? imgh : (j+1) * hdiv);
// Makes the outImage black and white.
subimgrect.SetRect(x1, y1, x2, y2);
// inChannel, inHistoPercent, nDesired, nBlue are ignored
nNew = ThresholdSubImage(subimgrect, outImage, inChannel, inHistoPercent, nDesired, nBlue);
}
}
return outImage;
}
/**
* This is the function that is actually used to distinguish foreground from background by picking a threshold,
* which is the return value. The function
* analyzes the section of resultImg defined by inSubImgRect and returns the result in resultImg by changing
* all background pixels to black and all foreground pixels to white. (The other parameters are no longer used.)
*
* If the given image is all one color, then all pixels are marked as black (background). If the image has background
* lighter than foreground, then (as usual) the lighter pixels are marked as white (foreground) and the darker
* pixels are marked as black (background).
*
* See http://en.wikipedia.org/wiki/Thresholding_%28image_processing%29. We used to use K-means clustering,
* but it was too slow and not accurate enough. It was better to build a histogram and look for the valley point.
*
* The following parameters are now ignored: inChannel, inHistoPercent, nDesired, nBlue.
*/
int CAdvImage::ThresholdSubImage(CRect inSubImgRect, CAdvImage * resultImg, int inChannel, float inHistoPercent, int nDesired, int nBlue)
{
unsigned char ** rval = resultImg->GetBand(0);
unsigned char ** gval = resultImg->GetBand(1);
unsigned char ** bval = resultImg->GetBand(2);
CPoint tl = inSubImgRect.TopLeft();
CPoint lr = inSubImgRect.BottomRight();
// Build histogram of L* values
std::vector<double> tmp(inSubImgRect.Width());
std::vector< std::vector<double> > lval(inSubImgRect.Height(), tmp);
for (int i = tl.y; i < lr.y; i++) {
for (int j = tl.x; j < lr.x; j++) {
double lab[3];
RGBtoLAB(rval[i][j], gval[i][j], bval[i][j], lab);
lval[i][j] = lab[0];
}
}
CHistogram hist(inSubImgRect, lval);
int minLIndex = hist.firstNonZeroIndex();
int maxLIndex = hist.lastNonZeroIndex();
int threshold; // minimum L* value for foreground
if (minLIndex == maxLIndex)
{
threshold = maxLIndex + 1; // image has uniform L* throughout --> all background
}
else
{
float avgL = hist.avgLValueInRange(minLIndex, maxLIndex + 1);
//threshold = avgL + 0.5; // Tentative threshold value (rounded)
threshold = avgL - (avgL * THESHOLD_ADJUST_CONST);
// Adjust tentative threshold value.
// If it's on the slope of one of the peaks, move it to the valley (absolute minimum point between peaks).
const int WINDOW = 10; // Assumes peaks are separated by > WINDOW indices in L* histogram
// I commented this out because it's adjusting the average pixel value, and
// the adjusted value is filtering out fruit.
/*
while (true) { // changed to false for debugging
float leftAvg = hist.avgInRange(threshold - 3*WINDOW/2, threshold - WINDOW/2);
float midAvg = hist.avgInRange(threshold - WINDOW/2, threshold + WINDOW/2);
float rightAvg = hist.avgInRange(threshold + WINDOW/2, threshold + 3*WINDOW/2);
if (leftAvg < midAvg && midAvg < rightAvg) {
threshold -= WINDOW;
} else if (leftAvg > midAvg && midAvg > rightAvg) {
threshold += WINDOW;
} else if (leftAvg >= midAvg && midAvg <= rightAvg) {
threshold = hist.minIndexInRange(threshold - WINDOW/2, threshold + WINDOW/2);
break;
} else {
threshold = avgL + 0.5; // Something went wrong - revert to average L* value
break;
}
} */
}
// Convert image to black (background) and white (foreground)
for (int i = tl.y; i < lr.y; i++) {
for (int j = tl.x; j < lr.x; j++) {
if (lval[i][j] < threshold) {
rval[i][j] = gval[i][j] = bval[i][j] = 0;
} else {
rval[i][j] = gval[i][j] = bval[i][j] = 255;
}
}
}
return threshold;
}
void * CAdvImage::RGBtoLAB(int r, int g, int b, double lab[]){
double var_R = ( double(r) / 255.0 ); //Where R = 0 � 255
double var_G = ( double(g) / 255.0 ); //Where G = 0 � 255
double var_B = ( double(b) / 255.0 ); //Where B = 0 � 255
if ( var_R > 0.04045 ) var_R = pow(( ( var_R + 0.055 ) / 1.055 ), 2.4);
else var_R = var_R / 12.92;
if ( var_G > 0.04045 ) var_G = pow(( ( var_G + 0.055 ) / 1.055 ), 2.4);
else var_G = var_G / 12.92;
if ( var_B > 0.04045 ) var_B = pow(( ( var_B + 0.055 ) / 1.055 ), 2.4);
else var_B = var_B / 12.92;
var_R = var_R * 100;
var_G = var_G * 100;
var_B = var_B * 100;
//Observer. = 2�, Illuminant = D65
double X = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805;
double Y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722;
double Z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505;
double var_X = X / 95.047 ; //ref_X = 95.047 Observer= 2�, Illuminant= D65
double var_Y = Y / 100.000; //ref_Y = 100.000
double var_Z = Z / 108.883; //ref_Z = 108.883
if ( var_X > 0.008856 ) var_X = pow(var_X, ( 1.0/3.0 ));
else var_X = ( 7.787 * var_X ) + ( 16.0 / 116.0 );
if ( var_Y > 0.008856 ) var_Y = pow(var_Y, ( 1.0/3.0 ));
else var_Y = ( 7.787 * var_Y ) + ( 16.0 / 116.0 );
if ( var_Z > 0.008856 ) var_Z = pow(var_Z,( 1.0/3.0 ));
else var_Z = ( 7.787 * var_Z ) + ( 16.0 / 116.0 );
double CIEL = ( 116 * var_Y ) - 16;
double CIEa = 500 * ( var_X - var_Y );
double CIEb = 200 * ( var_Y - var_Z );
//double * lab = new double[3];
lab[0] = CIEL;
lab[1] = CIEa;
lab[2] = CIEb;
return lab;
}
/**
* Finds the hue of a pixel, given the red, green and blue values.
* param red the red value of a pixel
* param gre the green value of a pixel
* param blu the blue value of a pixel
* return the hue (a double)
*/
double CAdvImage::findHue(double valL , double valA , double valB){
double hue = 0;
// The hue value depends on the b* value which is being passed as valB
if (valB > 0)
{
hue = (180 / pi) * acos(valA / (sqrt(pow(valA,2) + pow(valB, 2))));
}
else
{
hue = 360 - ((180 / pi) * acos(valA / (sqrt(pow(valA, 2) + pow(valB, 2)))));
}
return hue;
}
void CAdvImage::ColorBinaryObject(CPixelList & inList, float inr, float ing, float inb)
{
CPixelList::iterator pixIter;
CPixel myPix;
unsigned char ** rV = (unsigned char **)mImage->image_ptr[0]->rptr;
unsigned char ** gV = (unsigned char **)mImage->image_ptr[1]->rptr;
unsigned char ** bV = (unsigned char **)mImage->image_ptr[2]->rptr;
for(pixIter = inList.begin(); pixIter != inList.end(); pixIter++)
{
myPix = *pixIter;
rV[myPix.y][myPix.x] = inr;
gV[myPix.y][myPix.x] = ing;
bV[myPix.y][myPix.x] = inb;
}
}
/**
* This function will scan through a documents image in binary format,
* and label all of the occuring clusters of white pixels. For
* every such cluster, there will be a CPixelList * entry in the
* returned list
*/
CBinObjs * CAdvImage::GetBinaryObjects()
{
//Duplicating the image to label.
// Image * labelImg = duplicate_Image(mImage);
//The returned list
CBinObjs * binObjs = new CBinObjs();
//The list of pixels that make up a binary obj.
CPixelList * addList;
//The current maximum label
int labelOn = 1;
//Miscellaneous
int i, j;
int numRows, numCols;
//Pixel coordinate
CPixel pix;
bool deb = false;
//Need only the red channel because the image is binary.
unsigned char** binValue = (unsigned char**)mImage->image_ptr[0]->rptr;
unsigned char** gV = (unsigned char**)mImage->image_ptr[1]->rptr;
unsigned char** bV = (unsigned char**)mImage->image_ptr[2]->rptr;
numRows = getNoOfRows_Image(mImage);
numCols = getNoOfCols_Image(mImage);
//Allocating 2D array of labels.
unsigned int ** labelValue = new unsigned int*[numRows];
for(i=0; i<numRows; i++)
{
labelValue[i] = new unsigned int[numCols];
memset(labelValue[i], 0, numCols);
}
//Label first row and initialize labelled image. Special case.
for(j = 0; j < numCols; j++)
{
//So background has initial label.
labelValue[0][j] = 0;
//if we encounter a new obj
if(binValue[0][j] == 255)
{
//Special case no left neighbor
if(j == 0)
{
//Maintaining list of objects as we go.
addList = new CPixelList();
pix.Set(j, 0);
addList->push_back(pix);
binObjs->Add(addList, labelOn);
labelValue[0][j] = labelOn++;
}
else //There is a neighbor
{
//if neighbor is marked
if(labelValue[0][j-1] != 0)
{
pix.Set(j, 0);
binObjs->GetList(labelValue[0][j-1])->push_back(pix);
labelValue[0][j] = labelValue[0][j-1];
}
else //assign new label
{
//Maintaining list of objects as we go.
addList = new CPixelList();
pix.Set(j, 0); //Set(x,y)
addList->push_back(pix);
binObjs->Add(addList, labelOn);
labelValue[0][j] = labelOn++;
}
}
}
}
//General algorithm
for(i = 1; i < numRows; i++)
{
for(j = 0; j < numCols; j++)
{
if(i == 305 && j == 518)
deb = true;
labelValue[i][j] = 0;
//Encounter object
if(binValue[i][j] == 255)
{
//Three cases.
//1. First column
if(j == 0)
{
//[x][?]
if(labelValue[i-1][j] != 0)
{
pix.Set(j, i);
binObjs->GetList(labelValue[i-1][j])->push_back(pix);
labelValue[i][j] = labelValue[i-1][j];
}
//[0][x]
else if(labelValue[i-1][j+1] != 0)
{
pix.Set(j, i);
binObjs->GetList(labelValue[i-1][j+1])->push_back(pix);
labelValue[i][j] = labelValue[i-1][j+1];
}
//[0][0]
else
{
addList = new CPixelList();
pix.Set(j, i); //Set(x,y)
addList->push_back(pix);
binObjs->Add(addList, labelOn);
labelValue[i][j] = labelOn++;
}
}
//2. Last column
else if(j == (numCols - 1))
{
//Four cases
//[x][x]
//[x]
if(labelValue[i-1][j-1] != 0)
{
pix.Set(j, i);
binObjs->GetList(labelValue[i-1][j-1])->push_back(pix);
labelValue[i][j] = labelValue[i-1][j-1];
}
//[?][x]
//[]
else if((labelValue[i-1][j] != 0))
{
pix.Set(j, i);
CPixelList * list = binObjs->GetList(labelValue[i-1][j]);
int labelAt = labelValue[i-1][j];
if(list == NULL)
{
list = binObjs->GetList(labelValue[i-1][j]);
this->Write("temp.gif");
}
list->push_back(pix);
labelValue[i][j] = labelValue[i-1][j];
}
else if(labelValue[i][j-1] != 0)
{
pix.Set(j, i);
binObjs->GetList(labelValue[i][j-1])->push_back(pix);
labelValue[i][j] = labelValue[i][j-1];
}
else
{
addList = new CPixelList();
pix.Set(j, i);
addList->push_back(pix);
binObjs->Add(addList, labelOn);
labelValue[i][j] = labelOn++;
}
}
//First two cases almost never executed.
//Ignore previous notation.
//3. In general
//For a given pixel i, we only have to look at the four surrounding pixels
//[ ][ ][ ]
//[ ][i]
//----------------------------
//[o] means pixel is labeled
//[x] means pixel can't be labeled
//[?] means does not matter if pixel is labeled.
else
{
//[?][o][?]
//[?][i]
//No label connections can occur if this pixel is labeled.
// all surrounding if labeled must be same label.
if(labelValue[i-1][j] != 0)
{
pix.Set(j,i);
binObjs->GetList(labelValue[i-1][j])->push_back(pix);
labelValue[i][j] = labelValue[i-1][j];
}
else if(labelValue[i-1][j+1] != 0)
{
//[?][x][o]
//[o][i]
//A connection will occur.
if(labelValue[i][j-1] != 0)
{
if(labelValue[i-1][j+1] != labelValue[i][j-1])
{
CombineLabelLists(binObjs, labelValue[i][j-1],
labelValue[i-1][j+1], labelValue);
}
pix.Set(j, i);
binObjs->GetList(labelValue[i][j-1])->push_back(pix);
labelValue[i][j] = labelValue[i][j-1];
}
//[o][x][o]
//[x][i]
//A connection will occur
else if(labelValue[i-1][j-1] != 0)
{
if(labelValue[i-1][j-1] != labelValue[i-1][j+1])
{
CombineLabelLists(binObjs, labelValue[i-1][j-1],
labelValue[i-1][j+1], labelValue);
}
pix.Set(j, i);
binObjs->GetList(labelValue[i-1][j-1])->push_back(pix);
labelValue[i][j] = labelValue[i-1][j-1];
}
//[x][x][o]
//[x][i]
else
{
pix.Set(j,i);
binObjs->GetList(labelValue[i-1][j+1])->push_back(pix);
labelValue[i][j] = labelValue[i-1][j+1];
}
}
//[?][x][x]
//[o][i]
//No connection just set label.
else if((labelValue[i][j-1] != 0))
{
pix.Set(j, i);
binObjs->GetList(labelValue[i][j-1])->push_back(pix);
labelValue[i][j] = labelValue[i][j-1];
}
//[o][x][x]
//[x][i]
//Just set label.
else if(labelValue[i-1][j-1] != 0)
{
pix.Set(j, i);
binObjs->GetList(labelValue[i-1][j-1])->push_back(pix);
labelValue[i][j] = labelValue[i-1][j-1];
}
//[x][x][x]
//[x][i]
//New label created.
else
{
//New Label added
addList = new CPixelList();
pix.Set(j, i);
addList->push_back(pix);
binObjs->Add(addList, labelOn);
labelValue[i][j] = labelOn;
labelOn++;
}
}
}