fsgl.cpp
资源名称:fsgl.rar [点击查看]
上传用户:likuei778
上传日期:2022-07-12
资源大小:1361k
文件大小:2k
源码类别:
2D图形编程
开发平台:
Visual C++
- #include <cv.h>
- #include <highgui.h>
- #include<iostream.h>
- #pragma comment (lib, "cxcore.lib")
- #pragma comment (lib, "cv.lib")
- #pragma comment (lib, "highgui.lib")
- int main(int argc, char *argv[])
- {
- IplImage* img = 0; // 原图
- IplImage* dst_img;//ycbcr图
- IplImage* dst;//肤色图
- char *name="test.bmp";
- img=cvLoadImage(name,1); //加载图像
- dst=cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,1);
- dst_img=cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,3);
- int height,width,step,channels;
- uchar *data;
- height = dst_img->height;
- width = dst_img->width;
- step = dst_img->widthStep;
- channels = dst_img->nChannels;
- data = (uchar *)dst_img->imageData;
- cvCvtColor(img, dst_img, CV_BGR2YCrCb);// rgb->ycrcb
- double c[4]={0.0077 ,-0.0041,-0.0041 ,0.0047};//(肤色)协方差矩阵求逆后
- CvMat* max=cvCreateMat(height,width,CV_64FC1);//存储肤色概率密度矩阵(数组)
- CvMat* mat=cvCreateMat(2,2,CV_64FC1);//存储协方差矩阵(逆)
- CvMat* a=cvCreateMat(1,2,CV_64FC1);//{cb,cr}
- CvMat* a1=cvCreateMat(2,1,CV_64FC1);//{cb,cr}转至
- CvMat* res=cvCreateMat(1,1,CV_64FC1);
- cvInitMatHeader(mat,2,2,CV_64FC1,c);
- double res1[1]={0};
- cvInitMatHeader(res,1,1,CV_64FC1,res1);
- cvNamedWindow("rgb", CV_WINDOW_AUTOSIZE);
- cvNamedWindow("肤色", CV_WINDOW_AUTOSIZE);
- int i,j;
- double b,r,cb,cr,u;
- for(i=0;i<height;i++) for(j=0;j<width;j++)
- {
- b=data[i*step+j*channels+2];//cb 分量
- r=data[i*step+j*channels+1];//cr 分量
- cb=b-103.0056;//
- cr=r-140.1309;//
- double p1[2]={cb,cr};
- cvInitMatHeader(a,1,2,CV_64FC1,p1);
- cvMatMulAdd(a,mat,0,a);
- cvInitMatHeader(a1,2,1,CV_64FC1,p1);
- cvMatMulAdd(a,a1,0,res);
- u=CV_MAT_ELEM(*res,double,0,0);
- u=exp(-0.5*u);
- cvmSet(max,i,j,u);
- }
- double max1=0; //初始化最大值
- double mm=0;
- cvMinMaxLoc(max,NULL,&max1,NULL,NULL);
- // 肤色似然度
- for(i=0;i<height;i++) for(j=0;j<width;j++)
- {
- mm=CV_MAT_ELEM(*max,double,i,j)/max1;
- cvmSet(max,i,j,mm);
- ((dst->imageData + dst->widthStep*i))[j]=255*mm;
- }
- //cvSmooth( dst, dst,CV_MEDIAN,3, 0, 0 );//中值滤波
- cvThreshold(dst,dst,250,255,CV_THRESH_BINARY);
- cvShowImage("rgb", img );
- cvShowImage("肤色", dst );
- cvSaveImage("1.jpg",dst);
- cvWaitKey(0);
- cvReleaseImage(&img );
- return 0;
- }