FaceMFC_v1Dlg.cpp
上传用户:xiaoge123
上传日期:2022-08-05
资源大小:5794k
文件大小:40k
- // FaceMFC_v1Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "FaceMFC_v1.h"
- #include "FaceMFC_v1Dlg.h"
- #include "Face.h"
- #include <vector>
- using namespace std;
- #define maxnum 1.7976931348623158e+308
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- volatile BOOL pausestatus;
- int count;
- CvCapture* capture;
- CString path;
- CSliderCtrl m_slider;
- CSliderCtrl m_slider_distance;
- IplImage* ImgPause;
- CvMat *Mask = cvCreateMat(100,100,CV_8UC1);
- CvMat *Meanface = cvCreateMat(100,100,CV_64FC1);
- CvMat *ProjectY = cvCreateMat(100,1,CV_64FC1);
- CvMat *EigenFace = cvCreateMat(100,1,CV_64FC1);
- vector<double> facedistance; //??????
- vector<int> frameselect; //?????
- vector<int> frontposition; //??????????????
- vector<double> frontdistances; //??????????????
- vector<CvRect *> Framefacepos;
- 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()
- /////////////////////////////////////////////////////////////////////////////
- // CFaceMFC_v1Dlg dialog
- CFaceMFC_v1Dlg::CFaceMFC_v1Dlg(CWnd* pParent /*=NULL*/)
- : CDialog(CFaceMFC_v1Dlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CFaceMFC_v1Dlg)
- m_framestep =25;
- mImg=NULL;
- ImgData=NULL;
- m_Facesel=1;
- FaceNum=0;
- ImgPause=NULL;
- ImgTemp=NULL;
- m_p1=NULL;
- m_p2=NULL;
- m_iIsDraging=0;
- ImgFaceResult[0]=NULL;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CFaceMFC_v1Dlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CFaceMFC_v1Dlg)
- DDX_Control(pDX, IDC_EDIT_TOTALTIME, m_totaltime);
- DDX_Control(pDX, IDC_EDIT_TIMEDOWN, m_timedown);
- DDX_Control(pDX, IDC_EDIT_TIMEUP, m_timeup);
- DDX_Text(pDX, IDC_EDIT_STEP, m_framestep);
- DDX_Control(pDX, IDC_SLIDER, m_slider);
- DDX_Control(pDX, IDC_SLIDER_DISTANCE, m_slider_distance);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CFaceMFC_v1Dlg, CDialog)
- //{{AFX_MSG_MAP(CFaceMFC_v1Dlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_OPENIMAGE, OnOpenimage)
- ON_BN_CLICKED(IDC_FACEDETECT, OnFacedetect)
- ON_BN_CLICKED(IDC_RADIOFACE1, OnRadioface1)
- ON_BN_CLICKED(IDC_RADIOFACE2, OnRadioface2)
- ON_BN_CLICKED(IDC_RADIOFACE3, OnRadioface3)
- ON_BN_CLICKED(IDC_RADIOFACE4, OnRadioface4)
- ON_BN_CLICKED(IDC_RADIOFACE5, OnRadioface5)
- ON_BN_CLICKED(IDC_RADIOFACE6, OnRadioface6)
- ON_BN_CLICKED(IDC_OPENVIDEO, OnOpenvideo)
- ON_BN_CLICKED(IDC_RELOAD, OnReload)
- ON_BN_CLICKED(IDC_PLAY_PAUSE, OnPlayPause)
- ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER, OnCustomdrawSlider)
- ON_BN_CLICKED(IDC_TIMESTART, OnTimestart)
- ON_BN_CLICKED(IDC_TIMEOVER, OnTimeover)
- ON_EN_KILLFOCUS(IDC_EDIT_STEP, OnKillfocusEditStep)
- ON_BN_CLICKED(IDC_FACERETRIEVAL, OnFaceretrieval)
- ON_BN_CLICKED(IDC_DOWN, OnDown)
- ON_BN_CLICKED(IDC_UP, OnUp)
- ON_BN_CLICKED(IDC_FOLLOW, OnFollow)
- ON_BN_CLICKED(IDC_RADIORESULT1, OnRadioresult1)
- ON_BN_CLICKED(IDC_RADIORESULT2, OnRadioresult2)
- ON_BN_CLICKED(IDC_RADIORESULT3, OnRadioresult3)
- ON_BN_CLICKED(IDC_RADIORESULT4, OnRadioresult4)
- ON_BN_CLICKED(IDC_RADIORESULT5, OnRadioresult5)
- ON_BN_CLICKED(IDC_RADIORESULT6, OnRadioresult6)
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- ON_WM_RBUTTONDOWN()
- ON_BN_CLICKED(IDC_IMAGEINFO, OnImageinfo)
- ON_BN_CLICKED(IDC_VIDEOINFO, OnVideoinfo)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFaceMFC_v1Dlg message handlers
- BOOL CFaceMFC_v1Dlg::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
- ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
- ((CButton*)GetDlgItem(IDC_RADIORESULT1))->SetCheck(TRUE);
-
-
- ((CButton*)GetDlgItem(IDC_FACEDETECT))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_PLAY_PAUSE))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_FACERETRIEVAL))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_UP))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_DOWN))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_TIMESTART))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_TIMEOVER))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_FOLLOW))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_RELOAD))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_IMAGEINFO))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_VIDEOINFO))->EnableWindow(FALSE);
- /*读取软件信息的文本文档*/
- CFile m_file;
- char* pBuf;
- DWORD dwFileLen;
- m_file.Open("SoftInfo.txt",CFile::modeRead);
- dwFileLen=m_file.GetLength();
- pBuf=new char[dwFileLen];
- pBuf[dwFileLen]=0;
- m_file.Read(pBuf,dwFileLen);
- CWnd* pWnd=GetDlgItem(IDC_EDIT_SOFTINFO);
- pWnd->SetWindowText(pBuf);
- //确定鼠标活动的区域
- CWnd* mWnd=GetDlgItem(IDC_IMAGESHOW);
- mWnd->GetClientRect(m_MouseRect);
- mWnd->ClientToScreen(&m_MouseRect);
-
- CRect rect;
- GetClientRect(rect);
- ClientToScreen(&rect);
-
- m_width=m_MouseRect.right-m_MouseRect.left;
- m_height=m_MouseRect.bottom-m_MouseRect.top;
- m_MouseRect.top-=rect.top;
- m_MouseRect.left-=rect.left;
- m_MouseRect.right=m_MouseRect.left+m_width;
- m_MouseRect.bottom=m_MouseRect.top+m_height;
- m_bIsDraging=FALSE;
- //确定鼠标在软件信息栏的区域 edit 2009/5/14
- CWnd* mWnd1=GetDlgItem(IDC_SOFTWAREINFO);
- mWnd1->GetClientRect(m_InfoRect);
- mWnd1->ClientToScreen(&m_InfoRect);
-
- // CRect rect;
- // GetClientRect(rect);
- // ClientToScreen(&rect);
-
- int info_width=m_InfoRect.right-m_InfoRect.left;
- int info_height=m_InfoRect.bottom-m_InfoRect.top;
- m_InfoRect.top-=rect.top;
- m_InfoRect.left-=rect.left;
- m_InfoRect.right=m_InfoRect.left+info_width;
- m_InfoRect.bottom=m_InfoRect.top+info_height;
-
-
- Seperator();
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CFaceMFC_v1Dlg::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 CFaceMFC_v1Dlg::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();
- }
- CWnd* pWnd=GetDlgItem(IDC_IMAGESHOW);
- pWnd->UpdateWindow();
-
- if(mImg)
- {
- DrawPicToHDC(mImg,IDC_IMAGESHOW);
- }
- CWnd* pWnd_face;
- if(img_face[0])
- {
- for(int k=0;k<FaceNum;k++)
- {
- switch(k)
- {
- case 0:
- pWnd_face=GetDlgItem(IDC_FACESHOW1);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(img_face[0],IDC_FACESHOW1);break;
- case 1:pWnd_face=GetDlgItem(IDC_FACESHOW2);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(img_face[1],IDC_FACESHOW2);break;
- case 2:pWnd_face=GetDlgItem(IDC_FACESHOW3);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(img_face[2],IDC_FACESHOW3);break;
- case 3:pWnd_face=GetDlgItem(IDC_FACESHOW4);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(img_face[3],IDC_FACESHOW4);break;
- case 4:pWnd_face=GetDlgItem(IDC_FACESHOW5);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(img_face[4],IDC_FACESHOW5);break;
- case 5:pWnd_face=GetDlgItem(IDC_FACESHOW6);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(img_face[5],IDC_FACESHOW6);break;
- default:break;
- }
- }
- }
- if(ImgPause)
- {
- DrawPicToHDC(ImgPause,IDC_VIDEOSHOW);
- }
- if(ImgFaceResult[0])
- {
- for(int k=0;k<6;k++)
- {
- switch(k)
- {
- case 0:
- pWnd_face=GetDlgItem(IDC_FACERESULT1);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(ImgFaceResult[0],IDC_FACERESULT1);break;
- case 1:pWnd_face=GetDlgItem(IDC_FACERESULT2);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(ImgFaceResult[1],IDC_FACERESULT2);break;
- case 2:pWnd_face=GetDlgItem(IDC_FACERESULT3);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(ImgFaceResult[2],IDC_FACERESULT3);break;
- case 3:pWnd_face=GetDlgItem(IDC_FACERESULT4);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(ImgFaceResult[3],IDC_FACERESULT4);break;
- case 4:pWnd_face=GetDlgItem(IDC_FACERESULT5);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(ImgFaceResult[4],IDC_FACERESULT5);break;
- case 5:pWnd_face=GetDlgItem(IDC_FACERESULT6);
- pWnd_face->UpdateWindow();
- DrawPicToHDC(ImgFaceResult[5],IDC_FACERESULT6);break;
- default:break;
- }
- }
- }
- // if(ImgTemp)
- // {
- // DrawPicToHDC(ImgTemp,IDC_VIDEOSHOW);
- // }
- //2009/4/25修改
- if(m_p1.x&&m_p2.x&&m_p1.y&&m_p2.y)
- {
- CDC* pDC=GetDC();
-
- CPen* pPen=new CPen;
- pPen->CreatePen(PS_SOLID,1,RGB(0,0,255));
- CGdiObject* pOldPen=pDC->SelectObject(pPen);
- CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
- CBrush* pOldBrush=pDC->SelectObject(pBrush);
- pDC->Rectangle(CRect(m_p1.x,m_p1.y,m_p2.x,m_p2.y));
- pDC->SelectObject(pOldBrush);
-
- pDC->SelectObject(pOldPen);
-
- delete pPen;
- ReleaseDC(pDC);
-
- }
-
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CFaceMFC_v1Dlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CFaceMFC_v1Dlg::OnOpenimage()
- {
- CFileDialog dlg(TRUE, _T("*.bmp"), "",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
- "image files (*.bmp; *.jpg;*.jpeg) |*.bmp;*.jpg;*.jpeg|All Files (*.*)|*.*||",NULL);
- char title[]= {"Open Image"};
- dlg.m_ofn.lpstrTitle= title;
-
- if (dlg.DoModal() == IDOK)
- {
- CString path= dlg.GetPathName();
- mImg=cvLoadImage(path);
- ImgData=cvCloneImage(mImg);
- ImgReload=cvCloneImage(mImg);
- //IplImage* img=cvCreateImage(cvSize(mImg->width,mImg->height),IPL_DEPTH_8U,1);
- CreatMask(Mask,50);//创建掩模2009/4/23
- //cvCvtColor(mImg,img,CV_BGR2GRAY);
- if(mImg)
- {
- DrawPicToHDC(mImg,IDC_IMAGESHOW);
- ((CButton*)GetDlgItem(IDC_FACEDETECT))->EnableWindow(TRUE);
- ((CButton*)GetDlgItem(IDC_RELOAD))->EnableWindow(TRUE);
- ((CButton*)GetDlgItem(IDC_IMAGEINFO))->EnableWindow(TRUE);
- }
- }
- }
- void CFaceMFC_v1Dlg::DrawPicToHDC(IplImage *img, UINT ID)
- {
- CDC *pDC = GetDlgItem(ID)->GetDC();
- HDC hDC = pDC->GetSafeHdc();
- CRect rect;
- GetDlgItem(ID)->GetClientRect(&rect);
- CvvImage cimg;
- cimg.CopyOf(img);
- cimg.DrawToHDC(hDC,&rect);
- ReleaseDC(pDC);
- }
- int CFaceMFC_v1Dlg::detect_and_draw(IplImage *img, CvHaarClassifierCascade *cascade)
- {
- storage = cvCreateMemStorage(0);
- int scale = 1;
- CvPoint pt1, pt2;
-
- //CvScalar s;
- int i,N=0;
- //cvPyrDown( img, temp, CV_GAUSSIAN_5x5 );
- cvClearMemStorage(storage);
-
- if(cascade)
- {
-
- CvSeq* faces = cvHaarDetectObjects(img, cascade, storage,
- 1.1, 3, CV_HAAR_DO_CANNY_PRUNING,cvSize(40, 40));
- N=faces?faces->total:0;
-
- for(i=0;i<N;i++)
- {
- CvRect* r = (CvRect*)cvGetSeqElem(faces,i);
- pt1.x = r->x*scale;
- pt2.x = (r->x+r->width)*scale;
- pt1.y = r->y*scale;
- pt2.y = (r->y+r->height)*scale;
- cvSetImageROI(img,*r);
- img_face[i]=cvCloneImage(img);
-
- switch(i)
- {
- case 0:DrawPicToHDC(img_face[0],IDC_FACESHOW1);/*cvSaveImage("E:\1.jpg",img_face[0]);*/break;//2009/4/25修改
- case 1:DrawPicToHDC(img_face[1],IDC_FACESHOW2);/*cvSaveImage("E:\2.jpg",img_face[1]);*/break;
- case 2:DrawPicToHDC(img_face[2],IDC_FACESHOW3);/*cvSaveImage("E:\3.jpg",img_face[2]);*/break;
- case 3:DrawPicToHDC(img_face[3],IDC_FACESHOW4);/*cvSaveImage("E:\4.jpg",img_face[3]);*/break;
- case 4:DrawPicToHDC(img_face[4],IDC_FACESHOW5);/*cvSaveImage("E:\5.jpg",img_face[4]);*/break;
- case 5:DrawPicToHDC(img_face[5],IDC_FACESHOW6);/*cvSaveImage("E:\6.jpg",img_face[5]);*/break;
- default:break;
- }
- cvRectangle(mImg,pt1,pt2,CV_RGB(255,0,0),3,8,0);
- cvResetImageROI(img);//2009/4/25修改
- }
- }
- else
- {
- AfxMessageBox("没有分类器!");
- }
- cvResetImageROI(img);
- if(mImg)
- {
- DrawPicToHDC(mImg,IDC_IMAGESHOW);
- }
-
- return N;
- }
- void CFaceMFC_v1Dlg::OnFacedetect()
- {
- cascade_name="f:\Program Files\OpenCV\data\haarcascades\haarcascade_frontalface_alt2.xml";
- cascade=(CvHaarClassifierCascade*)cvLoad(cascade_name,0,0,0);
- if(!cascade)
- {
- AfxMessageBox("加载分类器失败!");
- }
- else
- {
- if(ImgData)
- {
- FaceNum=detect_and_draw(ImgData,cascade);
- }
- else
- AfxMessageBox("请打开要检测的图片!");
- }
-
- if(FaceNum==0)
- AfxMessageBox("没有检测到人脸!");
- else
- {
- FaceSel[0]=cvCreateImage(cvGetSize(img_face[0]),img_face[0]->depth,1);
- cvCvtColor(img_face[0],FaceSel[0],CV_BGR2GRAY);
- FaceToSel=cvCloneImage(FaceSel[0]);
- }
- InvalidateRect(CRect(10,330,370,585),TRUE);//将上一次检测的结果擦除,重新显示指定区域的背景,这样不会出现上一次检测的结果还在窗口上
- }
- void CFaceMFC_v1Dlg::OnRadioface1()
- {
- m_Facesel=0;
- if(m_Facesel<FaceNum)
- {
- //IplImage* FaceSel=cvCreateImage(cvGetSize(img_face[0]),img_face[0]->depth,1);
- cvCvtColor(img_face[0],FaceSel[0],CV_BGR2GRAY);
- FaceToSel=FaceSel[0];
- }
- }
- void CFaceMFC_v1Dlg::OnRadioface2()
- {
- m_Facesel=1;
- if(m_Facesel<FaceNum)
- {
- FaceSel[1]=cvCreateImage(cvGetSize(img_face[1]),img_face[0]->depth,1);
- cvCvtColor(img_face[1],FaceSel[1],CV_BGR2GRAY);
- FaceToSel=FaceSel[1];
- }
- else
- {
- AfxMessageBox("选择无效!");
- ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
- ((CButton*)GetDlgItem(IDC_RADIOFACE2))->SetCheck(FALSE);
- }
- }
- void CFaceMFC_v1Dlg::OnRadioface3()
- {
- m_Facesel=2;
- if(m_Facesel<FaceNum)
- {
- FaceSel[2]=cvCreateImage(cvGetSize(img_face[2]),img_face[0]->depth,1);
- cvCvtColor(img_face[2],FaceSel[2],CV_BGR2GRAY);
- FaceToSel=FaceSel[2];
- }
- else
- {
- AfxMessageBox("选择无效!");
- ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
- ((CButton*)GetDlgItem(IDC_RADIOFACE3))->SetCheck(FALSE);
- }
- }
- void CFaceMFC_v1Dlg::OnRadioface4()
- {
- m_Facesel=3;
- if(m_Facesel<FaceNum)
- {
- FaceSel[3]=cvCreateImage(cvGetSize(img_face[3]),img_face[0]->depth,1);
- cvCvtColor(img_face[3],FaceSel[3],CV_BGR2GRAY);
- FaceToSel=FaceSel[3];
- }
- else
- {
- AfxMessageBox("选择无效!");
- ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
- ((CButton*)GetDlgItem(IDC_RADIOFACE4))->SetCheck(FALSE);
- }
- }
- void CFaceMFC_v1Dlg::OnRadioface5()
- {
- m_Facesel=4;
- if(m_Facesel<FaceNum)
- {
- FaceSel[4]=cvCreateImage(cvGetSize(img_face[4]),img_face[0]->depth,1);
- cvCvtColor(img_face[4],FaceSel[4],CV_BGR2GRAY);
- FaceToSel=FaceSel[4];
- }
- else
- {
- AfxMessageBox("选择无效!");
- ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
- ((CButton*)GetDlgItem(IDC_RADIOFACE5))->SetCheck(FALSE);
- }
- }
- void CFaceMFC_v1Dlg::OnRadioface6()
- {
- m_Facesel=5;
- if(m_Facesel<FaceNum)
- {
- FaceSel[5]=cvCreateImage(cvGetSize(img_face[5]),img_face[0]->depth,1);
- cvCvtColor(img_face[5],FaceSel[5],CV_BGR2GRAY);
- FaceToSel=FaceSel[5];
- }
- else
- {
- AfxMessageBox("选择无效!");
- ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
- ((CButton*)GetDlgItem(IDC_RADIOFACE6))->SetCheck(FALSE);
- }
- }
- void CFaceMFC_v1Dlg::OnOpenvideo()
- {
- IplImage* frame_1st=0;
- CFileDialog dlg(TRUE, _T("*.avi"), "",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,"avi视频文件 (*.avi) |*.avi|All Files (*.*)|*.*||",NULL);
- char title[]= {"打开视频"};
- dlg.m_ofn.lpstrTitle= title;
- int m_hour,m_minute,m_second;
- char buf_time[200];
- if (dlg.DoModal()==IDOK)
- {
- if(capture)
- {
-
- cvReleaseCapture(&capture);
- count=1;
- m_slider.SetPos(count);
- }
- facedistance.clear();
- frameselect.clear();
- frontposition.clear();
- frontdistances.clear();
- Framefacepos.clear();
- path=dlg.GetPathName();
- capture=cvCaptureFromAVI(path);
- GetDlgItem(IDC_PLAY_PAUSE)->SetWindowText("播放");
- cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,1);
- frame_1st=cvQueryFrame(capture);
- DrawPicToHDC(frame_1st,IDC_VIDEOSHOW);
- numFrames=(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
- framerate=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
- startframe=0;
- overframe=numFrames;
- second[0]=int(overframe/framerate)%60;
- minute[0]=int(overframe/framerate)/60%60;
- hour[0]=int(overframe/framerate)/60/60;
- sprintf(buf_time,"%d:%d:%d",hour[0],minute[0],second[0]);
- m_timedown.SetWindowText(buf_time);
- second[1]=int(startframe/framerate)%60;
- minute[1]=int(startframe/framerate)/60%60;
- hour[1]=int(startframe/framerate)/60/60;
- sprintf(buf_time,"%d:%d:%d",hour[1],minute[1],second[1]);
- m_timeup.SetWindowText(buf_time);
-
- m_second=int(numFrames/framerate)%60;
- m_minute=int(numFrames/framerate)/60%60;
- m_hour=int(numFrames/framerate)/60/60;
- sprintf(buf_time,"%d:%d:%d",m_hour,m_minute,m_second);
- m_totaltime.SetWindowText(buf_time);
- ((CButton*)GetDlgItem(IDC_PLAY_PAUSE))->EnableWindow(TRUE);
- ((CButton*)GetDlgItem(IDC_FACERETRIEVAL))->EnableWindow(TRUE);
- ((CButton*)GetDlgItem(IDC_TIMESTART))->EnableWindow(TRUE);
- ((CButton*)GetDlgItem(IDC_TIMEOVER))->EnableWindow(TRUE);
- ((CButton*)GetDlgItem(IDC_VIDEOINFO))->EnableWindow(TRUE);
-
- if(!capture)
- {
- AfxMessageBox("加载视频失败!!!!");
- }
-
- }
- //hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,NULL,0,&ThreadID);
- m_slider.SetRange(1,numFrames);
- m_slider.SetTicFreq(1);
- }
- void CFaceMFC_v1Dlg::OnPlayPause()
- {
- if(!pausestatus)
- {
- GetDlgItem(IDC_PLAY_PAUSE)->SetWindowText("暂停");
- pausestatus=TRUE;
- }
- else
- {
- GetDlgItem(IDC_PLAY_PAUSE)->SetWindowText("播放");
- pausestatus=FALSE;
- }
- hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,NULL,0,&ThreadID);
-
- }
- //子线程入口
- void ThreadFunc()
- {
- char buf[200];
- IplImage* mImg;
- int hour,minute,second;
- double framerate=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
-
- while(pausestatus)
- {
- cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,count);
- if(capture)
- {
- mImg=cvQueryFrame(capture);
- CDC *pDC = AfxGetApp()->m_pMainWnd->GetDlgItem(IDC_VIDEOSHOW)->GetDC();
- HDC hDC= pDC->GetSafeHdc();
- CRect rect;
- AfxGetApp()->m_pMainWnd->GetDlgItem(IDC_VIDEOSHOW)->GetClientRect(&rect);
- CvvImage cimg;
- cimg.CopyOf(mImg);
- cimg.DrawToHDC(hDC,&rect);
- }
- second=int(count/framerate)%60;
- minute=int(count/framerate)/60%60;
- hour=int(count/framerate)/60/60;
- sprintf(buf,"%d:%d:%d",hour,minute,second);
- ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_PLAYTIME,buf);
- m_slider.SetPos(count);
- count++;
- Sleep(20);
- }
- ImgPause=mImg;
- cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,count);
- }
- void CFaceMFC_v1Dlg::OnCustomdrawSlider(NMHDR* pNMHDR, LRESULT* pResult)
- {
- count=m_slider.GetPos();
- UpdateData(FALSE);
- *pResult = 0;
- }
- void CFaceMFC_v1Dlg::OnTimestart()
- {
- char buf_time[200];
- startframe=m_slider.GetPos();
- second[1]=int(startframe/framerate)%60;
- minute[1]=int(startframe/framerate)/60%60;
- hour[1]=int(startframe/framerate)/60/60;
- sprintf(buf_time,"%d:%d:%d",hour[1],minute[1],second[1]);
- m_timeup.SetWindowText(buf_time);
- }
- void CFaceMFC_v1Dlg::OnTimeover()
- {
- char buf_time[200];
- overframe=m_slider.GetPos();
- if(overframe<=startframe)
- {
- AfxMessageBox("选择不合法!");
- }
- else
- {
- second[0]=int(overframe/framerate)%60;
- minute[0]=int(overframe/framerate)/60%60;
- hour[0]=int(overframe/framerate)/60/60;
- sprintf(buf_time,"%d:%d:%d",hour[0],minute[0],second[0]);
- m_timedown.SetWindowText(buf_time);
- }
- }
- void CFaceMFC_v1Dlg::OnKillfocusEditStep()
- {
- UpdateData(TRUE);
- }
- void CFaceMFC_v1Dlg::OnFaceretrieval()
- {
- double time1=0,time2=0;
- IplImage* FaceInput;
- FaceInput=cvCreateImage(cvSize(100,100),FaceToSel->depth,FaceToSel->nChannels);
- cvResize(FaceToSel,FaceInput,CV_INTER_LINEAR);
-
- time1=(double)cvGetTickCount();
- facedistance.clear();
- frameselect.clear();
- frontposition.clear();
- frontdistances.clear();
- Framefacepos.clear();
- int possiblefaces = 12;
- //int framestep =25;
- //IplImage *pImg;
- IplImage *pFrame;
- IplImage *framefaces[50];
- CvMat *framemat[50];
-
- int width=int(cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH));
- int height=int(cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT));
- Do2DPCA(FaceInput);
- m_slider.SetRange(1,overframe-startframe,TRUE);
- //m_slider.SetTicFreq(m_framestep);
- m_slider.SetPos(1);
- int slider=1;
- for(int i=startframe;i<overframe;i+=m_framestep)
- {
- IplImage *grayframe=cvCreateImage(cvSize(width,height),8,1);
- CvMat *frametemp1=cvCreateMat(100,100,CV_8UC1);
- CvMat *y_y=cvCreateMat(100,1,CV_64FC1);
- double distance;
- vector<CvRect *> faceposition;
- cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,i);
- pFrame=cvQueryFrame(capture);
- cvCvtColor(pFrame,grayframe,CV_BGR2GRAY);
-
- cvFlip(grayframe,grayframe,0);
-
-
- int n = FaceRecgnition(grayframe,cascade,faceposition);
-
- for (int j = 0;j < n; j++)
- {
- CvRect *r = faceposition[j];
- Framefacepos.push_back(r);
- cvSetImageROI(grayframe,*r);
- framefaces[j] = cvCreateImage(cvSize(100,100),grayframe->depth,grayframe->nChannels);
- cvResize(grayframe, framefaces[j], CV_INTER_LINEAR);
- cvResetImageROI(grayframe);//2009/4/25修改
- }
-
- for ( int k = 0;k < n; k++)
- {
-
- framemat[k] = cvCreateMat(100,100,CV_64FC1);
- cvGetMat(framefaces[k],frametemp1);
- //cvMul(frametemp1,Mask,frametemp1);//2009/4/23添加
- cvConvertScale(frametemp1,framemat[k]);
-
- //cvSub(framemat[k],Meanface,framemat[k]);
- cvmMul(framemat[k],EigenFace,y_y);
-
- distance = cvNorm(ProjectY,y_y,CV_L2);
-
- facedistance.push_back(distance);
- frameselect.push_back(i);
-
- }
- slider++;
- m_slider.SetPos(slider*m_framestep);
- }
- Findneighbors(facedistance,possiblefaces,frontposition);
-
- char buffer[10];
- sprintf(buffer,"距离是:%f",frontdistances[1]);
- if(frontdistances[1]<3000)
- AfxMessageBox(buffer);
- for (int m=0; m<possiblefaces;m++)
- {
- int selectframe = frameselect[frontposition[m]];
- int scale = 1;
- CvRect *r = Framefacepos[frontposition[m]];
-
- cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,selectframe);
- pFrame = cvQueryFrame(capture);
- cvFlip(pFrame,pFrame,0);
- cvSetImageROI(pFrame,*r);
- cvFlip(pFrame,pFrame,0);
- ImgFaceResult[m]=cvCloneImage(pFrame);
-
- switch(m)
- {
- case 0:DrawPicToHDC(ImgFaceResult[0],IDC_FACERESULT1);break;
- case 1:DrawPicToHDC(ImgFaceResult[1],IDC_FACERESULT2);break;
- case 2:DrawPicToHDC(ImgFaceResult[2],IDC_FACERESULT3);break;
- case 3:DrawPicToHDC(ImgFaceResult[3],IDC_FACERESULT4);break;
- case 4:DrawPicToHDC(ImgFaceResult[4],IDC_FACERESULT5);break;
- case 5:DrawPicToHDC(ImgFaceResult[5],IDC_FACERESULT6);break;
- }
- cvResetImageROI(pFrame);
- }
- ((CButton*)GetDlgItem(IDC_DOWN))->EnableWindow(TRUE);
- ((CButton*)GetDlgItem(IDC_FOLLOW))->EnableWindow(TRUE);
- count=0;
- /*int hour,minute,second;
- char buf_frame[10];
- aimframe=frameselect[frontposition[0]];
- second=int(aimframe/framerate)%60;
- minute=int(aimframe/framerate)/60%60;
- hour=int(aimframe/framerate)/60/60;
- sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
- ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame); */
- ((CButton*)GetDlgItem(IDC_UP))->EnableWindow(FALSE);
- time2=(double)cvGetTickCount();
- time1=time2-time1;
- time1=time1/((double)cvGetTickFrequency()*1000.);
- char buf_time[20];
- sprintf(buf_time,"检索时间为:%fms",time1);
- AfxMessageBox(buf_time);
- }
- void CFaceMFC_v1Dlg::Do2DPCA(IplImage *face)
- {
- CvMat* Matface = cvCreateMat(100,100,CV_64FC1);
- CvMat* Svdface = cvCreateMat(100,100,CV_64FC1);
- CvMat* EigenVec = cvCreateMat(100,100,CV_64FC1);
- CvMat* EigenValue = cvCreateMat(100,1,CV_64FC1);
-
- CvMat *temp1 = cvCreateMat(100,100,CV_8UC1);
- CvMat *temp2 = cvCreateMat(100,100,CV_64FC1);
- CvMat *temp2_T = cvCreateMat(100,100,CV_64FC1);
- CvMat *temp3 = cvCreateMat(100,100,CV_64FC1);
- CvMat *temp4 = cvCreateMat(100,100,CV_64FC1);
- CvMat *temp4_T = cvCreateMat(100,100,CV_64FC1);
- CvMat *temp5 = cvCreateMat(100,100,CV_64FC1);
- CvMat *G = cvCreateMat(100,100,CV_64FC1);
-
-
- cvGetMat(face,temp1);
- //cvMul(temp1,Mask,temp1);//2009/4/23添加
- cvConvertScale(temp1,Matface);
-
-
- double scale = 1.2;
- MatSVD(Matface,Svdface,scale);
-
- cvAdd(Matface,Svdface,Meanface);
- cvConvertScale(Meanface,Meanface,0.5,0);
-
- cvSub(Matface,Meanface,temp2);
- cvTranspose(temp2,temp2_T);
- cvmMul(temp2_T,temp2,temp3);
-
- cvSub(Svdface,Meanface,temp4);
- cvTranspose(temp4,temp4_T);
- cvmMul(temp4_T,temp4,temp5);
-
- cvAdd(temp3,temp5,G);
- cvConvertScale(G,G,0.5,0);
-
- cvEigenVV(G,EigenVec,EigenValue,DBL_EPSILON);
-
- for (int j = 0;j < 100; j++)
- {
- cvSetReal2D(EigenFace,j,0,cvGetReal2D(EigenVec,0,j));
- }
- cvmMul(Matface,EigenFace,ProjectY);
- return;
- }
- void CFaceMFC_v1Dlg::MatSVD(CvMat *src, CvMat *dst, double &scale)
- {
- CvMat *W=cvCreateMat(src->rows,src->cols,CV_64FC1);
- CvMat *V=cvCreateMat(src->cols,src->cols,CV_64FC1);
- CvMat *U=cvCreateMat(src->rows,src->rows,CV_64FC1);
- CvMat *V_T=cvCreateMat(src->cols,src->cols,CV_64FC1);
-
- cvSVD(src,W,U,V);
-
- for(int i = 0;i < src->rows;i++)
- {
- double temp = pow (cvGetReal2D(W,i,i),scale);
- cvSetReal2D(W,i,i,temp);
- }
- cvmMul(U,W,dst);
- cvTranspose(V,V_T);
- cvmMul(dst,V_T,dst);
- }
- int CFaceMFC_v1Dlg::FaceRecgnition(IplImage* img,CvHaarClassifierCascade* cascade,vector<CvRect *> &faceposition)
- {
- storage = cvCreateMemStorage(0);
- //int scale = 1;
- //IplImage* temp=cvCreateImage( cvSize(img->width,img->height), 8, 1 );
- int N = 0;
-
- cvClearMemStorage( storage );
-
- if( cascade )
- {
-
- CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(20, 20));
-
- N=faces ? faces->total:0;
- for(int i = 0; i < (faces ? faces->total : 0); i++ )
- {
- CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
- faceposition.push_back( r);
- }
- }
- else
- {
- AfxMessageBox("无分类器!");
- }
-
- return N;
- }
- void CFaceMFC_v1Dlg::Findneighbors(vector<double> v,int n,vector<int> &frontposition)
- {
- if ( n > v.size())
- {
- return;
- }
-
- for (int i = 0;i < n ;i++)
- {
- double smallest;
-
- int pos = FindSmallest(v,smallest);
- frontposition.push_back(pos);
- frontdistances.push_back(smallest);
-
- v[pos] = maxnum;
- }
- return;
- }
- int CFaceMFC_v1Dlg::FindSmallest(vector<double> v,double &smallest)
- {
- int n = 0;
- smallest = v[0];
- for (int i = 1;i < v.size();i++)
- {
- if (v[i] < smallest)
- {
- smallest = v[i];
- n = i ;
- }
- }
- return n;
- }
- void CFaceMFC_v1Dlg::OnDown()
- {
- ((CButton*)GetDlgItem(IDC_DOWN))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_UP))->EnableWindow(TRUE);
-
- for(int i=0;i<6;i++)
- {
- ImgFaceTemp[i]=ImgFaceResult[i];
- ImgFaceResult[i]=ImgFaceResult[i+6];
- }
- for(int j=0;j<6;j++)
- {
- frame_time[j]=frameselect[frontposition[j]];
- frameselect[frontposition[j]]=frameselect[frontposition[j+6]];
- }
- OnPaint();
- }
- void CFaceMFC_v1Dlg::OnUp()
- {
- ((CButton*)GetDlgItem(IDC_UP))->EnableWindow(FALSE);
- ((CButton*)GetDlgItem(IDC_DOWN))->EnableWindow(TRUE);
- for(int i=0;i<6;i++)
- {
- ImgFaceResult[i+6]=ImgFaceResult[i];
- ImgFaceResult[i]=ImgFaceTemp[i];
- }
- for(int j=0;j<6;j++)
- {
- frameselect[frontposition[j+6]]=frameselect[frontposition[j]];
- frameselect[frontposition[j]]=frame_time[j];
- }
- OnPaint();
- }
- void CFaceMFC_v1Dlg::OnFollow()
- {
- cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,aimframe);
- //ImgTemp=cvQueryFrame(capture);
- ImgPause=cvQueryFrame(capture);//2009/4/25修改
- //DrawPicToHDC(ImgTemp,IDC_VIDEOSHOW);
- DrawPicToHDC(ImgPause,IDC_VIDEOSHOW);//2009/4/25修改
- count=aimframe;
- m_slider.SetPos(count);
- }
- void CFaceMFC_v1Dlg::OnRadioresult1()
- {
- int hour,minute,second;
- char buf_frame[10];
- aimframe=frameselect[frontposition[0]];
- second=int(aimframe/framerate)%60;
- minute=int(aimframe/framerate)/60%60;
- hour=int(aimframe/framerate)/60/60;
- sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
- ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame);
- /*sprintf(buf_frame,"%d",aimframe);
- AfxMessageBox(buf_frame);*/
-
- }
- void CFaceMFC_v1Dlg::OnRadioresult2()
- {
- int hour,minute,second;
- char buf_frame[10];
- aimframe=frameselect[frontposition[1]];
- second=int(aimframe/framerate)%60;
- minute=int(aimframe/framerate)/60%60;
- hour=int(aimframe/framerate)/60/60;
- sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
- ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame);
- /*char buf_frame[10];
- sprintf(buf_frame,"%d",aimframe);
- AfxMessageBox(buf_frame);*/
- }
- void CFaceMFC_v1Dlg::OnRadioresult3()
- {
- int hour,minute,second;
- char buf_frame[10];
- aimframe=frameselect[frontposition[2]];
- second=int(aimframe/framerate)%60;
- minute=int(aimframe/framerate)/60%60;
- hour=int(aimframe/framerate)/60/60;
- sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
- ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame);
- /*char buf_frame[10];
- sprintf(buf_frame,"%d",aimframe);
- AfxMessageBox(buf_frame);*/
- }
- void CFaceMFC_v1Dlg::OnRadioresult4()
- {
- int hour,minute,second;
- char buf_frame[10];
- aimframe=frameselect[frontposition[3]];
- second=int(aimframe/framerate)%60;
- minute=int(aimframe/framerate)/60%60;
- hour=int(aimframe/framerate)/60/60;
- sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
- ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame);
- /*char buf_frame[10];
- sprintf(buf_frame,"%d",aimframe);
- AfxMessageBox(buf_frame);*/
- }
- void CFaceMFC_v1Dlg::OnRadioresult5()
- {
- int hour,minute,second;
- char buf_frame[10];
- aimframe=frameselect[frontposition[4]];
- second=int(aimframe/framerate)%60;
- minute=int(aimframe/framerate)/60%60;
- hour=int(aimframe/framerate)/60/60;
- sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
- ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame);
- /*char buf_frame[10];
- sprintf(buf_frame,"%d",aimframe);
- AfxMessageBox(buf_frame);*/
- }
- void CFaceMFC_v1Dlg::OnRadioresult6()
- {
- int hour,minute,second;
- char buf_frame[10];
- aimframe=frameselect[frontposition[5]];
- second=int(aimframe/framerate)%60;
- minute=int(aimframe/framerate)/60%60;
- hour=int(aimframe/framerate)/60/60;
- sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
- ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame);
- /*char buf_frame[10];
- sprintf(buf_frame,"%d",aimframe);
- AfxMessageBox(buf_frame);*/
- }
- //鼠标左击开始绘制矩形
- void CFaceMFC_v1Dlg::OnLButtonDown(UINT nFlags, CPoint point)
- {
- if(m_MouseRect.PtInRect(point))
- {
- temp_p1=m_p1;
- temp_p2=m_p2;
- m_p1=point;
- m_bIsDraging=TRUE;
- //m_bDrawed=FALSE;
- ::SetCursor(::LoadCursor(NULL,IDC_CROSS));
- SetCapture();
- switch(m_iIsDraging)
- {
- case 1:
- ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));break;
- case 2:
- ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));break;
- }
-
- if((point.x>temp_p1.x-3)&&(point.x<temp_p1.x+3)&&(point.y<temp_p1.y+3)&&(point.y>temp_p1.y-3))
- {
- ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));
- m_iIsDraging=1;
- m_bIsDraging=FALSE;
- }
- else
- m_bIsDraging=TRUE;
- if((point.x>temp_p1.x+10)&&(point.x<temp_p2.x-10)&&(point.y<temp_p1.y+2)&&(point.y>temp_p1.y-2))
- {
- ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
- m_iIsDraging=2;
- m_bIsDraging=FALSE;
- temp_p3=point;
- }
-
- }
-
- CDialog::OnLButtonDown(nFlags, point);
- }
- //鼠标左键弹起来表示绘制图形结束
- void CFaceMFC_v1Dlg::OnLButtonUp(UINT nFlags, CPoint point)
- {
- if(m_bIsDraging)
- {
- m_p2=point;
- }
- ::ReleaseCapture();
- m_bIsDraging=FALSE;
- m_iIsDraging=0;
- ::SetCursor(::LoadCursor(NULL,IDC_CROSS));
-
- CDC* pDC=GetDC();
- CPen* pPen=new CPen;
- pPen->CreatePen(PS_SOLID,1,RGB(0,255,0));
- CGdiObject* pOldPen=pDC->SelectObject(pPen);
-
- pDC->Rectangle(m_p1.x-4,m_p1.y-4,m_p1.x+4,m_p1.y+4);
- //pDC->Rectangle(m_p2.x-4,m_p1.y-4,m_p2.x+4,m_p1.y+4);
- //pDC->Rectangle(m_p2.x-4,m_p2.y-4,m_p2.x+4,m_p2.y+4);
- //pDC->Rectangle(m_p1.x-4,m_p2.y-4,m_p1.x+4,m_p2.y+4);
-
- CDialog::OnLButtonUp(nFlags, point);
- }
- //通过鼠标移动来选择矩形区域
- void CFaceMFC_v1Dlg::OnMouseMove(UINT nFlags, CPoint point)
- {
- if(m_MouseRect.PtInRect(point))
- {
- ::SetCursor(::LoadCursor(NULL,IDC_CROSS));
- if(m_iIsDraging==1)
- ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));
- if(m_iIsDraging==2)
- ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
-
- if(m_bIsDraging)
- {
- CDC* pDC=GetDC();
- CPen* pPen=new CPen;
- pPen->CreatePen(PS_SOLID,1,RGB(0,0,255));
- CGdiObject* pOldPen=pDC->SelectObject(pPen);
-
- m_p2=point;
- CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
- CBrush* pOldBrush=pDC->SelectObject(pBrush);
- pDC->Rectangle(CRect(m_p1.x,m_p1.y,m_p2.x,m_p2.y));
- pDC->SelectObject(pOldBrush);
-
- pDC->SelectObject(pOldPen);
- Invalidate(FALSE);
-
- delete pPen;
- ReleaseDC(pDC);
- }
-
- if((point.x>m_p1.x-3)&&(point.x<m_p1.x+3)&&(point.y<m_p1.y+3)&&(point.y>m_p1.y-3))
- {
- ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));
- }
-
- if((point.x>m_p1.x+10)&&(point.x<m_p2.x-10)&&(point.y<m_p1.y+2)&&(point.y>m_p1.y-2))
- {
- ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
- }
-
- if(m_iIsDraging==1)
- {
- Invalidate(FALSE);
- ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));
- CDC* pDC=GetDC();
-
- CPen* pPen=new CPen;
- pPen->CreatePen(PS_SOLID,1,RGB(0,0,255));
- CGdiObject* pOldPen=pDC->SelectObject(pPen);
-
- m_p1=point;
- CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
- CBrush* pOldBrush=pDC->SelectObject(pBrush);
- pDC->Rectangle(CRect(m_p1.x,m_p1.y,m_p2.x,m_p2.y));
- pDC->SelectObject(pOldBrush);
-
- pDC->SelectObject(pOldPen);
-
- delete pPen;
- ReleaseDC(pDC);
-
- }
-
- if(m_iIsDraging==2)
- {
- Invalidate(FALSE);
- ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
- CDC* pDC=GetDC();
-
- CPen* pPen=new CPen;
- pPen->CreatePen(PS_SOLID,1,RGB(0,0,255));
- CGdiObject* pOldPen=pDC->SelectObject(pPen);
-
- int x1=point.x-temp_p3.x;
- int y1=point.y-temp_p3.y;
-
- m_p1=temp_p1;
- m_p2=temp_p2;
-
- m_p1.x=m_p1.x+x1;
- m_p1.y=m_p1.y+y1;
- m_p2.x=m_p2.x+x1;
- m_p2.y=m_p2.y+y1;
- temp_p1=m_p1;
- temp_p2=m_p2;
- temp_p3=point;
- CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
- CBrush* pOldBrush=pDC->SelectObject(pBrush);
- pDC->Rectangle(CRect(m_p1.x,m_p1.y,m_p2.x,m_p2.y));
- pDC->SelectObject(pOldBrush);
- pDC->SelectObject(pOldPen);
-
- delete pPen;
- ReleaseDC(pDC);
-
- }
-
- }
- else
- {
- if(m_bIsDraging)
- {
-
- ::SetCursor(::LoadCursor(NULL,IDC_NO));
- }
-
- }
- if(m_InfoRect.PtInRect(point))
- {
- ::SetCursor(::LoadCursor(NULL,IDC_CROSS));
- Seperator();
- }
- else
- merge();
- CDialog::OnMouseMove(nFlags, point);
- }
- //右击选择被鼠标框中的区域
- void CFaceMFC_v1Dlg::OnRButtonDown(UINT nFlags, CPoint point)
- {
- if((point.x>m_p1.x)&&(point.x<m_p2.x)&&(point.y>m_p1.y)&&(point.y<m_p2.y))
- {
-
- imgwidth=m_p1.x-m_MouseRect.left;
- imgheight=m_p1.y-m_MouseRect.top;
- rectwidth=m_p2.x-m_p1.x;
- rectheight=m_p2.y-m_p1.y;
-
- imgwidth=int(double(imgwidth)/m_width*(mImg->width));
- imgheight=int(double(imgheight)/m_height*(mImg->height));
- rectwidth=int(double(rectwidth)/m_width*(mImg->width));
- rectheight=int(double(rectheight)/m_height*(mImg->height));
- CvRect r=cvRect(imgwidth,imgheight,rectwidth,rectheight);
- cvSetImageROI(ImgData,r);
- IplImage* temp=cvCreateImage(cvSize(r.width,r.height),ImgData->depth,ImgData->nChannels);
- cvCopyImage(ImgData,temp);
- mImg=cvCloneImage(temp);
- //cvSaveImage("E:\ss.jpg",mImg);//2009/4/25修改
- DrawPicToHDC(mImg,IDC_IMAGESHOW);
-
- ImgData=cvCloneImage(mImg);
- //width=mImg->width;
- //height=mImg->height;
- cvResetImageROI(mImg);
- }
- m_p1=NULL;
- m_p2=NULL;
-
- CDialog::OnRButtonDown(nFlags, point);
- }
- //重新加载图像
- void CFaceMFC_v1Dlg::OnReload()
- {
- mImg=cvCloneImage(ImgReload);
- ImgData=cvCloneImage(ImgReload);
- DrawPicToHDC(mImg,IDC_IMAGESHOW);
- }
- //显示图像的相关信息
- void CFaceMFC_v1Dlg::OnImageinfo()
- {
- int width_info=ImgData->width;
- int height_info=ImgData->height;
- int channel_info=ImgData->nChannels;
- int depth_info=ImgData->depth;
- char buf_info[100];
- sprintf(buf_info,"图像的宽度: %dn图像的高度: %dn通道数: %dn位深: %d",width_info,height_info,channel_info,depth_info);
- AfxMessageBox(buf_info);
- }
- //显示视频的相关信息
- void CFaceMFC_v1Dlg::OnVideoinfo()
- {
- int video_width=cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
- int video_height=cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT);
- int frame_rate=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
- char video_info[100];
- sprintf(video_info,"视频的宽度: %dn视频的高度: %dn帧速率: %d",video_width,video_height,frame_rate);
- AfxMessageBox(video_info);
-
- }
- void CFaceMFC_v1Dlg::CreatMask(CvMat* Mask,double r)
- {
- //CvMat *Mask = cvCreateMat(facesize.height,facesize.width,CV_8UC1);
- cvSet(Mask,cvScalarAll(1),0);
-
- for (float i = 50; i <49 ;i ++)
- {
- for (float j = 0; j < 49;j++)
- {
- if ( pow(double((i - 50 + 1)),2) + pow(double((j- 502 +1)),2) > pow(r,2))
- {
- cvSetReal2D(Mask,i,j,0);
- }
- }
- }
-
- }
- void CFaceMFC_v1Dlg::Seperator() //分离
- {
- static CRect rectLarge;
- static CRect rectSmall;
-
- if(rectLarge.IsRectNull())
- {
- CRect rectSeparator;
- GetWindowRect(&rectLarge);
- GetDlgItem(IDC_SEPERATOR)->GetWindowRect(&rectSeparator);
-
- rectSmall.left=rectLarge.left;
- rectSmall.top=rectLarge.top;
- rectSmall.right=rectLarge.right;
- rectSmall.bottom=rectSeparator.bottom;
- }
- SetWindowPos(NULL,0,0,rectLarge.Width(),rectLarge.Height(),SWP_NOMOVE | SWP_NOZORDER);
- }
- void CFaceMFC_v1Dlg::merge() //合并
- {
- static CRect rectLarge;
- static CRect rectSmall;
-
- if(rectLarge.IsRectNull())
- {
- CRect rectSeparator;
- GetWindowRect(&rectLarge);
- GetDlgItem(IDC_SEPERATOR)->GetWindowRect(&rectSeparator);
-
- rectSmall.left=rectLarge.left;
- rectSmall.top=rectLarge.top;
- rectSmall.right=rectLarge.right;
- rectSmall.bottom=rectSeparator.bottom;
- }
- SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),SWP_NOMOVE | SWP_NOZORDER);
- }