📄 facedlg.cpp
字号:
// faceDlg.cpp : implementation file
//
#include "stdafx.h"
#include "face.h"
#include "faceDlg.h"
#include "cv.h"
#include "highgui.h"
#include "cvcam.h"
#define max_corners 100
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//全局变量
IplImage *image=NULL,*hsv=NULL,*hue=NULL,*mask=NULL,*backproject=NULL,*histimg=NULL;
CvHistogram *hist=NULL;
int vmin=10,vmax=256,smin=30;
int backproject_mode=0,select_object=0,track_object=0,show_hist=1;
int hdims=50;
float hranges_arr[]={0,180};
float *hranges=hranges_arr;
CvPoint origin;
CvRect selection,track_window;
CvBox2D track_box;
CvConnectedComp track_comp;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFaceDlg dialog
CFaceDlg::CFaceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFaceDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFaceDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFaceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFaceDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFaceDlg, CDialog)
//{{AFX_MSG_MAP(CFaceDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_INIT, OnInit)
ON_BN_CLICKED(IDC_BUTTON1, OnVideoCap)
ON_BN_CLICKED(IDC_BUTTON2, OnMoveCheck)
ON_BN_CLICKED(IDC_BUTTON3, OnObjectTrack)
ON_BN_CLICKED(IDC_BUTTON4, OnHistImg)
ON_BN_CLICKED(IDC_BUTTON5, OnWriterVideo)
ON_BN_CLICKED(IDC_BUTTON6, OnCapture)
ON_BN_CLICKED(IDC_BUTTON7, OnCornerDetect)
ON_BN_CLICKED(IDC_BUTTON8, OnFilterImage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFaceDlg message handlers
BOOL CFaceDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
if(cvTestInit()) //初始化OPENCV 的xml数据库
return TRUE;
//MessageBox("haar xml loaded!","opencv",MB_OK);
else
return FALSE;
//MessageBox("loaded haar xml error!","opencv",MB_OK);
return TRUE; // return TRUE unless you set the focus to a control
}
void CFaceDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CFaceDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFaceDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFaceDlg::OnOK()
{
// TODO: Add extra validation here
cvTest();
}
void CFaceDlg::OnInit()
{
// TODO: Add your control notification handler code here
if(cvTestInit())
MessageBox("haar xml loaded!","opencv",MB_OK);
else
MessageBox("loaded haar xml error!","opencv",MB_OK);
}
void CFaceDlg::OnVideoCap()
{
// TODO: Add your control notification handler code here
VideoCap();
}
void CFaceDlg::OnMoveCheck() //运动目标检测
{
// TODO: Add your control notification handler code here
IplImage* pFrame=NULL;
IplImage* pFrImg=NULL;
IplImage* pBkImg=NULL;
CvMat* pFrameMat=NULL;
CvMat* pFrMat=NULL;
CvMat* pBkMat=NULL;
CvCapture* pCapture=NULL;
CString sm_filename;
CFileDialog dlg(true);
int nFrmNum=0;
//创建窗口
cvNamedWindow("video",1);
cvNamedWindow("background",1);
cvNamedWindow("foreground",1);
//使窗口有序排列
cvMoveWindow("video",30,20);
cvMoveWindow("background",360,20);
cvMoveWindow("foreground",690,20);
dlg.DoModal();
sm_filename = dlg.GetPathName();
if(sm_filename=="") return;
pCapture=cvCaptureFromAVI(sm_filename);
//逐帧读取视频
while(pFrame=cvQueryFrame(pCapture))
{
nFrmNum++;
//如果是第一帧,需要申请内存,并初始化
if(nFrmNum==1)
{
pBkImg=cvCreateImage(cvSize(pFrame->width,pFrame->height),IPL_DEPTH_8U,1);
pFrImg=cvCreateImage(cvSize(pFrame->width,pFrame->height),IPL_DEPTH_8U,1);
pBkMat=cvCreateMat(pFrame->height,pFrame->width,CV_32FC1);
pFrMat=cvCreateMat(pFrame->height,pFrame->width,CV_32FC1);
pFrameMat=cvCreateMat(pFrame->height,pFrame->width,CV_32FC1);
//转换成单通道图像再处理
cvCvtColor(pFrame,pBkImg,CV_BGR2GRAY);
cvCvtColor(pFrame,pFrImg,CV_BGR2GRAY);
cvConvert(pFrImg,pFrameMat);
cvConvert(pFrImg,pFrMat);
cvConvert(pFrImg,pBkMat);
cvFlip(pBkMat,pBkMat,0);//把数组绕x轴翻转,书上没有的
}
else
{
cvCvtColor(pFrame,pFrImg,CV_BGR2GRAY);
cvConvert(pFrImg,pFrameMat);
cvFlip(pFrameMat,pFrameMat,0);//把数组绕x轴翻转
//先做高斯滤波,以平滑图像
cvSmooth(pFrameMat,pFrameMat,CV_GAUSSIAN,3,0,0);
//当前帧与背景图相减
cvAbsDiff(pFrameMat,pBkMat,pFrMat);
//二值化前景图
cvThreshold(pFrMat,pFrImg,60,255.0,CV_THRESH_BINARY);
//进行形态学滤波,去掉噪声
cvErode(pFrImg,pFrImg,0,1); //腐蚀
cvDilate(pFrImg,pFrImg,0,1); //膨胀
//更新背景
//cvFlip(pFrameMat,pFrameMat);
cvRunningAvg(pFrameMat,pBkMat,0.005,0);
//将背景转换为图像格式,用于显示
cvConvert(pBkMat,pBkImg);
//显示图像
cvShowImage("video",pFrame);
cvShowImage("background",pBkImg);
cvShowImage("foreground",pFrImg);
//如果有按键发生,则跳出循环
//此等待也为cvShowImage函数提供时间完成显示,等待时间可以根据CPU速度调整
//cvWaitKey(15);
if(cvWaitKey(5)>=0)
break;
} //end of if-else
} //end of while-loop
//销毁窗口
cvDestroyWindow("video");
cvDestroyWindow("background");
cvDestroyWindow("foreground");
//释放图像和矩阵
cvReleaseImage(&pFrImg);
cvReleaseImage(&pBkImg);
cvReleaseMat(&pFrameMat);
cvReleaseMat(&pFrMat);
cvReleaseMat(&pBkMat);
}
void On_Mouse( int event, int x, int y, int flags, void* param )
{
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;
}
switch( event )
{
case CV_EVENT_LBUTTONDOWN:
origin = cvPoint(x,y);
selection = cvRect(x,y,0,0);
select_object = 1;
break;
case CV_EVENT_LBUTTONUP:
select_object = 0;
if( selection.width > 0 && selection.height > 0 )
track_object = -1;
break;
}
}
CvScalar hsv2rgb(float hue)
{
int rgb[3],p,sector;
static const int sector_data[][3]={{0,2,1},{1,2,0},{1,0,2},{2,0,1},{2,1,0},{0,1,2}};
hue*=0.0333333333333333333f;
sector=cvFloor(hue);
p=cvRound(255*(hue-sector));
p^=sector&1?255:0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -