forked from yangyangHu/LipSegmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLipSeg.cpp
More file actions
1209 lines (1101 loc) · 33.6 KB
/
Copy pathLipSeg.cpp
File metadata and controls
1209 lines (1101 loc) · 33.6 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
/**********************************************************************************
*
* 嘴唇分割LipSegmentation
* Robust Lip Segmentation Based on Complexion Mixture Model
* by Hu yangyang 2016/12/23
*
***********************************************************************************/
#include "LipSeg.h"
#include "RemoveNoise.h"
LipSegmentation::LipSegmentation(const IplImage* face):nGMM(5)
{
DynamicScale(face);
mComplexionGMM = NULL;
faceDwn = NULL;
faceDwn_Gauss = NULL;
lipExtractMask = NULL;
lipExtractImage = NULL;
lipRawMask = NULL;
lipImage = NULL;
}
LipSegmentation::~LipSegmentation()
{
//release
if(face != NULL) cvReleaseImage(&face);
if(mComplexionGMM != NULL) delete mComplexionGMM;
if(faceDwn != NULL) cvReleaseImage(&faceDwn);
if(faceDwn_Gauss != NULL) cvReleaseImage(&faceDwn_Gauss);
if(lipExtractMask != NULL) cvReleaseImage(&lipExtractMask);
if(lipExtractImage != NULL) cvReleaseImage(&lipExtractImage);
if(lipRawMask != NULL) cvReleaseImage(&lipRawMask);
if(lipImage != NULL) cvReleaseImage(&lipImage);
}
//输入图像尺寸动态调整
void LipSegmentation::DynamicScale(const IplImage* inputImage)
{
int size;
size = (inputImage->height > inputImage->width) ? inputImage->height : inputImage->width;
double type = ((double)size)/600.0;
if (type<=1)
{
face = cvCloneImage(inputImage);
}
else
{
face = cvCreateImage(cvSize(cvRound(inputImage->width/type),cvRound(inputImage->height/type)),inputImage->depth,inputImage->nChannels);
cvResize(inputImage,face);
}
//cvShowImage("face",face);
}
bool LipSegmentation::ProcessFlow()
{
BuildComplexionGMM();
ComputeComplexionProbabilityMap();
CvRect rect_Lip = cvRect(0,0,0,0);
IplImage* image_LipBi = NULL;
if(!DetectLipRegion(image_LipBi,rect_Lip))
{
return false;//嘴唇检测失败
}
//cvShowImage("BIII",image_LipBi);
OptimizationByGMMs(image_LipBi,rect_Lip);
LipContourExtract(rect_Lip.width/10);
//release
cvReleaseImage(&image_LipBi);
return true;//嘴唇检测成功
}
CvScalar LipSegmentation::ExtractLipColorFeature()
{
//获得灰度图像
IplImage* lipImageGray = cvCreateImage(cvGetSize(lipImage),8,1);
cvCvtColor(lipImage,lipImageGray, CV_BGR2GRAY);
//cvShowImage("Lgray",lipImageGray);
//进一步去除一些明暗的像素
int i_dark,i_bright;
double rate_dark = 0.2, rate_bright = 0.02;
calculateDarkAndBrightThreshold(lipRawMask,lipImageGray,i_dark,i_bright,rate_dark,rate_bright,0);
for (int y=0;y<lipRawMask->height;++y)
{
for (int x=0;x<lipRawMask->width;++x)
{
if (cvGetReal2D(lipImageGray,y,x)<i_dark || cvGetReal2D(lipImageGray,y,x)>i_bright)
{
cvSetReal2D(lipRawMask,y,x,0);
}
}
}
cvErode(lipRawMask,lipRawMask);
RemoveNoise rn;
rn.LessConnectedRegionRemove(lipRawMask,lipRawMask->height*lipRawMask->width/30);
//cvShowImage("lipRawMaskFine",lipRawMask);
//convert to Lab
IplImage* lipImage_Lab = cvCreateImage(cvGetSize(lipImage),8,3);
cvCvtColor(lipImage,lipImage_Lab, CV_BGR2Lab);
CvScalar lipColorFeature = cvAvg(lipImage_Lab,lipRawMask);
//release
cvReleaseImage(&lipImageGray);
cvReleaseImage(&lipImage_Lab);
return lipColorFeature;
}
//以上半脸和下部分脸中皮肤为训练样本建立肤色混合高斯模型
void LipSegmentation::BuildComplexionGMM()
{
//缩放人脸,以加快GMM训练速度
int len;
len = (face->height > face->width) ? face->height : face->width;
double type = ((double)len)/60.0;
IplImage* faceScale = cvCreateImage(cvSize(cvRound(face->width/type),cvRound(face->height/type)),face->depth,face->nChannels);
cvResize(face,faceScale);
//cvShowImage("faceScale",faceScale);
//获得灰度图像
IplImage* faceImageScaleGray = cvCreateImage(cvGetSize(faceScale),8,1);
cvCvtColor(faceScale,faceImageScaleGray, CV_BGR2GRAY);
//cvShowImage("gray",faceImageScaleGray);
//获取椭圆脸
IplImage* faceScaleEllipseMask = cvCreateImage(cvGetSize(faceScale),8,1);
cvZero(faceScaleEllipseMask);
CvPoint center = cvPoint(cvRound(faceScaleEllipseMask->width*0.5),cvRound(faceScaleEllipseMask->height*0.5));
CvSize size = cvSize(cvRound(faceScaleEllipseMask->width*0.36),cvRound(faceScaleEllipseMask->height*0.50));
cvEllipse(faceScaleEllipseMask,center,size,0,0,360,cvScalar(255),CV_FILLED);//用来截取椭圆形的脸型
//cvShowImage("ellipse",faceScaleEllipseMask);
//去除下部分脸的嘴唇、胡子等非肤色噪声
int i_dark,i_bright;
double rate_dark = 0.2, rate_bright = 0;
float lowPart = 0.65;
calculateDarkAndBrightThreshold(faceScaleEllipseMask,faceImageScaleGray,i_dark,i_bright,rate_dark,rate_bright,lowPart);
for (int y=faceScaleEllipseMask->height*lowPart;y<faceScaleEllipseMask->height;++y)
{
for (int x=0;x<faceScaleEllipseMask->width;++x)
{
if (cvGetReal2D(faceImageScaleGray,y,x)<i_dark)
{
cvSetReal2D(faceScaleEllipseMask,y,x,0);
}
}
}
cvErode(faceScaleEllipseMask,faceScaleEllipseMask);//erode
//cvShowImage("ellipseFine",faceScaleEllipseMask);
//取Lab颜色空间
IplImage* faceImageScale_Lab = NULL;
faceImageScale_Lab = cvCreateImage(cvGetSize(faceScale),8,3);
cvCvtColor(faceScale,faceImageScale_Lab,CV_BGR2Lab); //设置颜色空间
//以上部分脸和下部分脸中皮肤作为训练数据
//准备训练数据
uint cnt = 0,nrows = 0;
for (int y=0;y<faceScaleEllipseMask->height;y++)
{
for (int x=0;x<faceScaleEllipseMask->width;x++)
{
if (y<=faceScaleEllipseMask->height*2/3)
{
nrows++;
}
else
{
if (cvGetReal2D(faceScaleEllipseMask,y,x)>200) nrows++;
}
}
}
double** data ;
data = (double**)malloc(nrows*sizeof(double*));
for (int i = 0; i < nrows; i++) data[i] = (double*)malloc(3*sizeof(double));//设置为三维高斯模型
// copy the data from the color array to a temp array
// and assin each sample a random cluster id
for (int y=0;y<faceScaleEllipseMask->height;y++)
{
for (int x=0;x<faceScaleEllipseMask->width;x++)
{
if (y<=faceScaleEllipseMask->height*2/3)
{
data[cnt][0] = cvGet2D(faceImageScale_Lab,y,x).val[0];//设置颜色空间的三分量用于高斯建模
data[cnt][1] = cvGet2D(faceImageScale_Lab,y,x).val[1];
data[cnt++][2] = cvGet2D(faceImageScale_Lab,y,x).val[2];
}
else
{
if (cvGetReal2D(faceScaleEllipseMask,y,x)>200)
{
data[cnt][0] = cvGet2D(faceImageScale_Lab,y,x).val[0];//设置颜色空间的三分量用于高斯建模
data[cnt][1] = cvGet2D(faceImageScale_Lab,y,x).val[1];
data[cnt++][2] = cvGet2D(faceImageScale_Lab,y,x).val[2];
}
}
}
}
mComplexionGMM = new GMM(nGMM);
mComplexionGMM->Build(data,nrows);
for (int i = 0; i < nrows; i++) free(data[i]);
free(data);
//release
cvReleaseImage(&faceScale);
cvReleaseImage(&faceImageScaleGray);
cvReleaseImage(&faceScaleEllipseMask);
cvReleaseImage(&faceImageScale_Lab);
}
//利用肤色GMM求出下半脸的肤色概率图
void LipSegmentation::ComputeComplexionProbabilityMap()
{
//获取下半脸
cvSetImageROI(face,cvRect(0,face->height/2,face->width,face->height/2));
faceDwn = cvCreateImage(cvGetSize(face),8,3);
cvCopy(face,faceDwn);
cvResetImageROI(face);
//cvShowImage("faceDwn",faceDwn);
//计算下半脸的肤色概率
IplImage* faceDwn_Lab = NULL;
faceDwn_Lab = cvCreateImage(cvGetSize(faceDwn),8,3);
cvCvtColor(faceDwn,faceDwn_Lab,CV_BGR2Lab);//设置颜色空间
faceDwn_Gauss = cvCreateImage(cvGetSize(faceDwn),IPL_DEPTH_64F,1);
cvZero(faceDwn_Gauss);
for (int y=0;y<faceDwn_Gauss->height;y++)
{
for (int x=0;x<faceDwn_Gauss->width;x++)
{
CvScalar pixel = cvGet2D(faceDwn_Lab,y,x);
Color c(pixel.val[0],pixel.val[1],pixel.val[2]);//三维高斯模型的测试数据
float px = mComplexionGMM->p(c);
cvSetReal2D(faceDwn_Gauss,y,x,px);
}
}
cvNormalize(faceDwn_Gauss,faceDwn_Gauss,1.0,0.0,CV_C);
//cvShowImage("ComplexionProbabilityOriginal",faceDwn_Gauss);
cvSmooth(faceDwn_Gauss,faceDwn_Gauss,CV_GAUSSIAN,5,5);//改进
//cvShowImage("ComplexionProbabilityFine",faceDwn_Gauss);
//release
cvReleaseImage(&faceDwn_Lab);
}
void LipSegmentation::OptimizationByGMMs(const IplImage* image_LipBi,const CvRect rect_Lip)
{
cvSetImageROI(faceDwn,rect_Lip);
lipImage = cvCreateImage(cvGetSize(faceDwn),8,3);
cvCopy(faceDwn,lipImage);
cvResetImageROI(faceDwn);
IplImage* lipBi = cvCloneImage(image_LipBi);
//cvShowImage("lip",lipImage);
//cvShowImage("lipBi",lipBi);
//缩放嘴唇图像,以加快GMM训练速度
int len;
len = (lipImage->height > lipImage->width) ? lipImage->height : lipImage->width;
double type = ((double)len)/50.0;
IplImage* lipImageScale = cvCreateImage(cvSize(cvRound(lipImage->width/type),cvRound(lipImage->height/type)),lipImage->depth,lipImage->nChannels);
cvResize(lipImage,lipImageScale);
//cvShowImage("lipImageScale",lipImageScale);
IplImage* lipBiScale = cvCreateImage(cvSize(cvRound(lipBi->width/type),cvRound(lipBi->height/type)),lipBi->depth,lipBi->nChannels);
cvResize(lipBi,lipBiScale);
cvThreshold(lipBiScale, lipBiScale, 0, 255, CV_THRESH_BINARY| CV_THRESH_OTSU);//可考虑采用大津阈值
//cvShowImage("lipBiScale",lipBiScale);
//对嘴唇作为训练样本建立高斯模型
IplImage* lipScale_Lab = NULL;
lipScale_Lab = cvCreateImage(cvGetSize(lipImageScale),8,3);
cvCvtColor(lipImageScale,lipScale_Lab,CV_BGR2Lab);//设置颜色空间
uint cnt1 = 0,nrows1 = 0;
double** data1 ;
//nrows = faceTop_ycbcr->width*faceTop_ycbcr->height;
for (int y=0;y<lipBiScale->height;y++)
{
for (int x=0;x<lipBiScale->width;x++)
{
if (cvGetReal2D(lipBiScale,y,x)>200)
{
nrows1++;
}
}
}
data1 = (double**)malloc(nrows1*sizeof(double*));
for (int i = 0; i < nrows1; i++) data1[i] = (double*)malloc(3*sizeof(double));//设置三维GMM
// copy the data from the color array to a temp array
// and assin each sample a random cluster id
for (int y=0;y<lipBiScale->height;y++)
{
for (int x=0;x<lipBiScale->width;x++)
{
if (cvGetReal2D(lipBiScale,y,x)>200)
{
data1[cnt1][0] = cvGet2D(lipScale_Lab,y,x).val[0];
data1[cnt1][1] = cvGet2D(lipScale_Lab,y,x).val[1];
data1[cnt1++][2] = cvGet2D(lipScale_Lab,y,x).val[2];
}
}
}
GMM* mComplexionGMM1 = new GMM(3);
mComplexionGMM1->Build(data1,nrows1);
for (int i = 0; i < nrows1; i++) free(data1[i]);
free(data1);
//对嘴唇周围的皮肤作为训练样本建立高斯模型
uint cnt2 = 0,nrows2 = 0;
double** data2 ;
//nrows = faceTop_ycbcr->width*faceTop_ycbcr->height;
for (int y=0;y<lipBiScale->height;y++)
{
for (int x=0;x<lipBiScale->width;x++)
{
if (cvGetReal2D(lipBiScale,y,x)<100)
{
nrows2++;
}
}
}
data2 = (double**)malloc(nrows2*sizeof(double*));
for (int i = 0; i < nrows2; i++) data2[i] = (double*)malloc(3*sizeof(double));//三维GMM
// copy the data from the color array to a temp array
// and assin each sample a random cluster id
for (int y=0;y<lipBiScale->height;y++)
{
for (int x=0;x<lipBiScale->width;x++)
{
if (cvGetReal2D(lipBiScale,y,x)<100)
{
data2[cnt2][0] = cvGet2D(lipScale_Lab,y,x).val[0];
data2[cnt2][1] = cvGet2D(lipScale_Lab,y,x).val[1];
data2[cnt2++][2] = cvGet2D(lipScale_Lab,y,x).val[2];
}
}
}
GMM* mComplexionGMM2 = new GMM(3);
mComplexionGMM2->Build(data2,nrows2);
for (int i = 0; i < nrows2; i++) free(data2[i]);
free(data2);
//使用高斯模型来处理下部分脸
IplImage* lip_Lab = NULL;
lip_Lab = cvCreateImage(cvGetSize(lipImage),8,3);
cvCvtColor(lipImage,lip_Lab,CV_BGR2Lab);//设置颜色空间
IplImage* lip_Gauss = cvCreateImage(cvGetSize(lip_Lab),IPL_DEPTH_64F,1);
cvZero(lip_Gauss);
for (int y=0;y<lip_Gauss->height;y++)
{
for (int x=0;x<lip_Gauss->width;x++)
{
CvScalar pixel = cvGet2D(lip_Lab,y,x);
Color c(pixel.val[0],pixel.val[1],pixel.val[2]);
float px_f = mComplexionGMM1->p(c);
float px_b = mComplexionGMM2->p(c);
float px = px_b/(px_f+px_b);
cvSetReal2D(lip_Gauss,y,x,px);
}
}
cvNormalize(lip_Gauss,lip_Gauss,1.0,0.0,CV_C);
//cvShowImage("lip_Gauss",lip_Gauss);
cvSmooth(lip_Gauss,lip_Gauss,CV_GAUSSIAN,5,5);//改进
//cvShowImage("lip_GaussFine",lip_Gauss);
IplImage* lip_similar = cvCreateImage(cvGetSize(lip_Gauss),8,1);
cvScale(lip_Gauss,lip_similar,255);
double biThreshold = cvThreshold(lip_similar,lip_similar,200,255,CV_THRESH_BINARY_INV|CV_THRESH_OTSU);
//InitLipMakRefine(lip_similar);
RemoveNoise rn;
rn.LessConnectedRegionRemove(lip_similar,lip_similar->height*lip_similar->width/20);
//cvShowImage("lip_similar",lip_similar);
lipRawMask = cvCloneImage(lip_similar);
//重映像到下半脸,获取下部分脸中嘴唇的mask
lipExtractMask = cvCreateImage(cvGetSize(faceDwn),8,1);
cvZero(lipExtractMask);
for (int y=0;y<lip_similar->height;y++)
{
for (int x=0;x<lip_similar->width;x++)
{
int pixel = cvGetReal2D(lip_similar,y,x);
cvSetReal2D(lipExtractMask,rect_Lip.y+y,rect_Lip.x+x,pixel);
}
}
cvSmooth(lipExtractMask,lipExtractMask,CV_MEDIAN,5);//中值滤波去除椒盐噪声,使嘴唇边缘平滑
//cvShowImage("faceDwnMask",lipExtractMask);
//release
cvReleaseImage(&lipBi);
cvReleaseImage(&lipImageScale);
cvReleaseImage(&lipBiScale);
cvReleaseImage(&lipScale_Lab);
delete mComplexionGMM1;
delete mComplexionGMM2;
cvReleaseImage(&lip_Lab);
cvReleaseImage(&lip_Gauss);
cvReleaseImage(&lip_similar);
}
//迭代检测嘴唇
bool LipSegmentation::DetectLipRegion(IplImage*& image_LipBi,CvRect& rect_Lip)
{
//IplImage* faceDwnCpy = cvCloneImage(faceDwn);
IplImage* faceDwn_Gray = cvCreateImage(cvGetSize(faceDwn),8,1);
cvZero(faceDwn_Gray);
cvAddS(faceDwn_Gray,cvScalar(255),faceDwn_Gray);
double iterate_Threshold[16]={0.3,0.2,0.1,0.09,0.08,0.07,0.06,
0.05,0.04,0.03,0.02,0.01,0.009,0.007,0.005,0.003};
//粗略确定嘴唇中心点所在区域
//cout<<"--嘴唇粗定位--"<<endl;
CvRect lip_region = cvRect(0,0,0,0);
bool flag_Lip = false;
for (int i = 6;i<16;i++)
{
//IplImage* face_down2 = cvCloneImage(faceDwn);
IplImage* faceDown_Gray2 = cvCloneImage(faceDwn_Gray);
for (int y=0;y<faceDwn_Gauss->height;y++)
{
for (int x=0;x<faceDwn_Gauss->width;x++)
{
if(cvGetReal2D(faceDwn_Gauss,y,x)>=iterate_Threshold[i])
{
//cvSet2D(face_down2,y,x,CV_RGB(255,255,255));
cvSetReal2D(faceDown_Gray2,y,x,0);
}
}
}
//cout<<"--"<<i<<"--"<<endl;
//cvShowImage("itface",face_down2);
//cvShowImage("itfaceBI",faceDown_Gray2);
//cvWaitKey(0);
//寻找初嘴唇
IplImage* faceDown_GrayT = cvCloneImage(faceDown_Gray2);
lip_region = LipLocate(faceDown_GrayT,flag_Lip);
cvReleaseImage(&faceDown_GrayT);
cvReleaseImage(&faceDown_Gray2);
//cvReleaseImage(&face_down2);
if (flag_Lip)
{
/*lip_region.y -= lip_region.height/2;
lip_region.height += lip_region.height*1/2;
lip_region.x -= lip_region.width/2;
lip_region.width += lip_region.width;*/
break;
}
}
if (!flag_Lip)
{
for (int i = 5;i>=0;i--)
{
//IplImage* face_down2 = cvCloneImage(faceDwn);
IplImage* faceDown_Gray2 = cvCloneImage(faceDwn_Gray);
for (int y=0;y<faceDwn_Gauss->height;y++)
{
for (int x=0;x<faceDwn_Gauss->width;x++)
{
if(cvGetReal2D(faceDwn_Gauss,y,x)>=iterate_Threshold[i])
{
//cvSet2D(face_down2,y,x,CV_RGB(255,255,255));
cvSetReal2D(faceDown_Gray2,y,x,0);
}
}
}
//cout<<"--"<<i<<"--"<<endl;
//cvShowImage("itface",face_down2);
//cvShowImage("itfaceBI",faceDown_Gray2);
//cvWaitKey(0);
//寻找初嘴唇
IplImage* faceDown_GrayT = cvCloneImage(faceDown_Gray2);
lip_region = LipLocate(faceDown_GrayT,flag_Lip);
cvReleaseImage(&faceDown_GrayT);
cvReleaseImage(&faceDown_Gray2);
//cvReleaseImage(&face_down2);
if (flag_Lip)
{
/*lip_region.y -= lip_region.height/2;
lip_region.height += lip_region.height*1/2;
lip_region.x -= lip_region.width/2;
lip_region.width += lip_region.width;*/
break;
}
}
}
//嘴唇初定位异常,异常处理
if (!flag_Lip)
{
//cout<<"嘴唇初定位异常!"<<endl;
return flag_Lip;
}
flag_Lip = false;
//精确确定嘴唇区域
//cout<<"--嘴唇精定位--"<<endl;
int it;
for (it=0;it<16;it=it+1)
{
for (int y=0;y<faceDwn_Gauss->height;y++)
{
for (int x=0;x<faceDwn_Gauss->width;x++)
{
if(cvGetReal2D(faceDwn_Gauss,y,x)>=iterate_Threshold[it])
{
//cvSet2D(faceDwnCpy,y,x,CV_RGB(255,255,255));
cvSetReal2D(faceDwn_Gray,y,x,0);
}
}
}
//cout<<"--"<<it<<"--"<<endl;
//cvShowImage("itface",faceDwnCpy);
//cvShowImage("itfaceBI",faceDwn_Gray);
//cvWaitKey(0);
//寻找初嘴唇
IplImage* faceDown_GrayT = cvCloneImage(faceDwn_Gray);
rect_Lip = LipAccurateLocate(faceDown_GrayT,flag_Lip,lip_region);
cvReleaseImage(&faceDown_GrayT);
if (flag_Lip)
{
break;
}
}
//嘴唇初定位异常,异常处理
if (!flag_Lip)
{
//cout<<"嘴唇初定位异常!"<<endl;
return flag_Lip;
}
cvSetImageROI(faceDwn_Gray,rect_Lip);
IplImage* image_Lip = cvCreateImage(cvGetSize(faceDwn_Gray),8,1);
cvCopy(faceDwn_Gray,image_Lip);
cvResetImageROI(faceDwn_Gray);
cvSetImageROI(faceDwn_Gauss,rect_Lip);
IplImage* image_Gauss = cvCreateImage(cvGetSize(faceDwn_Gauss),faceDwn_Gauss->depth,1);
cvCopy(faceDwn_Gauss,image_Gauss);
cvResetImageROI(faceDwn_Gauss);
//继续迭代优化
//cout<<"--继续迭代优化--"<<endl;
image_LipBi = cvCloneImage(image_Lip);
int s1,s2,sd;
s1 = LipLocateRefine(image_Lip);
int sdFlagPre,sdFlagCur;
int sdItPre = it,sdItCur = it;
int sdPre,sdCur;
int continuousIncreaseCnt = 0;
int continuousDecreaseCnt = 0;
int nLipRefine;
for (int i = it+1;i<16;i++)
{
for (int y=0;y<image_Lip->height;y++)
{
for (int x=0;x<image_Lip->width;x++)
{
if(cvGetReal2D(image_Gauss,y,x)>=iterate_Threshold[i])
{
//cvSet2D(faceDwnCpy,rect_Lip.y+y,rect_Lip.x+x,CV_RGB(255,255,255));
cvSetReal2D(image_Lip,y,x,0);
}
}
}
//cout<<"--"<<i<<"--"<<endl;
//cvShowImage("itface",faceDwnCpy);
//cvShowImage("itLipBI",image_Lip);
//cvWaitKey(0);
s2 = LipLocateRefine(image_Lip);
sd = s1 - s2;
s1 = s2;
if (i==it+1)
{
sdPre = sd;
sdFlagPre = 0;
sdItPre = i;
sdItCur = i;
}
else
{
sdCur = sd;
sdItCur = i;
if (sdCur-sdPre>0)//单调递增
{
sdFlagCur = INCREASE;
++continuousIncreaseCnt;
continuousDecreaseCnt = 0;
}
else //单调递减
{
sdFlagCur = DECREASE;
continuousIncreaseCnt = 0;
++continuousDecreaseCnt;
}
if (continuousIncreaseCnt>=3||continuousDecreaseCnt>=3)//连续多次上升或者下降,使嘴唇丢失太多,停止迭代
{
break;
}
if ((sdFlagPre == DECREASE)&&(sdFlagCur == INCREASE))//寻找第一个局部最小值点
{
//if (sdPre<=750)//突变点sd值<=700->750 改进10
//{
break;
//}
//else
//{
//sdPre = sdCur;//更新
//sdItPre = sdItCur;
//sdFlagPre = sdFlagCur;
//}
}
else
{
sdPre = sdCur;//更新
sdItPre = sdItCur;
sdFlagPre = sdFlagCur;
}
}
//判断嘴唇能否检测到
{
IplImage* faceDownGrayRefine = cvCreateImage(cvGetSize(faceDwn),8,1);
cvZero(faceDownGrayRefine);
for (int y=0;y<image_Lip->height;y++)
{
for (int x=0;x<image_Lip->width;x++)
{
int pixel = cvGetReal2D(image_Lip,y,x);
cvSetReal2D(faceDownGrayRefine,rect_Lip.y+y,rect_Lip.x+x,pixel);
}
}
nLipRefine = LipDetect(faceDownGrayRefine);//获取疑似嘴唇的个数
if (nLipRefine == 0)//未检测到嘴唇,则返回上一步迭代的嘴唇
{
if (sdItPre>0)
{
sdItPre--;
}
sdItCur = (it+sdItPre)/2;
break;
}
cvReleaseImage(&faceDownGrayRefine);
}
}
//局部最优值的嘴唇
double T = 0.0;
if (nLipRefine==0)
{
T = (iterate_Threshold[sdItPre] + iterate_Threshold[sdItCur]/*+iterate_Threshold[it]*/)/2;
}
else
{
T = (iterate_Threshold[sdItPre] + iterate_Threshold[sdItPre]/*+iterate_Threshold[it]*/)/2;
}
for (int y=0;y<image_LipBi->height;y++)
{
for (int x=0;x<image_LipBi->width;x++)
{
if(cvGetReal2D(image_Gauss,y,x)>=T)
{
cvSetReal2D(image_LipBi,y,x,0);
}
}
}
cvSmooth(image_LipBi,image_LipBi,CV_MEDIAN);//中值滤波去除椒盐噪声
//cvMorphologyEx(image_LipBi,image_LipBi,NULL,NULL,CV_MOP_CLOSE,1);
//去除粗嘴唇mask中杂质,只留下嘴唇粗mask
InitLipMakRefine(image_LipBi);
//cvShowImage("lipRegion",image_LipBi);
//release
cvReleaseImage(&faceDwn_Gray);
//cvReleaseImage(&faceDwnCpy);
cvReleaseImage(&image_Lip);
cvReleaseImage(&image_Gauss);
return flag_Lip;
}
void LipSegmentation::LipContourExtract(const int maxStep)
{
//去除突出杂质
IplImage* lipContourAnd = FivePointAverage(lipExtractMask,5);
FillContour(lipContourAnd);
for (int num=10;num<=maxStep;num=num+5)
{
IplImage* lipContour_num = FivePointAverage(lipExtractMask,num);
FillContour(lipContour_num);
//and运算
cvAnd(lipContourAnd,lipContour_num,lipContourAnd);
cvReleaseImage(&lipContour_num);
}
InitLipMakRefine(lipContourAnd);
//cvShowImage("and",lipContourAnd);
//填补内凹
IplImage* lipContourOr = FivePointAverage(lipContourAnd,5);
FillContour(lipContourOr);
//cvShowImage("or",lipContourOr);
for (int num=5;num<=maxStep;num=num+5)
{
IplImage* lipContour_num = FivePointAverage(lipContourAnd,num);
FillContour(lipContour_num);
//or运算
cvOr(lipContourOr,lipContour_num,lipContourOr);
cvReleaseImage(&lipContour_num);
}
//cvShowImage("or",lipContourOr);
//更新嘴唇lipExtractMask,获得最优嘴唇mask
cvReleaseImage(&lipExtractMask);
lipExtractMask = cvCloneImage(lipContourOr);
//cvShowImage("LipMask",lipExtractMask);
//分割出嘴唇
SegLip();
//cvShowImage("lipExtract",lipExtractImage);
//release
cvReleaseImage(&lipContourAnd);
cvReleaseImage(&lipContourOr);
}
//嘴唇初定位
CvRect LipSegmentation::LipLocate(IplImage* faceDown_Gray,bool &falg_Lip)
{
CvSeq *pContour = NULL;
CvMemStorage *pStorage = cvCreateMemStorage(0);
int n=cvFindContours(faceDown_Gray,pStorage,&pContour,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
CvRect lipRect = cvRect(0,0,0,0);
int nlip = 0;//疑似嘴唇的个数
for(;pContour!=NULL;pContour=pContour->h_next)
{
int area=(int)cvContourArea(pContour);
CvRect rect=cvBoundingRect(pContour);//计算点集的最外面(up-right)矩形边界
double ratio_WH = 0;//外界矩形的宽高比
double ratio_FILL = 0;//目标填充率
double ratio_AREA = 0;//目标区域面积与整个图像面积之比
CvPoint center;
ratio_WH = ((double)(rect.width))/((double)(rect.height));
ratio_FILL = ((double)area)/((double)(rect.width*rect.height));
ratio_AREA = ((double)area)/((double)(faceDown_Gray->width*faceDown_Gray->height));
center.x = rect.x+rect.width/2;
center.y = rect.y+rect.height/2;
if ((ratio_WH>1)&&(ratio_WH<=6)&&(ratio_FILL>=0.3)&&(ratio_FILL<1)&&(ratio_AREA>=0.02)&&(ratio_AREA<=0.15)&&(center.x>=faceDown_Gray->width/3)&&(center.x<=faceDown_Gray->width*2/3)&&(center.y>=faceDown_Gray->height/3))
{
lipRect = rect;
nlip++;
}
}
if (nlip == 1)
{
falg_Lip = true;//找到嘴唇
}
else
{
falg_Lip = false;//未找到嘴唇
}
//释放内存
cvReleaseMemStorage(&pStorage);
return lipRect;
}
//嘴唇精确定位
CvRect LipSegmentation::LipAccurateLocate(IplImage* faceDown_Gray,bool &falg_Lip,CvRect lip_region)
{
CvSeq *pContour = NULL;
CvMemStorage *pStorage = cvCreateMemStorage(0);
int n=cvFindContours(faceDown_Gray,pStorage,&pContour,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
CvRect lipRect = cvRect(0,0,0,0);
int nlip = 0;//疑似嘴唇的个数
for(;pContour!=NULL;pContour=pContour->h_next)
{
int area=(int)cvContourArea(pContour);
CvRect rect=cvBoundingRect(pContour);//计算点集的最外面(up-right)矩形边界
double ratio_WH = 0;//外界矩形的宽高比
double ratio_FILL = 0;//目标填充率
double ratio_AREA = 0;//目标区域面积与整个图像面积之比
CvPoint center;
ratio_WH = ((double)(rect.width))/((double)(rect.height));
ratio_FILL = ((double)area)/((double)(rect.width*rect.height));
ratio_AREA = ((double)area)/((double)(faceDown_Gray->width*faceDown_Gray->height));
center.x = rect.x+rect.width/2;
center.y = rect.y+rect.height/2;
if ((ratio_WH>1)&&(ratio_WH<=6)&&(ratio_FILL>=0.3)&&(ratio_FILL<1)&&(ratio_AREA>=0.02)&&(ratio_AREA<=0.15)&&(center.x>=faceDown_Gray->width/3)&&(center.x<=faceDown_Gray->width*2/3)&&(center.y>=faceDown_Gray->height/3)&&(center.y<=lip_region.y+lip_region.height)&&(center.y>=lip_region.y)&&(center.x<=lip_region.x+lip_region.width)&&(center.x>=lip_region.x))
{
lipRect = rect;
nlip++;
}
}
if (nlip == 1)
{
falg_Lip = true;//找到嘴唇
}
else
{
falg_Lip = false;//未找到嘴唇
}
//释放内存
cvReleaseMemStorage(&pStorage);
return lipRect;
}
//优化嘴唇
int LipSegmentation::LipLocateRefine(IplImage* image_lip)
{
int s = 0;
for (int y=0;y<image_lip->height;y++)
{
for (int x=0;x<image_lip->width;x++)
{
if (cvGetReal2D(image_lip,y,x)>200)
{
s++;
}
}
}
return s;
}
//嘴唇检测,用于嘴唇优化时检测嘴唇是否存在
int LipSegmentation::LipDetect(IplImage* faceDown_Gray)
{
CvSeq *pContour = NULL;
CvMemStorage *pStorage = cvCreateMemStorage(0);
int n=cvFindContours(faceDown_Gray,pStorage,&pContour,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
int nlip = 0;//疑似嘴唇的个数
for(;pContour!=NULL;pContour=pContour->h_next)
{
int area=(int)cvContourArea(pContour);
CvRect rect=cvBoundingRect(pContour);//计算点集的最外面(up-right)矩形边界
double ratio_WH = 0;//外界矩形的宽高比
double ratio_FILL = 0;//目标填充率
double ratio_AREA = 0;//目标区域面积与整个图像面积之比
CvPoint center;
ratio_WH = ((double)(rect.width))/((double)(rect.height));
ratio_FILL = ((double)area)/((double)(rect.width*rect.height));
ratio_AREA = ((double)area)/((double)(faceDown_Gray->width*faceDown_Gray->height));
if ((ratio_WH>1)&&(ratio_WH<=6)&&(ratio_FILL>=0.25)&&(ratio_FILL<1)&&(ratio_AREA>=0.022)&&(ratio_AREA<=0.15))
{
nlip++;
}
}
//释放内存
cvReleaseMemStorage(&pStorage);
return nlip;
}
//去除粗嘴唇mask中杂质干扰,得到唯一嘴唇mask
void LipSegmentation::InitLipMakRefine(IplImage* mask)
{
IplImage* mask1 = cvCloneImage(mask);
IplImage* mask2 = cvCloneImage(mask);
//确定嘴唇区域
CvSeq *pContour1 = NULL;
CvMemStorage *pStorage1 = cvCreateMemStorage(0);
int n1=cvFindContours(mask1,pStorage1,&pContour1,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
int areaMax = 0;
for(;pContour1!=NULL;pContour1=pContour1->h_next)
{
int area=(int)cvContourArea(pContour1);
if (area>areaMax)
{
areaMax = area;
}
}
//去除面积较小的轮廓
CvSeq *pContour2 = NULL;
CvMemStorage *pStorage2 = cvCreateMemStorage(0);
int n2=cvFindContours(mask2,pStorage2,&pContour2,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
for(;pContour2!=NULL;pContour2=pContour2->h_next)
{
int area=(int)cvContourArea(pContour2);
CvRect rect=cvBoundingRect(pContour2);//计算点集的最外面(up-right)矩形边界
if (area<areaMax)
{
cvSetImageROI(mask,rect);
cvSetZero(mask);
cvResetImageROI(mask);
}
}
//释放内存
cvReleaseMemStorage(&pStorage1);
cvReleaseMemStorage(&pStorage2);
cvReleaseImage(&mask1);
cvReleaseImage(&mask2);
}
//五点平均法,平滑嘴唇边缘
IplImage* LipSegmentation::FivePointAverage(IplImage* mask,int step)//mask为嘴唇掩膜(唯一轮廓,无内空洞),step为步长
{
IplImage* lipContour = cvCreateImage(cvGetSize(mask),8,1);
cvZero(lipContour);
/***********对上嘴唇的上轮廓,从左往右每隔step个点取一个点***************/
CvPoint lipTopStartPoint = cvPoint(0,0),lipTopEndPoint = cvPoint(0,0);
//求上嘴唇上轮廓最左边的点
for (int x=0;x<mask->width;x++)
{
bool flag = false;
for (int y=0;y<mask->height;y++)
{
int pixel = cvGetReal2D(mask,y,x);
if (pixel>200)
{
lipTopStartPoint.x = x;
lipTopStartPoint.y = y;
flag = true;
break;
}
}
if (flag)
{
break;
}
}
//求上嘴唇上轮廓最右边的点
for (int x=mask->width-1;x>=0;x--)
{
bool flag = false;
for (int y=0;y<mask->height;y++)
{
int pixel = cvGetReal2D(mask,y,x);
if (pixel>200)
{
lipTopEndPoint.x = x;
lipTopEndPoint.y = y;
flag = true;
break;
}
}
if (flag)
{
break;
}
}
//对上嘴唇的上轮廓每隔step个点取一个典型点,并将这些典型点连接起来
CvPoint point1 = lipTopStartPoint,point2 = cvPoint(0,0);
for (int x=lipTopStartPoint.x+step;x<=lipTopEndPoint.x;x=x+step)
{
for (int y=0;y<mask->height;y++)
{
int pixel = cvGetReal2D(mask,y,x);
if (pixel>200)
{
point2.x = x;
point2.y = y;
break;
}
}
//用直线把point1和point2连接起来
cvLine(lipContour,point1,point2,cvScalarAll(255),1,8);
point1 = point2;
}