📄 testdoc.cpp
字号:
// TestDoc.cpp : implementation of the CTestDoc class
//
#include "stdafx.h"
#include "Test.h"
#include "TestDoc.h"
#include "func.h"
#include "MainFrm.h"
#include "math.h"
#include "SizeDlg1.h"
#define _CRTDBG_MAP_ALLOC
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestDoc
IMPLEMENT_DYNCREATE(CTestDoc, CDocument)
BEGIN_MESSAGE_MAP(CTestDoc, CDocument)
//{{AFX_MSG_MAP(CTestDoc)
ON_COMMAND(IDM_OPENSINGLE, OnOpensingle)
ON_COMMAND(ID_JIEQU, OnJiequ)
ON_COMMAND(ID_INTEEQUALIZE, OnInteequalize)
ON_COMMAND(ID_SIZEEQUALIZE, OnSizeequalize)
ON_COMMAND(ID_DCT, OnDct)
ON_COMMAND(ID_DETECT, OnDetect)
ON_COMMAND(ID_SIZEPROPERTY, OnSizeProperty)
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
ON_COMMAND(ID_OPENSEQUENCE, OnOpenSequence)
ON_COMMAND(ID_TRAININGALLMAP, OnTrainingAllMap)
ON_COMMAND(ID_TESTALLMAP, OnTestAllMap)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDoc construction/destruction
CTestDoc::CTestDoc()
{
// TODO: add one-time construction code here
flag=0;
width = 0;
hight = 0;
number = 0;
//以下的值为手动设定的图像路径值.也可以自动获取.
TypeString[0] = "test\\";
TypeString[1] ="training\\";
SaveTypeString[0] ="donetest\\";
SaveTypeString[1] = "donetraining\\";
FaceString[0] ="angry\\";
FaceString[1] ="disgust\\";
FaceString[2] ="fear\\";
FaceString[3] ="happy\\";
FaceString[4] ="sad\\";
FaceString[5] ="surprise\\";
OpenType = FALSE;
JieQuempimage = NULL;
m_pSingle = NULL;
SizeTempImage = NULL;
m_beginpoint = CPoint(0,0);
m_endpoint = CPoint(0,0);
CvHaarClassifierCascade* cascade = cvLoadHaarClassifierCascade( "<default_face_cascade>",cvSize( ORIG_WIN_SIZE, ORIG_WIN_SIZE ));
hid_cascade = cvCreateHidHaarClassifierCascade( cascade, 0, 0, 0, 1 );
cvReleaseHaarClassifierCascade( &cascade );
}
CTestDoc::~CTestDoc()
{
if (m_pSingle != NULL)
{
delete m_pSingle;
}
}
BOOL CTestDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CTestDoc serialization
void CTestDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CTestDoc diagnostics
#ifdef _DEBUG
void CTestDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CTestDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestDoc commands
void CTestDoc::OnOpensingle()
{
// TODO: Add your command handler code here
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"*.bmp|*.bmp||*.png|*.png||");
if (dlg.DoModal() == IDOK)
{
str = dlg.GetPathName();
}
flag=3;
OpenType = TRUE;
if (m_pSingle)
{
delete m_pSingle;
m_pSingle = NULL;
}
m_pSingle=new CDIB(str);
if (m_pSingle == NULL)
{
AfxMessageBox("不能打开图片!");
}
m_path = str;
UpdateAllViews(NULL);
if (width == 0 && hight == 0)
{//v尺寸初始设置
OnSizeProperty();
}
_CrtDumpMemoryLeaks();
OnDetect();//人脸检测
_CrtDumpMemoryLeaks();
OnJiequ(); //获取子图像
_CrtDumpMemoryLeaks();
OnInteequalize();//灰度归一化
_CrtDumpMemoryLeaks();
OnSizeequalize();//尺寸归一化
_CrtDumpMemoryLeaks();
//////////////////////////////////////////////////////////////////////////
CFileDialog MyDlg(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"*.bmp|*.bmp||*.png|*.png||");
if (MyDlg.DoModal() == IDOK)
{
SequenceStr = MyDlg.GetPathName();
}
SequenceSaveDictory.Format("%s.bmp",SequenceStr);
OnSaveDocument(SequenceSaveDictory);
/*delete m_pSingle;
m_pSingle = NULL;
if (SizeTempImage)
{
delete SizeTempImage;
SizeTempImage = NULL;
}
if (JieQuempimage)
{
delete JieQuempimage;
JieQuempimage = NULL;
}*/
OpenType = FALSE;
}
void CTestDoc::OnJiequ()
{//获取子图像.
// TODO: Add your command handler code here
/*if (JieQuempimage)
{
delete JieQuempimage;
JieQuempimage = NULL;
}*/
JieQuempimage=new CDIB(m_pSingle,m_beginpoint.x,m_beginpoint.y,m_endpoint.x,m_endpoint.y);
m_pSingle=JieQuempimage;
if (OpenType)
{
UpdateAllViews(NULL);
}
}
void CTestDoc::OnInteequalize()
{//灰度均衡化
// TODO: Add your command handler code here
LONG lWidth;
LONG lHeight;
lWidth = m_pSingle->GetDIBSize().cx;
lHeight = m_pSingle->GetDIBSize().cy;
LPSTR lpDIB;
// 锁定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_pSingle->GetHDIB());
// 指向源图像的指针
LPSTR lpDIBBits = m_pSingle->FindDIBBits(lpDIB);
m_pSingle->InteEqualize(lpDIBBits,lWidth,lHeight);
UpdateAllViews(NULL);
}
void CTestDoc::OnSizeequalize()
{//尺寸归一化
// TODO: Add your command handler code here
if (width == 0 && hight == 0)
{
OnSizeProperty();
}
/*if (SizeTempImage)
{
delete SizeTempImage;
SizeTempImage = NULL;
}*/
SizeTempImage = new CDIB(m_pSingle,width,hight);//生成新的一幅图像:输入原始图像,图像的宽度和高度.
m_pSingle=SizeTempImage;
if (OpenType)
{
UpdateAllViews(NULL);
}
}
void CTestDoc::OnDct()
{
// TODO: Add your command handler code here
LONG lWidth;
LONG lHeight;
lWidth = m_pSingle->GetDIBSize().cx;
lHeight = m_pSingle->GetDIBSize().cy;
LPSTR lpDIB;
// 锁定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_pSingle->GetHDIB());
// 指向源图像的指针
LPSTR lpDIBBits = m_pSingle->FindDIBBits(lpDIB);
DIBDct(lpDIBBits,lWidth,lHeight);
UpdateAllViews(NULL);
}
CRect detect_and_draw( IplImage* img, IplImage* temp )
{
int scale = 2;
CvPoint pt1, pt2;
CRect rect;
int i;
cvPyrDown( img, temp, CV_GAUSSIAN_5x5 );
cvFlip( temp, temp, 0 );
cvClearMemStorage( storage );
if( hid_cascade )
{
CvSeq* faces = cvHaarDetectObjects( temp, hid_cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING );
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, i, 0 );
pt1.x = (r->x+7)*scale;
pt2.x = (r->x+r->width-7)*scale;
pt1.y = img->height - r->y*scale;
pt2.y = img->height - (r->y+r->height+10)*scale;
// cvRectangle( img, pt1, pt2, CV_RGB(0,255,0), 3 );
//temp = cvCreateImage( cvSize(frame->width/2,frame->height/2), 8, 3 );
}
}
//CString str;
// str.Format("%d",img->width);
CPoint p1(pt1.x,img->height-pt1.y);
CPoint p2(pt2.x,img->height-pt2.y);
int x_with = p2.x - p1.x;
int y_hight = p2.y - p1.y;
//杨忠修改部分.
p1.x+=x_with/8;
p1.y+=y_hight/8;
p2.x-=x_with/8;
p2.y-=y_hight/8;
//杨忠修改结束
//cvShowImage(WINNAME, img );
return rect = CRect(p1,p2);
}
void CTestDoc::OnDetect()
{//
// TODO: Add your command handler code here
IplImage *frame, *temp;
storage = cvCreateMemStorage(0);
frame=cvLoadImage(m_path,1);
temp = cvCreateImage( cvSize(frame->width/2,frame->height/2), 8, 3 );
CRect rect;
if(frame)
{ cvFlip( frame, frame, 0 );
rect = detect_and_draw( frame, temp );//此函数可以获得人脸的坐标.rect
cvWaitKey(0);
cvFlip( frame, frame, 0 );
CString str;
//str.Format("%d,%d,%d,%d",rect.top,rect.bottom,rect.left,rect.right);
//AfxMessageBox(str);
m_beginpoint= CPoint(rect.left,rect.top);
m_endpoint=CPoint(rect.right,rect.bottom);
}
else AfxMessageBox("error");
}
BOOL CTestDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
BOOL bSuccess = FALSE;
bSuccess=m_pSingle->SaveDIB(lpszPathName);
if (!bSuccess)
{
// 保存失败,可能是其它格式的DIB,可以读取但是不能保存
// 或者是SaveDIB函数有误
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -