⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 facedetectiondoc.cpp

📁 人脸识别,希望对大家有所帮助!它的应用将扩展到教育、培训和娱乐等新的领域。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// FaceDetectionDoc.cpp : implementation of the CFaceDetectionDoc class
//

#include "stdafx.h"
#include "FaceDetection.h"

#include "FaceDetectionDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFaceDetectionDoc

IMPLEMENT_DYNCREATE(CFaceDetectionDoc, CDocument)

BEGIN_MESSAGE_MAP(CFaceDetectionDoc, CDocument)
	//{{AFX_MSG_MAP(CFaceDetectionDoc)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFaceDetectionDoc construction/destruction

CFaceDetectionDoc::CFaceDetectionDoc()
{
	// TODO: add one-time construction code here
	int i,j;
	m_hDIB=NULL;
	for( i=0; i<ImgRange; i++)
		for ( j=0; j<ImgRange; j++)
		{
			emymapc[i][j] = false;
			emymapl[i][j] = false;
			//lab[i][j] = false;
		}
	for( i=0;i<ImgRange; i++)
		for ( j=0; j<ImgRange; j++)
			lab[i][j] = false;

}

CFaceDetectionDoc::~CFaceDetectionDoc()
{
}

BOOL CFaceDetectionDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CFaceDetectionDoc serialization

void CFaceDetectionDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CFaceDetectionDoc diagnostics

#ifdef _DEBUG
void CFaceDetectionDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CFaceDetectionDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFaceDetectionDoc commands

BOOL CFaceDetectionDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	CFile fileM;
	CFileException feM;
	m_hDIB = NULL;
	if (!fileM.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &feM))
	{
		// 失败
		ReportSaveLoadException(lpszPathName, &feM,FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);	
		// 返回FALSE
		return FALSE;
	}
	if (!fileM2.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &feM))
	{
		// 失败
		ReportSaveLoadException(lpszPathName, &feM,FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);	
		// 返回FALSE
		return FALSE;
	}
		TRY
	{
		m_hDIB = ::ReadDIBFile(fileM);
	}
	CATCH (CFileException, eLoad)
	{
		// 读取失败
		fileM.Abort();		
		// 恢复光标形状
		EndWaitCursor();		
		// 报告失败
		ReportSaveLoadException(lpszPathName, eLoad,
			FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);	
		// 设置DIB为空
		m_hDIB = NULL;	
		fileM.Close();
		// 返回FALSE
		return FALSE;
	}
	END_CATCH
	m_hDIBtemp=(HDIB)CopyHandle(m_hDIB);
	//if(fileM.Read(&bmfHeaderMulspec,sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
	//return FALSE;

	fileM.Close();
	// TODO: Add your specialized creation code here
	return TRUE;
}
bool CFaceDetectionDoc::LightingCompensate()
{

	LPBITMAPINFOHEADER lpbi;
	lpbi = (LPBITMAPINFOHEADER)::GlobalLock((HGLOBAL) m_hDIB);//读取头文件
	lLineBytesMulspec = WIDTHBYTES((lpbi->biWidth)*24);
	::GlobalUnlock((HGLOBAL) m_hDIB);
	int width,height;
	LPBYTE lpData;
	long wBytesPerLine;
	LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);	
	// 获取DIB宽度
	width= (int) ::DIBWidth(lpDIB);		
	// 获取DIB高度
	height= (int) ::DIBHeight(lpDIB);
	::GlobalUnlock((HGLOBAL) m_hDIB);
	lpData = (unsigned char*)::FindDIBBits(lpDIB);
	//得到图片每行的象素所占字节个数
	wBytesPerLine = lLineBytesMulspec;
	//比例系数
	const float thresholdco = 0.05;
	//象素个数的临界常数
	const int thresholdnum = 100;
	//灰度级数组
	int histogram[256];
	for(int i =0;i<256;i++)
		histogram[i] = 0;
	//对于过于小的图片的判断
	if(width*height*thresholdco < thresholdnum)
		return false;
	int colorr,colorg,colorb;
	long lOffset;
	//考察整个图片
	for( i=0;i<height;i++)
		for(int j=0;j<width;j++)
		{	
			//得到象素数据的偏移
			lOffset = i*wBytesPerLine + j*3;
			//得到rgb值
			colorb = *(lpData+lOffset++);
			colorg = *(lpData+lOffset++);
			colorr = *(lpData+lOffset++);
			//计算灰度值
			int gray = (colorr * 299 + colorg * 587 + colorb * 114)/1000;
			histogram[gray]++;
		}
		int calnum =0;
		int total = width*height;
		int num;
		//下面的循环得到满足系数thresholdco的临界灰度级
		for(i =0;i<256;i++)
		{
			if((float)calnum/total < thresholdco)
			{
				calnum+= histogram[255-i];
				num = i;
			}
			else
				break;
		}
		int averagegray = 0;
		calnum =0;
		//得到满足条件的象素总的灰度值
		for(i = 255;i>=255-num;i--)
		{
			averagegray += histogram[i]*i;
			calnum += histogram[i];
		}
		averagegray /=calnum;
		//得到光线补偿的系数
		float co = 255.0/(float)averagegray;
		//下面的循环对图象进行光线补偿
		for(i =0;i<height;i++)
			for(int j=0;j<width;j++)
			{	
				//得到数据便宜
				lOffset =i*wBytesPerLine + j*3;
				//得到蓝色分量
				colorb = *(lpData+lOffset);
				//调整
				colorb *=co;
				//临界判断
				if(colorb >255)
					colorb = 255;
				//保存
				*(lpData+lOffset) = colorb;
				//绿色分量
				colorb = *(lpData+lOffset+1);
				colorb *=co;
				if(colorb >255)
					colorb = 255;
				*(lpData+lOffset+1) = colorb;
				//红色分量
				colorb = *(lpData+lOffset+2);
				colorb *=co;
				if(colorb >255)
					colorb = 255;
				*(lpData+lOffset+2) = colorb;
			}
	return TRUE;
}
void CFaceDetectionDoc::RgbtoYcb(HDIB hDIB,LPBYTE lpYcb)
{
	LPBITMAPINFOHEADER lpbi;
	lpbi = (LPBITMAPINFOHEADER)::GlobalLock((HGLOBAL) hDIB);//读取头文件
	lLineBytesMulspec = WIDTHBYTES((lpbi->biWidth)*24);
	::GlobalUnlock((HGLOBAL)hDIB);
	int width,height;
	WORD wBytesPerLine;
	LPBYTE lpData;
	LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);	
	// 获取DIB宽度
	width= (int) ::DIBWidth(lpDIB);		
	// 获取DIB高度
	height= (int) ::DIBHeight(lpDIB);
	lpData = (unsigned char*)::FindDIBBits(lpDIB);
	//得到图象的基本信息	
	wBytesPerLine = lLineBytesMulspec;//得到图片每行的象素所占字节个数

	long lOffset;
	//下面的循环实现从rgb到ycc的转化
	for(int i=0;i<height;i++)
		for(int j=0;j<width;j++)
		{
			
			lOffset = i*wBytesPerLine + j*3;	//得到象素数据的偏移
			//得到rgb数值
			int b = *(lpData + lOffset);
			int g = *(lpData + lOffset+1);
			int r = *(lpData + lOffset+2);
			//计算得到y,cr,cb的数值
			int Y = (257*r+504*g+98*b)/1000+16;
			int Cr = (439*r-368*g-71*b)/1000+128;
			int Cb = (-148*r-291*g+439*b)/1000+128;
			//保存计算得到的数值
			*(lpYcb+lOffset++) = Y;
			*(lpYcb+lOffset++) = Cr;
			*(lpYcb+lOffset++) = Cb;
		}
	::GlobalUnlock((HGLOBAL) hDIB);
}

//////////////*皮肤颜色建模*//////////////
int CFaceDetectionDoc::_Cb(int Y)
{	
	int Cb;
	//如果亮度很小的情况
	if(Y<Kl)
		Cb = 108 + ((Kl-Y)*10)/(Kl-Ymin);
	//亮度很大的情况
	else if(Y>Kh)
		Cb = 108 + ((Y-Kh)*10)/(Ymax - Kh);
	else 
		Cb = -1;
	return Cb;
}

int CFaceDetectionDoc::_Cr(int Y)
{
	int Cr;
	//亮度很小的情况
	if(Y<Kl)
		Cr = 154 - ((Kl-Y)*10)/(Kl-Ymin);
	//亮度很大的情况
	else if(Y>Kh)
		Cr = 154 - ((Y-Kh)*22)/(Ymax - Kh);
	else
		Cr = -1;
	return Cr;
}
int CFaceDetectionDoc::_WCr(int Y)
{
	int WCr;
	if(Y<Kl)
		//亮度很小的情况
		WCr = WLcr + ((Y-Ymin)*(Wcr-WLcr))/(Kl-Ymin);
	else if(Y>Kh)
		//亮度很大的情况
		WCr = WHcr + ((Ymax-Y)*(Wcr-WHcr))/(Ymax-Kh);
	else WCr = -1;
	return WCr;
}

int CFaceDetectionDoc:: _WCb(int Y)
{
	int WCb;
	if(Y<Kl)
		//亮度很小的情况
		WCb = WLcb + ((Y-Ymin)*(Wcb-WLcb))/(Kl-Ymin);
	else if(Y>Kh)
		//亮度很大的情况
		WCb = WHcb + ((Ymax-Y)*(Wcb-WHcb))/(Ymax-Kh);
	else WCb = -1;
	return WCb;
}
void CFaceDetectionDoc::YccTransform(LPBYTE lpYcc,WORD wBytesPerLine,int height,int width)
{	
	int Y,Cr,Cb;
	long lOffset;
	//下面的循环实现ycc色彩空间的非线性转换
	for(int i=0;i<height;i++)
		for(int j=0;j<width;j++)
		{	
			//得到数据偏移
			lOffset = i*wBytesPerLine + j*3;
			//得到y,Cr,Cb数值
			Y = *(lpYcc+lOffset);
			Cr = *(lpYcc+lOffset+1);
			Cb = *(lpYcc+lOffset+2);
			//如果y数值在两个临界值之间,保持不变
			if(Y>=Kl && Y<=Kh)
				continue;
			//调用非线性转换函数调整Cr,Cb的数值
			Cr = (Cr-_Cr(Y))*(Wcr/_WCr(Y))+_Cr(Kh);
			Cb = (Cb-_Cb(Y))*(Wcb/_WCb(Y))+_Cb(Kh);
			*(lpYcc+lOffset+1) = Cr;
			*(lpYcc+lOffset+2) = Cb;
		}
}

void CFaceDetectionDoc::faceear(LPBYTE lpYcc, WORD wBytesPerLine, int height,int width, bool flag[ImgRange][ImgRange])
{	
	//初始化标志位
	for (int i=0; i<ImgRange; i++)
		for (int j=0; j<ImgRange; j++)
		{
			flag[i][j] = false;
		}
	long lOffset;
	int Cr;
	int Cb;
	for (i=0; i<height; i++)
		for (int j=0; j<width; j++)
		{	
			//得到偏移
			lOffset = i*wBytesPerLine + j*3;
			//得到Cr,Cb数值
			Cr = *(lpYcc+lOffset+1);
			Cb = *(lpYcc+lOffset+2);
			//人脸颜色建模
			if(FaceModeling(Cr,Cb))
			{	
				//修改标志位
				flag[i][j] = true;
			}
		}	
}
BOOL CFaceDetectionDoc::FaceModeling(int Cr,int Cb)
{	
	//Cb的系数常量
	const float cx = 122.453;//114.38;
	//cr的系数常量
	const float cy = 158.442;//160.02;
	//角度常量
	const float theta = 2.53;
	//x轴线和y轴线的两个常量
	const float ecx = 1.60;
	const float ecy = 2.41;
	//长轴
	const float a = 25.39;
	//短轴
	const float b = 14.03;
	//相似度常量
	const float judge = 0.5;
	//计算得到x轴数值
	float  x = cos(theta)*(Cb-cx)+sin(theta)*(Cr-cy);
	//y轴数值
	float  y = -sin(theta)*(Cb -cx)+cos(theta)*(Cr-cy);
	//计算离心率
	float temp = pow(x-ecx,2)/pow(a,2)+pow(y-ecy,2)/pow(b,2);
	//如果满足要求返回真,否则假
	if(fabs(temp-1.0)<judge)
		return TRUE;
	else
		return FALSE;
}

void CFaceDetectionDoc::Skintone() 
{	
	LPBITMAPINFOHEADER lpbi;
	lpbi = (LPBITMAPINFOHEADER)::GlobalLock((HGLOBAL) m_hDIB);//读取头文件
	lLineBytesMulspec = WIDTHBYTES((lpbi->biWidth)*24);
	::GlobalUnlock((HGLOBAL) m_hDIB);
	int width,height;
	LPBYTE lpData;
	long wBytesPerLine;
	LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);	
	// 获取DIB宽度
	width= (int) ::DIBWidth(lpDIB);		
	// 获取DIB高度
	height= (int) ::DIBHeight(lpDIB);
	::GlobalUnlock((HGLOBAL) m_hDIB);
	lpData = (unsigned char*)::FindDIBBits(lpDIB);
	//得到图片每行的象素所占字节个数
	wBytesPerLine = lLineBytesMulspec;
	lpYcc = new BYTE[wBytesPerLine * height];
	RgbtoYcb(m_hDIB,lpYcc);
	YccTransform(lpYcc, wBytesPerLine,height,width);
	faceear(lpYcc,wBytesPerLine,height,width,flag);	
	lpData =(LPBYTE)::GlobalLock(lpDIB)+sizeof(BITMAPINFOHEADER);	
	for (int i=0; i<height; i++)
		for (int j=0; j<width; j++)
		{	
			long lOffset = i*wBytesPerLine + j*3;
			if (flag[i][j] == true)
			{
				*(lpData + lOffset++) = 255;
				*(lpData + lOffset++) = 255;
				*(lpData + lOffset++) = 255;
			}
			else
			{
				*(lpData + lOffset++) = 0;
				*(lpData + lOffset++) = 0;
				*(lpData + lOffset++) = 0;
			}
		}
		
		::GlobalUnlock(lpDIB);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -