/
ImageDecomp.cpp
914 lines (829 loc) · 23.6 KB
/
ImageDecomp.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
// ImageDecomp.cpp: implementation of the CImageDecomp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ImageDecomposition.h"
#include "ImageDecomp.h"
#include "Img.h"
#include "Color.h"
#include "Matrix.h"
#include "DisplayerDlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
double ** LMatrix;
Img result;
//double **M;
//for G-S iterations
const int nbIter = 10;
CImageDecomp::CImageDecomp()
{
scribbletype = 0;
albedoNum = 0;
shadingNum = 0;
fixShadingNum = 0;
//
scribbleShadingImg = NULL;
scribbleAlbedoImg = NULL;
scribbleFixShadingImg = NULL;
imgIn = NULL;
}
CImageDecomp::~CImageDecomp()
{
if (flagMatrix)
{
for (int j=0; j<imgHeight; j++)
{
if(flagMatrix[j])
{
delete[] flagMatrix[j];
flagMatrix[j]=NULL;
}
}
delete[] flagMatrix;
flagMatrix = NULL;
}
if(imgBuf)
{
delete[] imgBuf;
imgBuf = NULL;
}
if(rgbBuf)
{
delete[] rgbBuf;
rgbBuf = NULL;
}
if (albedoImgBuf)
{
delete[] albedoImgBuf;
albedoImgBuf = NULL;
}
if (shadingImgBuf)
{
delete[] shadingImgBuf;
shadingImgBuf = NULL;
}
if (scribbleAlbedoImg)
{
delete scribbleAlbedoImg;
scribbleAlbedoImg = NULL;
}
if (scribbleShadingImg)
{
delete scribbleShadingImg;
scribbleShadingImg = NULL;
}
if (scribbleFixShadingImg)
{
delete scribbleFixShadingImg;
scribbleFixShadingImg = NULL;
}
if (imgIn)
{
delete imgIn;
imgIn = NULL;
}
}
CImageDecomp::CImageDecomp(int x, int y)
{
scribbletype = 0;
albedoNum = 0;
shadingNum = 0;
fixShadingNum = 0;
// imgWidth = x;
// imgHeight = y;
// flagMatrix = new int*[y];
// for (int j=0; j<y; j++)
// {
// flagMatrix[j] = new int[x];
// }
// for (j=0; j<y; j++)
// {
// for (int i=0; i<x; i++)
// {
// flagMatrix[j][i] = 0;
// }
// }
}
void CImageDecomp::LoadSrcImage(CString pathnam, int height, int width, int bitdep, int type, int format)
{
//image format which denotes 4:4:4 format is 0 by default
imgHeight = height;
imgWidth = width;
bitDepth = bitdep;
FlagMemAlloc(imgWidth, imgHeight);
CFile infile;
infile.Open(pathnam, CFile::modeRead);
UINT flen = (UINT)infile.GetLength();
infile.SeekToBegin();
if (imgBuf)
delete[] imgBuf;
imgBuf = new BYTE[flen];
infile.Read((void *)imgBuf, flen);
infile.Close();
switch(type)
{
case TYPE_BMP:
LoadBmpImage();
break;
case TYPE_RGB:
LoadRgbImage();
break;
case TYPE_YUV:
LoadYuvImage(format);
}
}
void CImageDecomp::LoadYuvImage(int format)
{
}
void CImageDecomp::LoadBmpImage()
{
BITMAPFILEHEADER *bmpfile = (BITMAPFILEHEADER *)imgBuf;
BITMAPINFOHEADER *bmpinfo = (BITMAPINFOHEADER *)(imgBuf + sizeof(BITMAPFILEHEADER));
if (bmpfile->bfType != 0x4d42)
{
AfxMessageBox("Image is not bmp file!");
return;
}
if ((bmpinfo->biWidth!=imgWidth)||(bmpinfo->biHeight!=imgHeight))
{
AfxMessageBox("Wrong input image definition!");
return;
}
// int imgsize = imgWidth*imgHeight;
// int bufsize = (bitDepth==8) ? imgsize*3 : imgsize*3*2;
int bbc=24; //3*8
int bpl = ((bbc*imgWidth + 31)/32)*4;
int bufsize = bpl*imgHeight;
if(rgbBuf)
delete[] rgbBuf;
rgbBuf = new BYTE[bufsize];
BYTE *rgbdata = imgBuf + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
memcpy(rgbBuf, rgbdata, bufsize);
HeadStand((void*)rgbBuf, imgHeight, imgWidth, bitDepth);
//
ImgIn();
}
void CImageDecomp::LoadRgbImage()
{
}
//from bottom->top to top->bottom, for bmp file
void CImageDecomp::HeadStand(void *buffer, int height, int width, int bitdep)
{
int imgsize = width*height;
void *buftmp;
int bbc = 24; //3*8
int bpl = ((bbc*imgWidth + 31)/32)*4;
int bufsize = bpl*imgHeight;
buftmp = new BYTE[bufsize];
for (int i=0; i<height/2; i++)
{
memcpy((BYTE*)buftmp, (BYTE*)buffer+i*bpl, bpl);
memcpy((BYTE*)buffer+i*bpl, (BYTE*)buffer+(height-1)*bpl-i*bpl, bpl);
memcpy((BYTE*)buffer+(height-1)*bpl-i*bpl, buftmp, bpl);
}
delete[] buftmp;
}
BOOL CImageDecomp::InitFlags(int w, int h)
{
if (!flagMatrix)
{
AfxMessageBox("Flag memory failed.");
return FALSE;
}
for (int j=0; j<h; j++)
{
memset(flagMatrix[j], 0, w);
}
scribbletype = 0;
return TRUE;
}
void CImageDecomp::StoreFlags(int y, int x, BYTE type, int scribID)
{
flagMatrix[y][x] = scribID;
//type: 1-R, 2-G, 3-B
scribbletype = type;
}
BOOL CImageDecomp::FlagMemAlloc(int w, int h)
{
if (flagMatrix)
{
AfxMessageBox("Flag memory not released.");
return FALSE;
}
flagMatrix = new BYTE*[h];
for (int k=0; k<h; k++)
{
flagMatrix[k] = new BYTE[w];
}
//initialize
InitFlags(w, h);
return TRUE;
}
BOOL CImageDecomp::FlagMemRelease(int w, int h)
{
if (!flagMatrix)
{
AfxMessageBox("Flag memory already released");
return FALSE;
}
if (flagMatrix)
{
for (int k=0; k<h; k++)
{
if(flagMatrix[k])
{
delete[] flagMatrix[k];
flagMatrix[k] = NULL;
}
}
delete[] flagMatrix;
flagMatrix = NULL;
}
return TRUE;
}
BOOL CImageDecomp::SaveScribbles()
{
int maxScribbleIds = 0;
int tmp, i, j;
switch (scribbletype)
{
case 1:
//constant reflectance/albedo
if (scribbleAlbedoImg!=NULL)
{
delete scribbleAlbedoImg;
scribbleAlbedoImg = NULL;
}
scribbleAlbedoImg = new Img(imgWidth, imgHeight, Color(0, 0, 0));
for (j=0; j<imgHeight; j++)
{
for (i=0; i<imgWidth; i++)
{
tmp = flagMatrix[j][i];
//current pixel is scribbles
if (tmp!=0)
{
scribbleAlbedoImg->setPixel(i, j, Color(tmp, tmp, tmp, tmp));
// (scribbleAlbedoImg->pixel(i, j)).setScribbled();
if (maxScribbleIds<tmp)
maxScribbleIds = tmp;
}
}
}
albedoNum = maxScribbleIds;
break;
case 2:
//constant illumination/shading
if (scribbleShadingImg!=NULL)
{
delete scribbleShadingImg;
scribbleShadingImg = NULL;
}
scribbleShadingImg = new Img(imgWidth, imgHeight, Color(0,0,0));
for (j=0; j<imgHeight; j++)
{
for (i=0; i<imgWidth; i++)
{
tmp = flagMatrix[j][i];
if (tmp!=0)
{
scribbleShadingImg->setPixel(i, j, Color(tmp, tmp, tmp, tmp));
// (scribbleShadingImg->pixel(i, j)).setScribbled();
if (maxScribbleIds<tmp)
maxScribbleIds = tmp;
}
}
}
shadingNum = maxScribbleIds;
break;
case 3:
//fix illumination
if (scribbleFixShadingImg!=NULL)
{
delete scribbleFixShadingImg;
scribbleFixShadingImg = NULL;
}
scribbleFixShadingImg = new Img(imgWidth, imgHeight, Color(0, 0, 0));
int val = FIXEDSHADING;
for (j=0; j<imgHeight; j++)
{
for (i=0; i<imgWidth; i++)
{
tmp = flagMatrix[j][i];
if (tmp!=0)
{
//set "val" fix illumination pixels
scribbleFixShadingImg->setPixel(i, j, Color(val, val, val, tmp));
// (scribbleFixShadingImg->pixel(i, j)).setScribbled();
if (maxScribbleIds<tmp)
maxScribbleIds = tmp;
}
}
}
fixShadingNum = maxScribbleIds;
break;
}
scribbletype = 0;
return TRUE;
}
//this function convert rgb value of inputing image to an Img type object,
BOOL CImageDecomp::ImgIn()
{
int i, j, sp;
double r, g, b;
int mpv = MAX_PIX_VAL;
if (imgIn)
{
delete imgIn;
imgIn = NULL;
}
int bbc=24; //3*8
int bpl = ((bbc*imgWidth + 31)/32)*4;
//initialized to be 0
imgIn = new Img(imgWidth, imgHeight, Color(0, 0, 0));
for (j=0; j<imgHeight; j++)
{
for (i=0; i<imgWidth; i++)
{
sp = j*bpl + i*3; //3 for three bytes for rgb
r = (double)rgbBuf[sp];
g = (double)rgbBuf[sp+1];
b = (double)rgbBuf[sp+2];
//get the color value, here color value is the pixel value/max value
r = r/mpv;
g = g/mpv;
b = b/mpv;
imgIn->setPixel(i, j, Color(r, g, b));
}
}
return TRUE;
}
void CImageDecomp::buildLMatrix(Img imgIn)
{
int i,j,ik,jk;
int x,y;
int xC,yC,xiC,yiC,xjC,yjC;
double epsilon = 0.0000001;
double sqrt_epsilon = 0.001;
int w = imgIn.width();
int h = imgIn.height();
double **M = new double*[MSIZE];
for (i=0; i<MSIZE; i++)
{
M[i] = new double[3];
}
//M matrix (see equation 8)
M[MSIZE-3][0] = sqrt_epsilon; M[MSIZE-3][1] = 0; M[MSIZE-3][2] = 0;
M[MSIZE-2][0] = 0; M[MSIZE-2][1] = sqrt_epsilon; M[MSIZE-2][2] = 0;
M[MSIZE-1][0] = 0; M[MSIZE-1][1] = 0; M[MSIZE-1][2] = sqrt_epsilon;
//Init the L matrix. L is a sparse matrix that only contains SIZE_N2 non-zero elements per row.
//We store the L matrix in an array with one row per image pixel and one column per non-zero value.
//Each array cell accumulates values according to equation 11.
LMatrix = new double* [w*h];
for(i=0; i<w*h; i++){
LMatrix[i] = new double[SIZE_N2];
for(j=0; j<SIZE_N2; j++){
LMatrix[i][j] = 0;
}
}
//Compute the L matrix
for( y = WSIZE_2; y < h-WSIZE_2; y++) {//for each pixel...
for( x = WSIZE_2; x < w-WSIZE_2; x++) {
//fill the M matrix with the image values in the current window
for (int m=-WSIZE_2; m<=WSIZE_2; m++) {
yC = y+m;
for (int n=-WSIZE_2; n<=WSIZE_2; n++) {
xC = x+n;
M[(m+WSIZE_2)*WSIZE + n+WSIZE_2][0] = imgIn.getR(xC,yC);
M[(m+WSIZE_2)*WSIZE + n+WSIZE_2][1] = imgIn.getG(xC,yC);
M[(m+WSIZE_2)*WSIZE + n+WSIZE_2][2] = imgIn.getB(xC,yC);
}
}
//Compute the N matrix (equation 9 and 10)
/*Matrix<MSIZE,3,double> Mi(M);
Matrix<3,MSIZE,double> Mit(Mi.transpose());
Square_matrix<3,double> MitMi(Mit*Mi);
MitMi_inv = inverse_matrix(MitMi);
Square_matrix<MSIZE,double> Ni(Mi*MitMi_inv*Mit);
Ni = Ni.identity() - Ni;
Square_matrix<MSIZE,double> Nit(Ni.transpose());
Square_matrix<MSIZE,double> NitNi(Nit*Ni);*/
//in fact, Nitmp==Nit==Nit*Nitmp
Matrix<double> Mi(M, 12, 3);
Matrix<double> Mit(Mi.transpose());
Matrix<double> MitMi(Mit*Mi);
Matrix<double> MitMi_inv(MitMi.inverse());
Matrix<double> Ni(Mi*MitMi_inv*Mit);
Matrix<double> Nitmp(Ni.identity()-Ni);
Matrix<double> Nit(Nitmp.transpose());
Matrix<double> NitNi(Nit*Nitmp); /////////
//accumulate the values in the L matrix, for each couple of pixels (i,j) covered by the window
//--- i
for (int mi=-WSIZE_2; mi<=WSIZE_2; mi++) {//(mi,ni) = coordinate of pixel i in the local window
yiC = y+mi; //(xiC,yiC) = coordinate of pixel i in the image
for (int ni=-WSIZE_2; ni<=WSIZE_2; ni++) {
xiC = x+ni;
i = yiC*w+xiC; //row number in the matrix
ik = (mi+WSIZE_2)*WSIZE + ni+WSIZE_2;
//--- j
for (int mj=-WSIZE_2; mj<=WSIZE_2; mj++) {
yjC = y+mj;
for (int nj=-WSIZE_2; nj<=WSIZE_2; nj++) {
xjC = x+nj;
jk = (mj+WSIZE_2)*WSIZE + nj+WSIZE_2;
//accumulate values (equation 11)
LMatrix[i][(yiC-yjC+2*WSIZE_2)*SIZE_N+(xiC-xjC+2*WSIZE_2)] += NitNi.matrix[ik][jk];
//pixel j is at coordinate (xiC-xjC,yiC-yjC) in the SIZE_N*SIZE_N neighborhood affected by pixel i (centered at pixel i)
}
}
}
}
}
}
for (i=0; i<MSIZE; i++)
{
delete[] M[i];
M[i] = NULL;
}
delete[] M;
M = NULL;
}
//----- Gauss-Seidel iterations -----//
/*
imgScribbleAbsolute: image that stores the fixed-illumination scribbles (pixels store illumination values, alpha = true if the pixel is scribbled)
imgScribbleSimAlbedo: image that stores the constant-reflectance scribbles (pixels store scribbles ids)
imgScribbleSimShading: image that stores the constant-illumination scribbles (pixels store scribbles ids)
maxAlbedoNum: number of constant-reflectance scribble ids
maxShadingNum: number of constant-illumination scribble ids
albedoNumColors: constant-reflectance scribble ids
shadingNumColors: constant-illumination scribble ids
*/
Img CImageDecomp::solve(Img imgIn, Img &imgScribbleAbsolute, Img imgScribbleSimAlbedo, Img imgScribbleSimShading, int maxAlbedoNum, int maxShadingNum, Color *albedoNumColors, Color *shadingNumColors)
{
int w = imgIn.width();
int h = imgIn.height();
result = Img(w,h,Color(0.5,0.5,0.5)); //initial value(noted by zhll)
//result = Img(w,h,Color(1,1,1));
double rRes, gRes, bRes;//temporary result values
double denom;
double lambdaCstr = 0.5;//weight of the user constraints
int i, xjC, yjC, nbP;
//variables used to compute user constraints
double rSumI, gSumI, bSumI;//sum of images values
double rSumS, gSumS, bSumS;//sum of illumination values
double rAvrg, gAvrg, bAvrg;//average illumination
Color imgVal, resVal, scribVal;
Color* albedoAvgColors = new Color[maxAlbedoNum];//average reflectance for each constant-reflectance scribble
Color* shadingAvgColors = new Color[maxShadingNum];//average illumination for each constant-illumination scribble
Img scribbleSimAlbedo = imgScribbleSimAlbedo;
Img scribbleSimShading = imgScribbleSimShading;
Img scribbleAbsolute = imgScribbleAbsolute;
//Gauss-Seidel iterations.
//for simplicity, we apply here a fixed number of iterations
for (int k=0; k<nbIter; k++){
//-------------------------------//
//--- User constraints values ---//
//-------------------------------//
//In order to apply the user constraints, we first need to compute the average value of constant reflectance or illumination pixels
//-- Constant-reflectance
for (int c=0; c<maxAlbedoNum; c++){//for each constant-reflectance scribble id
//illumination estimation for pixel p = I(p)*sum(S)/sum(I), where S stands for illumination values of constant pixels and I for input value of constant pixels
rSumI = gSumI = bSumI = 0;
rSumS = gSumS = bSumS = 0;
nbP = 0;
for (int y = 0; y < h; y++) {//for each pixel of the scribbles image
for (int x = 0; x < w; x++) {
if(scribbleSimAlbedo.pixel(x,y)==albedoNumColors[c]){//pixel scribbled with the current constraint
//get the image values for this pixel
// imgVal = imgIn.pixel(x,y);
// resVal = result.pixel(x,y);
imgVal = imgIn.imga[y][x];
resVal = result.imga[y][x];
rSumI += imgVal.getR();
rSumS += resVal.getR();
gSumI += imgVal.getG();
gSumS += resVal.getG();
bSumI += imgVal.getB();
bSumS += resVal.getB();
nbP++;
}
}//for
}//for
if(nbP>0){//if there was some scribbled pixels
albedoAvgColors[c] = Color(rSumS/rSumI,gSumS/gSumI,bSumS/bSumI);
}//if(nbP>0){
}
//-- Constant-illumination
for (int c=0; c<maxShadingNum; c++){//for each constant-illumination scribble id
//illumination estimation = average illumination
rAvrg = gAvrg = bAvrg = 0;
nbP = 0;
for (int y = 0; y < h; y++) {//for each pixel of the scribbles image
for (int x = 0; x < w; x++) {
if(scribbleSimShading.pixel(x,y)==shadingNumColors[c]){//pixel scribbled with the current constraint
//resVal = result.pixel(x,y);
resVal = result.imga[y][x];
rAvrg += resVal.getR();
gAvrg += resVal.getG();
bAvrg += resVal.getB();
nbP++;
}
}
}
if(nbP>0){//if there was some scribbled pixels
rAvrg/=nbP;
gAvrg/=nbP;
bAvrg/=nbP;
shadingAvgColors[c] = Color(rAvrg,gAvrg,bAvrg);
}
}
//------------------------------------------------//
//--- Gauss-Seidel iteration with the L matrix ---//
//------------------------------------------------//
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
i = y*w+x; //row number for the current pixel
//is that a fixed-illumination pixel?
//imgVal = scribbleAbsolute.pixel(x,y);
imgVal = scribbleAbsolute.imga[y][x];
if(imgVal.getA()){//fixed-illumination pixel: impose the constraint
rRes = imgVal.getR();
gRes = imgVal.getG();
bRes = imgVal.getB();
}//if
else{//non scribbled pixel
//-- Gauss-Seidel update
rRes = gRes = bRes = 0;
for (int k=0; k < SIZE_N2; k++){//for each value on the matrix row corresponding to the current pixel
if(LMatrix[i][k]!=0){
//coordinate of the corresponding pixel in the image
yjC = -k/SIZE_N+2*WSIZE_2+y;
xjC = -k+(y-yjC+2*WSIZE_2)*SIZE_N+2*WSIZE_2+x;
if(x==xjC && y==yjC){//diagonal
denom = LMatrix[i][k];
}
else{
//resVal = result.pixel(xjC,yjC);
resVal = result.imga[yjC][xjC];
//relaxation
rRes -= LMatrix[i][k]*resVal.getR();
gRes -= LMatrix[i][k]*resVal.getG();
bRes -= LMatrix[i][k]*resVal.getB();
}
}//if(LMatrix[i][k]!=0)
}//for
rRes /= denom;
gRes /= denom;
bRes /= denom;
//-- User constraints updates
//check for constant-reflectance
scribVal = scribbleSimAlbedo.pixel(x,y);
if(scribVal.getA()){//constant-reflectance pixel
//get the scribble id
//int id = albedoNumColors.indexOf(QColor(scribVal));
int id = scribVal.index-1; //here is modified by zhaoll
//get the image value for this pixel
//imgVal = imgIn.pixel(x,y);
imgVal = imgIn.imga[y][x];
//illumination = I(p) * sum(S)/sum(I)
//if one channel is too small, take another one
//red
if(imgVal.getR()>EPSILON_PIXEL_VAL && albedoAvgColors[id].getR()>EPSILON_PIXEL_VAL)
rRes = lambdaCstr*rRes + (1.0-lambdaCstr)*imgVal.getR()*albedoAvgColors[id].getR();
else{
if(imgVal.getG()>EPSILON_PIXEL_VAL && albedoAvgColors[id].getG()>EPSILON_PIXEL_VAL)
rRes = lambdaCstr*gRes + (1.0-lambdaCstr)*imgVal.getG()*albedoAvgColors[id].getG();
else
rRes = lambdaCstr*bRes + (1.0-lambdaCstr)*imgVal.getB()*albedoAvgColors[id].getB();
}
//green
if(imgVal.getG()>EPSILON_PIXEL_VAL && albedoAvgColors[id].getG()>EPSILON_PIXEL_VAL)
gRes = lambdaCstr*gRes + (1.0-lambdaCstr)*imgVal.getG()*albedoAvgColors[id].getG();
else
gRes = rRes;
//blue
if(imgVal.getB()>EPSILON_PIXEL_VAL && albedoAvgColors[id].getB()>EPSILON_PIXEL_VAL)
bRes = lambdaCstr*bRes + (1.0-lambdaCstr)*imgVal.getB()*albedoAvgColors[id].getB();
else
bRes = rRes;
}//if(scribVal.getA()){
else{
//check for constant-illumination
//scribVal = scribbleSimShading[deepness-1].pixel(x,y);
scribVal = scribbleSimShading.pixel(x,y); ////////////////////////////////////
if(/*qAlpha(scribVal)>1*/scribVal.getA()){//constant-illumination pixel
//get the scribble id
//int id = shadingNumColors.indexOf(QColor(scribVal));
int id = scribVal.index-1; //by zhaoll
rRes = lambdaCstr*rRes + (1.0-lambdaCstr)*shadingAvgColors[id].getR();
double test = shadingAvgColors[id].getR();
double test2 = shadingAvgColors[id].cr;
gRes = lambdaCstr*gRes + (1.0-lambdaCstr)*shadingAvgColors[id].getG();
bRes = lambdaCstr*bRes + (1.0-lambdaCstr)*shadingAvgColors[id].getB();
}
}//else
}//else
if(rRes<EPSILON_PIXEL_VAL)
rRes = EPSILON_PIXEL_VAL;
if(gRes<EPSILON_PIXEL_VAL)
gRes = EPSILON_PIXEL_VAL;
if(bRes<EPSILON_PIXEL_VAL)
bRes = EPSILON_PIXEL_VAL;
//normalized, so is 1,
if(rRes>1.0)
rRes = 1.0;
if(gRes>1.0)
gRes = 1.0;
if(bRes>1.0)
bRes = 1.0;
result.setPixel(x,y,Color(rRes,gRes,bRes));
}//for (int x = 0; x < w; x++) {
}//for (int y = 0; y < h; y++) {
}//for (int k=0; k<nbIter; k++){
delete[] albedoAvgColors;
albedoAvgColors = NULL;
delete[] shadingAvgColors;
shadingAvgColors = NULL;
return result;
}
//image decomposition
void CImageDecomp::decompImg()
{
//in case no srcibbles
if (albedoNum==0)
{
AfxMessageBox("No saved constant reflectance!");
return;
}
if (shadingNum==0)
{
AfxMessageBox("No saved constant illumination!");
return;
}
//in case no scribbles
if (scribbleFixShadingImg==NULL)
{
scribbleFixShadingImg = new Img(imgWidth, imgHeight, Color(0,0,0,0));
}
if (scribbleAlbedoImg==NULL)
{
scribbleAlbedoImg = new Img(imgWidth, imgHeight, Color(0,0,0,0));
}
if (scribbleShadingImg==NULL)
{
scribbleShadingImg = new Img(imgWidth, imgHeight, Color(0,0,0,0));
}
Img img = *imgIn;
Img imgScribbleAbsolute = *scribbleFixShadingImg;
Img imgScribbleSimAlbedo = *scribbleAlbedoImg;
Img imgScribbleSimShading = *scribbleShadingImg;
int maxAlbedoNum = albedoNum;
int maxShadingNum = shadingNum;
//albedoNumColors are the color value of all scribbles, which is used here as scribble line ids
Color *albedoNumColors = new Color[albedoNum];
Color *shadingNumColors = new Color[shadingNum];
int k;
for (k=0; k<albedoNum; k++)
{
albedoNumColors[k] = Color(k+1, k+1, k+1);
}
for (k=0; k<shadingNum; k++)
{
shadingNumColors[k] = Color(k+1, k+1, k+1);
}
//build L matrix
buildLMatrix(img);
//gauss-jordan for minimization solution, the follwing returns the illumination of the input image
shadingImg = solve(img, imgScribbleAbsolute, imgScribbleSimAlbedo, imgScribbleSimShading, maxAlbedoNum, maxShadingNum, albedoNumColors, shadingNumColors);
/************************************************************************/
//compute the reflectance
double rI, gI, bI; //rgb component of imgIn
double rS, gS, bS; //rgb component of Shading image
double rA, gA, bA; //rgb component of albedo image, which is to be computed below
Img tmp(imgWidth, imgHeight, Color(0,0,0));
for (int j=0; j<imgHeight; j++)
{
for (int i=0; i<imgWidth; i++)
{
//img input
rI = img.getR(i, j);
gI = img.getG(i, j);
bI = img.getB(i, j);
//shading img
rS = shadingImg.getR(i, j);
gS = shadingImg.getG(i, j);
bS = shadingImg.getB(i, j);
//compute albedo img I = S*A
rA = rI/rS;
gA = gI/gS;
bA = bI/bS;
tmp.setPixel(i, j, Color(rA, gA, bA));
}
}
albedoImg = tmp;
//
/************************************************************************/
//put images to buffers
Img2buf(shadingImg, albedoImg);
// // output albedo and shading images //just for test here
// int framesize = imgHeight*imgWidth*3;
// char filename1[128];
// char filename2[128];
//
// sprintf(filename1, "d:\\albedo.rgb");
// sprintf(filename2, "d:\\shading.rgb");
//
// FILE* fp = fopen(filename1, "wb");
// fwrite(albedoImgBuf, 1, framesize, fp);
// fclose(fp);
//
// fp = fopen(filename2, "wb");
// fwrite(shadingImgBuf, 1, framesize, fp);
// fclose(fp);
//delete temporary mem
delete[] albedoNumColors;
delete[] shadingNumColors;
}
//buffer albedo and shading images
BOOL CImageDecomp::Img2buf(Img &shadingImg, Img &albedoImg)
{
//allocate memory for img buffer
if (albedoImgBuf)
delete[] albedoImgBuf;
int bbc = 24;
int bpl = ((bbc*imgWidth + 31)/32)*4;
int imagesize = bpl*imgHeight;
albedoImgBuf = new BYTE[imagesize];
if (shadingImgBuf)
delete[] shadingImgBuf;
shadingImgBuf = new BYTE[imagesize];
//
double r, g, b;
int i, j, mpv=MAX_PIX_VAL;
Color tmp;
BYTE pelR, pelG, pelB;
int sp;
//albedo img
for (j=0; j<imgHeight; j++)
{
for (i=0; i<imgWidth; i++)
{
sp = (j*imgWidth + i)*3;
//tmp = albedoImg.pixel(i, j);
tmp = albedoImg.imga[j][i];
r = tmp.cr*mpv;
g = tmp.cg*mpv;
b = tmp.cb*mpv;
// r = tmp.cr;
// g = tmp.cg;
// b = tmp.cb;
pelR = (r<0) ? 0 : ((r>mpv)?mpv:r);
pelG = (g<0) ? 0 : ((g>mpv)?mpv:g);
pelB = (b<0) ? 0 : ((b>mpv)?mpv:b);
albedoImgBuf[sp] = pelR;
albedoImgBuf[sp+1] = pelG;
albedoImgBuf[sp+2] = pelB;
}
}
HeadStand((void *)albedoImgBuf, imgHeight, imgWidth, bitDepth); //temporarily canceled
//shading img
for (j=0; j<imgHeight; j++)
{
for (i=0; i<imgWidth; i++)
{
sp = (j*imgWidth + i)*3;
// tmp = shadingImg.pixel(i, j);
tmp = shadingImg.imga[j][i];
r = tmp.cr*mpv;
g = tmp.cg*mpv;
b = tmp.cb*mpv;
// r = tmp.cr;
// g = tmp.cg;
// b = tmp.cb;
pelR = (r<0) ? 0 : ((r>mpv)?mpv:r);
pelG = (g<0) ? 0 : ((g>mpv)?mpv:g);
pelB = (b<0) ? 0 : ((b>mpv)?mpv:b);
shadingImgBuf[sp] = pelR;
shadingImgBuf[sp+1] = pelG;
shadingImgBuf[sp+2] = pelB;
}
}
HeadStand((void *)shadingImgBuf, imgHeight, imgWidth, bitDepth); //temporarily canceled
return true;
}
//get the albedo image buffer
void * CImageDecomp::GetAlbedoBuf()
{
return (void *)albedoImgBuf;
}
//get the shading image buffer
void * CImageDecomp::GetShadingBuf()
{
return (void *)shadingImgBuf;
}