//肤色提取,skinArea为二值化肤色图像
void skinExtract(const Mat &frame, Mat &skinArea)
{
Mat YCbCr;
vector<Mat> planes;
//转换为YCrCb颜色空间
cvtColor(frame, YCbCr, CV_RGB2YCrCb);
//将多通道图像分离为多个单通道图像
split(YCbCr, planes);
skinArea.create(YCbCr.size(), planes[1].type());
//人的皮肤颜色在YCbCr色度空间的分布范围:100<=Cb<=127, 138<=Cr<=170
int Cols = frame.cols;
for (int r = 0; r < frame.rows; r++){
const uchar* Cb = planes[1].ptr<uchar>(r);
const uchar* Cr = planes[2].ptr<uchar>(r);
uchar* data = skinArea.ptr<uchar>(r);
for (int c = 0; c < Cols; c++)
{
if (100 <= Cb[c] && Cb[c] <= 127 && 138 <= Cr[c] && Cr[c] <= 170)
{
data[c] = 0;
}
else{
data[c] = 255;
}
}
}
//膨胀和腐蚀,膨胀可以填补凹洞(将裂缝桥接),腐蚀可以消除细的凸起(“斑点”噪声)
//dilate(skinArea, skinArea, Mat(3, 3, CV_8UC1), Point(-1, -1));
//erode(skinArea, skinArea, Mat(3, 3, CV_8UC1), Point(-1, -1));
//blur(skinArea, skinArea, cvSize(2, 2));
//erode(skinArea, skinArea,cv::Mat());
}
//肤色提取
double skinExtract(const IplImage *pFrame)
{
IplImage *pYCbCr = cvCreateImage(cvGetSize(pFrame), 8, 3);;
IplImage *pCb = cvCreateImage(cvGetSize(pFrame), 8, 1);
IplImage *pCr = cvCreateImage(cvGetSize(pFrame), 8, 1);
cv::vector<IplImage*> planes;
//转换成YCrCb颜色空间
cvCvtColor(pFrame, pYCbCr, CV_RGB2YCrCb);
//把多通道图像分离为单通道图像
cvSplit(pYCbCr, 0, pCb, pCr, 0);
//Cb[100,127] Cr[138,170]
long area = pYCbCr->height * pYCbCr->width;
long skin = 0;
for (int h = 0; h < pYCbCr->height; h++){
unsigned char *strCb = (unsigned char*)pCb->imageData + h*pCb->widthStep;
unsigned char *strCr = (unsigned char*)pCr->imageData + h*pCr->widthStep;
for (int w = 0; w < pYCbCr->width; w++){
if (strCb[w]>100 && strCb[w]<127 && (int)strCr[w]>138 && (int)strCr[w] < 170){
skin++;
}
}
}
/*
cvErode(skinFrame, skinFrame, NULL, 1); //腐蚀
//cvDilate(skinFrame, skinFrame, NULL, 2); //膨胀
cvSmooth(skinFrame, skinFrame, CV_BLUR, 3, 3, 0, 0);
//cvShowImage("dst1", skinFrame);
*/
cvReleaseImage(&pYCbCr);
cvReleaseImage(&pCb);
cvReleaseImage(&pCr);
return skin*1.0 / area;
}