📄 face.cpp
字号:
// face.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "face.h"
#include "faceDlg.h"
#include "cv.h"
#include "highgui.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
/////////////////////////////////////////////////////////////////////////////
// CFaceApp
BEGIN_MESSAGE_MAP(CFaceApp, CWinApp)
//{{AFX_MSG_MAP(CFaceApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFaceApp construction
CFaceApp::CFaceApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CFaceApp object
CFaceApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CFaceApp initialization
BOOL CFaceApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CFaceDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
void detect_and_draw( IplImage* img )
{
static CvScalar colors[] =
{
{{0,0,255}},
{{0,128,255}},
{{0,255,255}},
{{0,255,0}},
{{255,128,0}},
{{255,255,0}},
{{255,0,0}},
{{255,0,255}}
};
double scale = 1.3;
//int img_width,img_height;
//img_width=cvSize(img->width,img->height).width; //取图像的宽度
//img_height=cvSize(img->width,img->height).height;//取图像的高度
IplImage* gray = cvCreateImage(cvSize(img->width,img->height), 8, 1 );
IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
cvRound (img->height/scale)),
8, 1 );
int i;
cvCvtColor( img, gray, CV_BGR2GRAY );//进行色彩空间转换
cvResize( gray, small_img, CV_INTER_LINEAR ); //改变图像的大小
cvEqualizeHist( small_img, small_img ); //对灰度图进行直方图均衡化
cvClearMemStorage( storage ); //清空内存存储块
if( cascade )
{
double t = (double)cvGetTickCount();//返回时钟计数
CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
1.1, 2, 0,
cvSize(5, 5) );
t = (double)cvGetTickCount() - t;
t=t/((double)cvGetTickFrequency()*1000.);
printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );//通过索引值查找相应的集合元素,前为集合,后为索引
CvPoint center;
int radius;
center.x = cvRound((r->x + r->width*0.5)*scale); //取与输入参数最接近的整数
center.y = cvRound((r->y + r->height*0.5)*scale);
radius = cvRound((r->width + r->height)*0.25*scale);
cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
}
}
cvShowImage( "result", img );
cvReleaseImage( &gray );
cvReleaseImage( &small_img );
}
int cvTestInit(void)
{
CString cascade_name;
//OpenCV初始化,于CFileDialog有冲突,不能放在DoModal之后
storage = cvCreateMemStorage(0);
cvFirstType();
cascade_name = "haarcascade_frontalface_alt2.xml";
cascade = (CvHaarClassifierCascade*) cvLoad( cascade_name, NULL, NULL, NULL );
if(cascade)
return 1;
else
return 0;
}
void cvTest(void)
{
IplImage* image=0,*gray=0,*edge=0;
CString sm_filename;
CFileDialog dlg(true);
CvFont font;
double hScale=1.0,vScale=1.0;
int lineWidth=1;
int height,width,step,channels;
uchar *data;
//image=cvCreateImage(cvSize(458,375),IPL_DEPTH_8U,1);
if( dlg.DoModal() )
{
sm_filename = dlg.GetPathName();
if(sm_filename == "")
return;
image = cvLoadImage(sm_filename, 1);
if(cascade)
{
if(image)
{
//AfxMessageBox("load image sucessed!");
//test获取图像信息
height=image->height;
width=image->width;
step=image->widthStep;
channels=image->nChannels;
data=(uchar*)image->imageData;
/*********************test end ****************************/
gray=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
edge=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
cvNamedWindow( "result", 1 );
cvMoveWindow("result",100,100);
//反转图像
/* for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
for(int k=0;k<channels;k++)
data[i*step+j*channels+k]=255-data[i*step+j*channels+k];*/
detect_and_draw(image); //人脸检测并作标识
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC,hScale,vScale,0,lineWidth);
cvPutText(image,"My First word",cvPoint(100,91),&font,cvScalar(255,255,0));//添加字符到图像上
cvCvtColor(image,gray,CV_BGR2GRAY);
//cvSmooth(gray,edge,CV_BLUR,3,3,0);
//cvNot(gray,edge);
//边缘检测
cvCanny(gray,edge,30,70,3);
//cvCvtColor(edge,image,CV_GRAY2BGR);
cvShowImage( "result",image); //仅显示图像
cvWaitKey(0);
cvShowImage("result",edge);
cvWaitKey(0);
cvReleaseImage(&image);
cvDestroyWindow("result");
}
else
AfxMessageBox("load image error!");
}
}
}
void VideoCap(void)
{
IplImage *pFrame = NULL;
CvCapture* pCapture = NULL;
CString sm_filename;
CFileDialog dlg(true);
CvFont font;
double hScale=1.0,vScale=1.0,t=0;
int lineWidth=1,decimal,sign;
char *t_temp;
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC,hScale,vScale,0,lineWidth);
cvNamedWindow("video", 1);
pCapture = cvCaptureFromCAM(0); //从摄像头上采集数据
/*if(dlg.DoModal()) //从文件中读取视频
{
sm_filename = dlg.GetPathName();
pCapture=cvCaptureFromAVI(sm_filename);
}*/
while(pFrame = cvQueryFrame( pCapture ))
{
// fps=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS);
t = (double)cvGetTickCount();//返回时钟计数
t_temp=_fcvt(t,12,&decimal,&sign);
cvPutText(pFrame,t_temp,cvPoint(10,200),&font,cvScalar(0,0,255));//添加字符到图像上
//cvPutText(pFrame,str(fps),cvPoint(10,50),&font,cvScalar(0,255,255));//添加字符到图像上
cvShowImage("video", pFrame);
t = (double)cvGetTickCount() - t;
t=t/((double)cvGetTickFrequency()*1000.);
//cvWaitKey(20);
if(cvWaitKey(1)>=0 )
break;
}
cvDestroyWindow("video");
cvReleaseCapture(&pCapture);
}
/*void On_Mouse(int event,int x,int y,int flags)
{
if(!image)
return;
if(image->origin)
y=image->height-y;
if(select_object)
{
selection.x=MIN(x,origin.x);
selection.y=MIN(y,origin.y);
selection.width=selection.x+CV_IABS(x-origin.x);
selection.height=selection.y+CV_IABS(y-origin.y);
selection.x=MAX(selection.x,0);
selection.y=MAX(selection.y,0);
selection.width=MIN(selection.width,image->width);
selection.height=MIN(selection.height,image->height);
selection.width-=selection.x;
selection.height-=selection.y;
}
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -