FaceMFC_v1Dlg.cpp
上传用户:xiaoge123
上传日期:2022-08-05
资源大小:5794k
文件大小:40k
源码类别:

OpenCV

开发平台:

Visual C++

  1. // FaceMFC_v1Dlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "FaceMFC_v1.h"
  5. #include "FaceMFC_v1Dlg.h"
  6. #include "Face.h"
  7. #include <vector>
  8. using namespace std;
  9. #define maxnum 1.7976931348623158e+308
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAboutDlg dialog used for App About
  17. volatile BOOL pausestatus;
  18. int count;
  19. CvCapture* capture;
  20. CString path;
  21. CSliderCtrl m_slider;
  22. CSliderCtrl m_slider_distance;
  23. IplImage* ImgPause;
  24. CvMat *Mask = cvCreateMat(100,100,CV_8UC1);
  25. CvMat *Meanface   =  cvCreateMat(100,100,CV_64FC1);
  26. CvMat *ProjectY   =  cvCreateMat(100,1,CV_64FC1);
  27. CvMat *EigenFace  =  cvCreateMat(100,1,CV_64FC1);
  28. vector<double> facedistance;        //??????
  29. vector<int>    frameselect;         //?????
  30. vector<int>    frontposition;       //??????????????
  31. vector<double> frontdistances;      //??????????????
  32. vector<CvRect *> Framefacepos;
  33. class CAboutDlg : public CDialog
  34. {
  35. public:
  36. CAboutDlg();
  37. // Dialog Data
  38. //{{AFX_DATA(CAboutDlg)
  39. enum { IDD = IDD_ABOUTBOX };
  40. //}}AFX_DATA
  41. // ClassWizard generated virtual function overrides
  42. //{{AFX_VIRTUAL(CAboutDlg)
  43. protected:
  44. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  45. //}}AFX_VIRTUAL
  46. // Implementation
  47. protected:
  48. //{{AFX_MSG(CAboutDlg)
  49. //}}AFX_MSG
  50. DECLARE_MESSAGE_MAP()
  51. };
  52. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  53. {
  54. //{{AFX_DATA_INIT(CAboutDlg)
  55. //}}AFX_DATA_INIT
  56. }
  57. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  58. {
  59. CDialog::DoDataExchange(pDX);
  60. //{{AFX_DATA_MAP(CAboutDlg)
  61. //}}AFX_DATA_MAP
  62. }
  63. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  64. //{{AFX_MSG_MAP(CAboutDlg)
  65. // No message handlers
  66. //}}AFX_MSG_MAP
  67. END_MESSAGE_MAP()
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CFaceMFC_v1Dlg dialog
  70. CFaceMFC_v1Dlg::CFaceMFC_v1Dlg(CWnd* pParent /*=NULL*/)
  71. : CDialog(CFaceMFC_v1Dlg::IDD, pParent)
  72. {
  73. //{{AFX_DATA_INIT(CFaceMFC_v1Dlg)
  74. m_framestep =25;
  75. mImg=NULL;
  76. ImgData=NULL;
  77. m_Facesel=1;
  78. FaceNum=0;
  79. ImgPause=NULL;
  80. ImgTemp=NULL;
  81. m_p1=NULL;
  82. m_p2=NULL;
  83. m_iIsDraging=0;
  84. ImgFaceResult[0]=NULL;
  85. //}}AFX_DATA_INIT
  86. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  87. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  88. }
  89. void CFaceMFC_v1Dlg::DoDataExchange(CDataExchange* pDX)
  90. {
  91. CDialog::DoDataExchange(pDX);
  92. //{{AFX_DATA_MAP(CFaceMFC_v1Dlg)
  93. DDX_Control(pDX, IDC_EDIT_TOTALTIME, m_totaltime);
  94. DDX_Control(pDX, IDC_EDIT_TIMEDOWN, m_timedown);
  95. DDX_Control(pDX, IDC_EDIT_TIMEUP, m_timeup);
  96. DDX_Text(pDX, IDC_EDIT_STEP, m_framestep);
  97. DDX_Control(pDX, IDC_SLIDER, m_slider);
  98. DDX_Control(pDX, IDC_SLIDER_DISTANCE, m_slider_distance);
  99. //}}AFX_DATA_MAP
  100. }
  101. BEGIN_MESSAGE_MAP(CFaceMFC_v1Dlg, CDialog)
  102. //{{AFX_MSG_MAP(CFaceMFC_v1Dlg)
  103. ON_WM_SYSCOMMAND()
  104. ON_WM_PAINT()
  105. ON_WM_QUERYDRAGICON()
  106. ON_BN_CLICKED(IDC_OPENIMAGE, OnOpenimage)
  107. ON_BN_CLICKED(IDC_FACEDETECT, OnFacedetect)
  108. ON_BN_CLICKED(IDC_RADIOFACE1, OnRadioface1)
  109. ON_BN_CLICKED(IDC_RADIOFACE2, OnRadioface2)
  110. ON_BN_CLICKED(IDC_RADIOFACE3, OnRadioface3)
  111. ON_BN_CLICKED(IDC_RADIOFACE4, OnRadioface4)
  112. ON_BN_CLICKED(IDC_RADIOFACE5, OnRadioface5)
  113. ON_BN_CLICKED(IDC_RADIOFACE6, OnRadioface6)
  114. ON_BN_CLICKED(IDC_OPENVIDEO, OnOpenvideo)
  115. ON_BN_CLICKED(IDC_RELOAD, OnReload)
  116. ON_BN_CLICKED(IDC_PLAY_PAUSE, OnPlayPause)
  117. ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER, OnCustomdrawSlider)
  118. ON_BN_CLICKED(IDC_TIMESTART, OnTimestart)
  119. ON_BN_CLICKED(IDC_TIMEOVER, OnTimeover)
  120. ON_EN_KILLFOCUS(IDC_EDIT_STEP, OnKillfocusEditStep)
  121. ON_BN_CLICKED(IDC_FACERETRIEVAL, OnFaceretrieval)
  122. ON_BN_CLICKED(IDC_DOWN, OnDown)
  123. ON_BN_CLICKED(IDC_UP, OnUp)
  124. ON_BN_CLICKED(IDC_FOLLOW, OnFollow)
  125. ON_BN_CLICKED(IDC_RADIORESULT1, OnRadioresult1)
  126. ON_BN_CLICKED(IDC_RADIORESULT2, OnRadioresult2)
  127. ON_BN_CLICKED(IDC_RADIORESULT3, OnRadioresult3)
  128. ON_BN_CLICKED(IDC_RADIORESULT4, OnRadioresult4)
  129. ON_BN_CLICKED(IDC_RADIORESULT5, OnRadioresult5)
  130. ON_BN_CLICKED(IDC_RADIORESULT6, OnRadioresult6)
  131. ON_WM_LBUTTONDOWN()
  132. ON_WM_LBUTTONUP()
  133. ON_WM_MOUSEMOVE()
  134. ON_WM_RBUTTONDOWN()
  135. ON_BN_CLICKED(IDC_IMAGEINFO, OnImageinfo)
  136. ON_BN_CLICKED(IDC_VIDEOINFO, OnVideoinfo)
  137. //}}AFX_MSG_MAP
  138. END_MESSAGE_MAP()
  139. /////////////////////////////////////////////////////////////////////////////
  140. // CFaceMFC_v1Dlg message handlers
  141. BOOL CFaceMFC_v1Dlg::OnInitDialog()
  142. {
  143. CDialog::OnInitDialog();
  144. // Add "About..." menu item to system menu.
  145. // IDM_ABOUTBOX must be in the system command range.
  146. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  147. ASSERT(IDM_ABOUTBOX < 0xF000);
  148. CMenu* pSysMenu = GetSystemMenu(FALSE);
  149. if (pSysMenu != NULL)
  150. {
  151. CString strAboutMenu;
  152. strAboutMenu.LoadString(IDS_ABOUTBOX);
  153. if (!strAboutMenu.IsEmpty())
  154. {
  155. pSysMenu->AppendMenu(MF_SEPARATOR);
  156. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  157. }
  158. }
  159. // Set the icon for this dialog.  The framework does this automatically
  160. //  when the application's main window is not a dialog
  161. SetIcon(m_hIcon, TRUE); // Set big icon
  162. SetIcon(m_hIcon, FALSE); // Set small icon
  163. // TODO: Add extra initialization here
  164. ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
  165. ((CButton*)GetDlgItem(IDC_RADIORESULT1))->SetCheck(TRUE);
  166. ((CButton*)GetDlgItem(IDC_FACEDETECT))->EnableWindow(FALSE);
  167. ((CButton*)GetDlgItem(IDC_PLAY_PAUSE))->EnableWindow(FALSE);
  168. ((CButton*)GetDlgItem(IDC_FACERETRIEVAL))->EnableWindow(FALSE);
  169. ((CButton*)GetDlgItem(IDC_UP))->EnableWindow(FALSE);
  170. ((CButton*)GetDlgItem(IDC_DOWN))->EnableWindow(FALSE);
  171. ((CButton*)GetDlgItem(IDC_TIMESTART))->EnableWindow(FALSE);
  172. ((CButton*)GetDlgItem(IDC_TIMEOVER))->EnableWindow(FALSE);
  173. ((CButton*)GetDlgItem(IDC_FOLLOW))->EnableWindow(FALSE);
  174. ((CButton*)GetDlgItem(IDC_RELOAD))->EnableWindow(FALSE);
  175. ((CButton*)GetDlgItem(IDC_IMAGEINFO))->EnableWindow(FALSE);
  176. ((CButton*)GetDlgItem(IDC_VIDEOINFO))->EnableWindow(FALSE);
  177. /*读取软件信息的文本文档*/
  178. CFile m_file;
  179. char* pBuf;
  180. DWORD dwFileLen;
  181. m_file.Open("SoftInfo.txt",CFile::modeRead);
  182. dwFileLen=m_file.GetLength();
  183. pBuf=new char[dwFileLen];
  184. pBuf[dwFileLen]=0;
  185. m_file.Read(pBuf,dwFileLen);
  186. CWnd* pWnd=GetDlgItem(IDC_EDIT_SOFTINFO);
  187. pWnd->SetWindowText(pBuf);
  188. //确定鼠标活动的区域
  189. CWnd* mWnd=GetDlgItem(IDC_IMAGESHOW);
  190. mWnd->GetClientRect(m_MouseRect);
  191. mWnd->ClientToScreen(&m_MouseRect);
  192. CRect rect;
  193. GetClientRect(rect);
  194. ClientToScreen(&rect);
  195. m_width=m_MouseRect.right-m_MouseRect.left;
  196. m_height=m_MouseRect.bottom-m_MouseRect.top;
  197. m_MouseRect.top-=rect.top;
  198. m_MouseRect.left-=rect.left;
  199. m_MouseRect.right=m_MouseRect.left+m_width;
  200. m_MouseRect.bottom=m_MouseRect.top+m_height;
  201. m_bIsDraging=FALSE;
  202. //确定鼠标在软件信息栏的区域   edit 2009/5/14
  203. CWnd* mWnd1=GetDlgItem(IDC_SOFTWAREINFO);
  204. mWnd1->GetClientRect(m_InfoRect);
  205. mWnd1->ClientToScreen(&m_InfoRect);
  206. //  CRect rect;
  207. //  GetClientRect(rect);
  208. //  ClientToScreen(&rect);
  209. int info_width=m_InfoRect.right-m_InfoRect.left;
  210. int info_height=m_InfoRect.bottom-m_InfoRect.top;
  211. m_InfoRect.top-=rect.top;
  212. m_InfoRect.left-=rect.left;
  213. m_InfoRect.right=m_InfoRect.left+info_width;
  214. m_InfoRect.bottom=m_InfoRect.top+info_height;
  215. Seperator();
  216. return TRUE;  // return TRUE  unless you set the focus to a control
  217. }
  218. void CFaceMFC_v1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
  219. {
  220. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  221. {
  222. CAboutDlg dlgAbout;
  223. dlgAbout.DoModal();
  224. }
  225. else
  226. {
  227. CDialog::OnSysCommand(nID, lParam);
  228. }
  229. }
  230. // If you add a minimize button to your dialog, you will need the code below
  231. //  to draw the icon.  For MFC applications using the document/view model,
  232. //  this is automatically done for you by the framework.
  233. void CFaceMFC_v1Dlg::OnPaint() 
  234. {
  235. if (IsIconic())
  236. {
  237. CPaintDC dc(this); // device context for painting
  238. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  239. // Center icon in client rectangle
  240. int cxIcon = GetSystemMetrics(SM_CXICON);
  241. int cyIcon = GetSystemMetrics(SM_CYICON);
  242. CRect rect;
  243. GetClientRect(&rect);
  244. int x = (rect.Width() - cxIcon + 1) / 2;
  245. int y = (rect.Height() - cyIcon + 1) / 2;
  246. // Draw the icon
  247. dc.DrawIcon(x, y, m_hIcon);
  248. }
  249. else
  250. {
  251. CDialog::OnPaint();
  252. }
  253. CWnd*   pWnd=GetDlgItem(IDC_IMAGESHOW);
  254. pWnd->UpdateWindow();
  255. if(mImg)
  256. {
  257. DrawPicToHDC(mImg,IDC_IMAGESHOW);
  258. }
  259. CWnd* pWnd_face;
  260. if(img_face[0])
  261. {
  262. for(int k=0;k<FaceNum;k++)
  263. {
  264. switch(k)
  265. {
  266. case 0:
  267. pWnd_face=GetDlgItem(IDC_FACESHOW1);
  268. pWnd_face->UpdateWindow();
  269. DrawPicToHDC(img_face[0],IDC_FACESHOW1);break;
  270. case 1:pWnd_face=GetDlgItem(IDC_FACESHOW2);
  271. pWnd_face->UpdateWindow();
  272. DrawPicToHDC(img_face[1],IDC_FACESHOW2);break;
  273. case 2:pWnd_face=GetDlgItem(IDC_FACESHOW3);
  274. pWnd_face->UpdateWindow();
  275. DrawPicToHDC(img_face[2],IDC_FACESHOW3);break;
  276. case 3:pWnd_face=GetDlgItem(IDC_FACESHOW4);
  277. pWnd_face->UpdateWindow();
  278. DrawPicToHDC(img_face[3],IDC_FACESHOW4);break;
  279. case 4:pWnd_face=GetDlgItem(IDC_FACESHOW5);
  280. pWnd_face->UpdateWindow();
  281. DrawPicToHDC(img_face[4],IDC_FACESHOW5);break;
  282. case 5:pWnd_face=GetDlgItem(IDC_FACESHOW6);
  283. pWnd_face->UpdateWindow();
  284. DrawPicToHDC(img_face[5],IDC_FACESHOW6);break;
  285. default:break;
  286. }
  287. }
  288. }
  289. if(ImgPause)
  290. {
  291. DrawPicToHDC(ImgPause,IDC_VIDEOSHOW);
  292. }
  293. if(ImgFaceResult[0])
  294. {
  295. for(int k=0;k<6;k++)
  296. {
  297. switch(k)
  298. {
  299. case 0:
  300. pWnd_face=GetDlgItem(IDC_FACERESULT1);
  301. pWnd_face->UpdateWindow();
  302. DrawPicToHDC(ImgFaceResult[0],IDC_FACERESULT1);break;
  303. case 1:pWnd_face=GetDlgItem(IDC_FACERESULT2);
  304. pWnd_face->UpdateWindow();
  305. DrawPicToHDC(ImgFaceResult[1],IDC_FACERESULT2);break;
  306. case 2:pWnd_face=GetDlgItem(IDC_FACERESULT3);
  307. pWnd_face->UpdateWindow();
  308. DrawPicToHDC(ImgFaceResult[2],IDC_FACERESULT3);break;
  309. case 3:pWnd_face=GetDlgItem(IDC_FACERESULT4);
  310. pWnd_face->UpdateWindow();
  311. DrawPicToHDC(ImgFaceResult[3],IDC_FACERESULT4);break;
  312. case 4:pWnd_face=GetDlgItem(IDC_FACERESULT5);
  313. pWnd_face->UpdateWindow();
  314. DrawPicToHDC(ImgFaceResult[4],IDC_FACERESULT5);break;
  315. case 5:pWnd_face=GetDlgItem(IDC_FACERESULT6);
  316. pWnd_face->UpdateWindow();
  317. DrawPicToHDC(ImgFaceResult[5],IDC_FACERESULT6);break;
  318. default:break;
  319. }
  320. }
  321. }
  322. //  if(ImgTemp)
  323. //  {
  324. //  DrawPicToHDC(ImgTemp,IDC_VIDEOSHOW);
  325. //  }
  326. //2009/4/25修改
  327. if(m_p1.x&&m_p2.x&&m_p1.y&&m_p2.y)
  328. {
  329. CDC* pDC=GetDC();
  330. CPen* pPen=new CPen;
  331. pPen->CreatePen(PS_SOLID,1,RGB(0,0,255));
  332. CGdiObject* pOldPen=pDC->SelectObject(pPen);
  333. CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
  334. CBrush* pOldBrush=pDC->SelectObject(pBrush);
  335. pDC->Rectangle(CRect(m_p1.x,m_p1.y,m_p2.x,m_p2.y));
  336. pDC->SelectObject(pOldBrush);
  337. pDC->SelectObject(pOldPen);
  338. delete pPen;
  339. ReleaseDC(pDC);
  340. }
  341. }
  342. // The system calls this to obtain the cursor to display while the user drags
  343. //  the minimized window.
  344. HCURSOR CFaceMFC_v1Dlg::OnQueryDragIcon()
  345. {
  346. return (HCURSOR) m_hIcon;
  347. }
  348. void CFaceMFC_v1Dlg::OnOpenimage() 
  349. {
  350. CFileDialog dlg(TRUE, _T("*.bmp"), "",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
  351. "image files (*.bmp; *.jpg;*.jpeg) |*.bmp;*.jpg;*.jpeg|All Files (*.*)|*.*||",NULL);
  352. char title[]= {"Open Image"};
  353. dlg.m_ofn.lpstrTitle= title;
  354. if (dlg.DoModal() == IDOK) 
  355. {
  356. CString path= dlg.GetPathName(); 
  357. mImg=cvLoadImage(path);
  358. ImgData=cvCloneImage(mImg);
  359. ImgReload=cvCloneImage(mImg);
  360. //IplImage* img=cvCreateImage(cvSize(mImg->width,mImg->height),IPL_DEPTH_8U,1);
  361. CreatMask(Mask,50);//创建掩模2009/4/23
  362. //cvCvtColor(mImg,img,CV_BGR2GRAY);
  363. if(mImg)
  364. {
  365. DrawPicToHDC(mImg,IDC_IMAGESHOW);
  366. ((CButton*)GetDlgItem(IDC_FACEDETECT))->EnableWindow(TRUE);
  367. ((CButton*)GetDlgItem(IDC_RELOAD))->EnableWindow(TRUE);
  368. ((CButton*)GetDlgItem(IDC_IMAGEINFO))->EnableWindow(TRUE);
  369. }
  370. }
  371. }
  372. void CFaceMFC_v1Dlg::DrawPicToHDC(IplImage *img, UINT ID)
  373. {
  374. CDC *pDC = GetDlgItem(ID)->GetDC();
  375. HDC hDC  = pDC->GetSafeHdc();
  376. CRect rect;
  377. GetDlgItem(ID)->GetClientRect(&rect);
  378. CvvImage cimg;
  379. cimg.CopyOf(img);
  380. cimg.DrawToHDC(hDC,&rect);
  381. ReleaseDC(pDC);
  382. }
  383. int CFaceMFC_v1Dlg::detect_and_draw(IplImage *img, CvHaarClassifierCascade *cascade)
  384. {
  385. storage = cvCreateMemStorage(0);
  386. int scale = 1;
  387.     CvPoint pt1, pt2;
  388.     //CvScalar s;
  389. int i,N=0;
  390. //cvPyrDown( img, temp, CV_GAUSSIAN_5x5 );
  391.     cvClearMemStorage(storage);
  392.     if(cascade)
  393.     {
  394.         CvSeq* faces = cvHaarDetectObjects(img, cascade, storage,
  395. 1.1, 3, CV_HAAR_DO_CANNY_PRUNING,cvSize(40, 40));
  396. N=faces?faces->total:0;
  397.         for(i=0;i<N;i++)
  398.         {
  399.             CvRect* r = (CvRect*)cvGetSeqElem(faces,i);
  400.             pt1.x = r->x*scale;
  401.             pt2.x = (r->x+r->width)*scale;
  402.             pt1.y = r->y*scale;
  403.             pt2.y = (r->y+r->height)*scale;
  404. cvSetImageROI(img,*r);
  405. img_face[i]=cvCloneImage(img);
  406. switch(i)
  407. {
  408. case 0:DrawPicToHDC(img_face[0],IDC_FACESHOW1);/*cvSaveImage("E:\1.jpg",img_face[0]);*/break;//2009/4/25修改
  409. case 1:DrawPicToHDC(img_face[1],IDC_FACESHOW2);/*cvSaveImage("E:\2.jpg",img_face[1]);*/break;
  410. case 2:DrawPicToHDC(img_face[2],IDC_FACESHOW3);/*cvSaveImage("E:\3.jpg",img_face[2]);*/break;
  411. case 3:DrawPicToHDC(img_face[3],IDC_FACESHOW4);/*cvSaveImage("E:\4.jpg",img_face[3]);*/break;
  412. case 4:DrawPicToHDC(img_face[4],IDC_FACESHOW5);/*cvSaveImage("E:\5.jpg",img_face[4]);*/break;
  413. case 5:DrawPicToHDC(img_face[5],IDC_FACESHOW6);/*cvSaveImage("E:\6.jpg",img_face[5]);*/break;
  414. default:break;
  415. }
  416. cvRectangle(mImg,pt1,pt2,CV_RGB(255,0,0),3,8,0);
  417. cvResetImageROI(img);//2009/4/25修改
  418.         }
  419.     }
  420. else
  421. {
  422. AfxMessageBox("没有分类器!");
  423. }
  424. cvResetImageROI(img);
  425. if(mImg)
  426. {
  427. DrawPicToHDC(mImg,IDC_IMAGESHOW);
  428. }
  429. return N;
  430. }
  431. void CFaceMFC_v1Dlg::OnFacedetect() 
  432. {
  433. cascade_name="f:\Program Files\OpenCV\data\haarcascades\haarcascade_frontalface_alt2.xml";
  434. cascade=(CvHaarClassifierCascade*)cvLoad(cascade_name,0,0,0);
  435.     if(!cascade)
  436. {
  437. AfxMessageBox("加载分类器失败!");
  438. }
  439. else
  440. {
  441. if(ImgData)
  442. {
  443. FaceNum=detect_and_draw(ImgData,cascade);
  444. }
  445. else
  446. AfxMessageBox("请打开要检测的图片!");
  447. }
  448. if(FaceNum==0)
  449. AfxMessageBox("没有检测到人脸!");
  450. else
  451. {
  452. FaceSel[0]=cvCreateImage(cvGetSize(img_face[0]),img_face[0]->depth,1);
  453. cvCvtColor(img_face[0],FaceSel[0],CV_BGR2GRAY);
  454. FaceToSel=cvCloneImage(FaceSel[0]);
  455. }
  456.  InvalidateRect(CRect(10,330,370,585),TRUE);//将上一次检测的结果擦除,重新显示指定区域的背景,这样不会出现上一次检测的结果还在窗口上
  457. }
  458. void CFaceMFC_v1Dlg::OnRadioface1() 
  459. {
  460. m_Facesel=0;
  461. if(m_Facesel<FaceNum)
  462. {
  463. //IplImage* FaceSel=cvCreateImage(cvGetSize(img_face[0]),img_face[0]->depth,1);
  464. cvCvtColor(img_face[0],FaceSel[0],CV_BGR2GRAY);
  465. FaceToSel=FaceSel[0];
  466. }
  467. }
  468. void CFaceMFC_v1Dlg::OnRadioface2() 
  469. {
  470. m_Facesel=1;
  471. if(m_Facesel<FaceNum)
  472. {
  473. FaceSel[1]=cvCreateImage(cvGetSize(img_face[1]),img_face[0]->depth,1);
  474. cvCvtColor(img_face[1],FaceSel[1],CV_BGR2GRAY);
  475. FaceToSel=FaceSel[1];
  476. }
  477. else
  478. {
  479. AfxMessageBox("选择无效!");
  480. ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
  481. ((CButton*)GetDlgItem(IDC_RADIOFACE2))->SetCheck(FALSE);
  482. }
  483. }
  484. void CFaceMFC_v1Dlg::OnRadioface3() 
  485. {
  486. m_Facesel=2;
  487. if(m_Facesel<FaceNum)
  488. {
  489. FaceSel[2]=cvCreateImage(cvGetSize(img_face[2]),img_face[0]->depth,1);
  490. cvCvtColor(img_face[2],FaceSel[2],CV_BGR2GRAY);
  491. FaceToSel=FaceSel[2];
  492. }
  493. else
  494. {
  495. AfxMessageBox("选择无效!");
  496. ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
  497. ((CButton*)GetDlgItem(IDC_RADIOFACE3))->SetCheck(FALSE);
  498. }
  499. }
  500. void CFaceMFC_v1Dlg::OnRadioface4() 
  501. {
  502. m_Facesel=3;
  503. if(m_Facesel<FaceNum)
  504. {
  505. FaceSel[3]=cvCreateImage(cvGetSize(img_face[3]),img_face[0]->depth,1);
  506. cvCvtColor(img_face[3],FaceSel[3],CV_BGR2GRAY);
  507. FaceToSel=FaceSel[3];
  508. }
  509. else
  510. {
  511. AfxMessageBox("选择无效!");
  512. ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
  513. ((CButton*)GetDlgItem(IDC_RADIOFACE4))->SetCheck(FALSE);
  514. }
  515. }
  516. void CFaceMFC_v1Dlg::OnRadioface5() 
  517. {
  518. m_Facesel=4;
  519. if(m_Facesel<FaceNum)
  520. {
  521. FaceSel[4]=cvCreateImage(cvGetSize(img_face[4]),img_face[0]->depth,1);
  522. cvCvtColor(img_face[4],FaceSel[4],CV_BGR2GRAY);
  523. FaceToSel=FaceSel[4];
  524. }
  525. else
  526. {
  527. AfxMessageBox("选择无效!");
  528. ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
  529. ((CButton*)GetDlgItem(IDC_RADIOFACE5))->SetCheck(FALSE);
  530. }
  531. }
  532. void CFaceMFC_v1Dlg::OnRadioface6() 
  533. {
  534. m_Facesel=5;
  535. if(m_Facesel<FaceNum)
  536. {
  537. FaceSel[5]=cvCreateImage(cvGetSize(img_face[5]),img_face[0]->depth,1);
  538. cvCvtColor(img_face[5],FaceSel[5],CV_BGR2GRAY);
  539. FaceToSel=FaceSel[5];
  540. }
  541. else
  542. {
  543. AfxMessageBox("选择无效!");
  544. ((CButton*)GetDlgItem(IDC_RADIOFACE1))->SetCheck(TRUE);
  545. ((CButton*)GetDlgItem(IDC_RADIOFACE6))->SetCheck(FALSE);
  546. }
  547. }
  548. void CFaceMFC_v1Dlg::OnOpenvideo() 
  549. {
  550. IplImage* frame_1st=0;
  551. CFileDialog dlg(TRUE, _T("*.avi"), "",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,"avi视频文件 (*.avi) |*.avi|All Files (*.*)|*.*||",NULL);
  552. char title[]= {"打开视频"};
  553. dlg.m_ofn.lpstrTitle= title;
  554. int m_hour,m_minute,m_second;
  555. char buf_time[200];
  556. if (dlg.DoModal()==IDOK) 
  557. {
  558. if(capture)
  559. {
  560. cvReleaseCapture(&capture);
  561. count=1;
  562. m_slider.SetPos(count);
  563. }
  564. facedistance.clear(); 
  565. frameselect.clear();  
  566. frontposition.clear();
  567. frontdistances.clear();
  568. Framefacepos.clear();
  569. path=dlg.GetPathName();
  570. capture=cvCaptureFromAVI(path);
  571. GetDlgItem(IDC_PLAY_PAUSE)->SetWindowText("播放");
  572. cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,1);
  573. frame_1st=cvQueryFrame(capture);
  574. DrawPicToHDC(frame_1st,IDC_VIDEOSHOW);
  575. numFrames=(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
  576. framerate=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
  577. startframe=0;
  578. overframe=numFrames;
  579. second[0]=int(overframe/framerate)%60;
  580. minute[0]=int(overframe/framerate)/60%60;
  581. hour[0]=int(overframe/framerate)/60/60;
  582. sprintf(buf_time,"%d:%d:%d",hour[0],minute[0],second[0]);
  583. m_timedown.SetWindowText(buf_time);
  584. second[1]=int(startframe/framerate)%60;
  585. minute[1]=int(startframe/framerate)/60%60;
  586. hour[1]=int(startframe/framerate)/60/60;
  587. sprintf(buf_time,"%d:%d:%d",hour[1],minute[1],second[1]);
  588. m_timeup.SetWindowText(buf_time);
  589. m_second=int(numFrames/framerate)%60;
  590. m_minute=int(numFrames/framerate)/60%60;
  591. m_hour=int(numFrames/framerate)/60/60;
  592. sprintf(buf_time,"%d:%d:%d",m_hour,m_minute,m_second);
  593. m_totaltime.SetWindowText(buf_time);
  594. ((CButton*)GetDlgItem(IDC_PLAY_PAUSE))->EnableWindow(TRUE);
  595. ((CButton*)GetDlgItem(IDC_FACERETRIEVAL))->EnableWindow(TRUE);
  596. ((CButton*)GetDlgItem(IDC_TIMESTART))->EnableWindow(TRUE);
  597. ((CButton*)GetDlgItem(IDC_TIMEOVER))->EnableWindow(TRUE);
  598. ((CButton*)GetDlgItem(IDC_VIDEOINFO))->EnableWindow(TRUE);
  599. if(!capture)
  600. {
  601. AfxMessageBox("加载视频失败!!!!");
  602. }
  603. }
  604. //hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,NULL,0,&ThreadID);
  605. m_slider.SetRange(1,numFrames);
  606. m_slider.SetTicFreq(1);
  607. }
  608. void CFaceMFC_v1Dlg::OnPlayPause() 
  609. {
  610. if(!pausestatus)
  611. {
  612. GetDlgItem(IDC_PLAY_PAUSE)->SetWindowText("暂停");
  613. pausestatus=TRUE;
  614. }
  615. else
  616. {
  617. GetDlgItem(IDC_PLAY_PAUSE)->SetWindowText("播放");
  618. pausestatus=FALSE;
  619. }
  620. hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,NULL,0,&ThreadID);
  621. }
  622. //子线程入口
  623. void ThreadFunc()
  624. {
  625. char buf[200];
  626. IplImage* mImg;
  627. int hour,minute,second;
  628. double framerate=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
  629. while(pausestatus)
  630. {
  631. cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,count);
  632. if(capture)
  633. {
  634. mImg=cvQueryFrame(capture);
  635. CDC *pDC = AfxGetApp()->m_pMainWnd->GetDlgItem(IDC_VIDEOSHOW)->GetDC();
  636. HDC hDC=  pDC->GetSafeHdc();
  637. CRect rect;
  638. AfxGetApp()->m_pMainWnd->GetDlgItem(IDC_VIDEOSHOW)->GetClientRect(&rect);
  639. CvvImage cimg;
  640. cimg.CopyOf(mImg);
  641. cimg.DrawToHDC(hDC,&rect);
  642. }
  643. second=int(count/framerate)%60;
  644. minute=int(count/framerate)/60%60;
  645. hour=int(count/framerate)/60/60;
  646. sprintf(buf,"%d:%d:%d",hour,minute,second);
  647. ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_PLAYTIME,buf);
  648. m_slider.SetPos(count);
  649. count++;
  650.         Sleep(20);
  651. }
  652. ImgPause=mImg;
  653. cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,count);
  654. }
  655. void CFaceMFC_v1Dlg::OnCustomdrawSlider(NMHDR* pNMHDR, LRESULT* pResult) 
  656. {
  657. count=m_slider.GetPos();
  658. UpdateData(FALSE); 
  659. *pResult = 0;
  660. }
  661. void CFaceMFC_v1Dlg::OnTimestart() 
  662. {
  663. char buf_time[200];
  664. startframe=m_slider.GetPos();
  665. second[1]=int(startframe/framerate)%60;
  666. minute[1]=int(startframe/framerate)/60%60;
  667. hour[1]=int(startframe/framerate)/60/60;
  668. sprintf(buf_time,"%d:%d:%d",hour[1],minute[1],second[1]);
  669. m_timeup.SetWindowText(buf_time);
  670. }
  671. void CFaceMFC_v1Dlg::OnTimeover() 
  672. {
  673. char buf_time[200];
  674. overframe=m_slider.GetPos();
  675. if(overframe<=startframe)
  676. {
  677. AfxMessageBox("选择不合法!");
  678. }
  679. else
  680. {
  681. second[0]=int(overframe/framerate)%60;
  682. minute[0]=int(overframe/framerate)/60%60;
  683. hour[0]=int(overframe/framerate)/60/60;
  684. sprintf(buf_time,"%d:%d:%d",hour[0],minute[0],second[0]);
  685. m_timedown.SetWindowText(buf_time);
  686. }
  687. }
  688. void CFaceMFC_v1Dlg::OnKillfocusEditStep() 
  689. {
  690. UpdateData(TRUE);
  691. }
  692. void CFaceMFC_v1Dlg::OnFaceretrieval() 
  693. {
  694. double time1=0,time2=0;
  695. IplImage* FaceInput;
  696. FaceInput=cvCreateImage(cvSize(100,100),FaceToSel->depth,FaceToSel->nChannels);
  697. cvResize(FaceToSel,FaceInput,CV_INTER_LINEAR);
  698.      
  699. time1=(double)cvGetTickCount();
  700. facedistance.clear(); 
  701. frameselect.clear();  
  702. frontposition.clear();
  703. frontdistances.clear();
  704. Framefacepos.clear();
  705. int possiblefaces = 12;
  706. //int framestep =25; 
  707. //IplImage *pImg;
  708. IplImage *pFrame;
  709. IplImage *framefaces[50];
  710. CvMat *framemat[50];
  711. int width=int(cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH));
  712. int height=int(cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT));
  713. Do2DPCA(FaceInput);
  714. m_slider.SetRange(1,overframe-startframe,TRUE);
  715. //m_slider.SetTicFreq(m_framestep);
  716. m_slider.SetPos(1);
  717. int slider=1;
  718. for(int i=startframe;i<overframe;i+=m_framestep)
  719. {
  720. IplImage *grayframe=cvCreateImage(cvSize(width,height),8,1);
  721. CvMat *frametemp1=cvCreateMat(100,100,CV_8UC1);
  722. CvMat *y_y=cvCreateMat(100,1,CV_64FC1);
  723. double distance;
  724. vector<CvRect *> faceposition;
  725. cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,i);
  726. pFrame=cvQueryFrame(capture);
  727. cvCvtColor(pFrame,grayframe,CV_BGR2GRAY);
  728. cvFlip(grayframe,grayframe,0);
  729. int n = FaceRecgnition(grayframe,cascade,faceposition);
  730. for (int j = 0;j < n; j++)
  731. {
  732. CvRect *r = faceposition[j];
  733. Framefacepos.push_back(r);
  734. cvSetImageROI(grayframe,*r);
  735. framefaces[j] = cvCreateImage(cvSize(100,100),grayframe->depth,grayframe->nChannels);
  736. cvResize(grayframe, framefaces[j], CV_INTER_LINEAR);
  737. cvResetImageROI(grayframe);//2009/4/25修改
  738. }
  739. for ( int k = 0;k < n; k++)
  740. {   
  741. framemat[k]  =  cvCreateMat(100,100,CV_64FC1);
  742. cvGetMat(framefaces[k],frametemp1);
  743. //cvMul(frametemp1,Mask,frametemp1);//2009/4/23添加
  744. cvConvertScale(frametemp1,framemat[k]);
  745. //cvSub(framemat[k],Meanface,framemat[k]);
  746. cvmMul(framemat[k],EigenFace,y_y);
  747. distance = cvNorm(ProjectY,y_y,CV_L2);
  748. facedistance.push_back(distance);
  749. frameselect.push_back(i);
  750. }
  751. slider++;
  752. m_slider.SetPos(slider*m_framestep);
  753. }
  754. Findneighbors(facedistance,possiblefaces,frontposition);
  755.   char buffer[10];
  756.   sprintf(buffer,"距离是:%f",frontdistances[1]);
  757.   if(frontdistances[1]<3000)
  758.    AfxMessageBox(buffer);
  759. for (int m=0; m<possiblefaces;m++)
  760. {
  761. int selectframe = frameselect[frontposition[m]];
  762. int scale = 1;
  763. CvRect *r = Framefacepos[frontposition[m]];
  764. cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,selectframe);
  765. pFrame = cvQueryFrame(capture);
  766. cvFlip(pFrame,pFrame,0);
  767. cvSetImageROI(pFrame,*r);
  768. cvFlip(pFrame,pFrame,0);
  769. ImgFaceResult[m]=cvCloneImage(pFrame);
  770. switch(m)
  771. {
  772. case 0:DrawPicToHDC(ImgFaceResult[0],IDC_FACERESULT1);break;
  773. case 1:DrawPicToHDC(ImgFaceResult[1],IDC_FACERESULT2);break;
  774. case 2:DrawPicToHDC(ImgFaceResult[2],IDC_FACERESULT3);break;
  775. case 3:DrawPicToHDC(ImgFaceResult[3],IDC_FACERESULT4);break;
  776. case 4:DrawPicToHDC(ImgFaceResult[4],IDC_FACERESULT5);break;
  777. case 5:DrawPicToHDC(ImgFaceResult[5],IDC_FACERESULT6);break;
  778. }
  779. cvResetImageROI(pFrame);
  780. }
  781. ((CButton*)GetDlgItem(IDC_DOWN))->EnableWindow(TRUE);
  782. ((CButton*)GetDlgItem(IDC_FOLLOW))->EnableWindow(TRUE);
  783. count=0;
  784. /*int hour,minute,second;
  785. char buf_frame[10];
  786. aimframe=frameselect[frontposition[0]];
  787. second=int(aimframe/framerate)%60;
  788. minute=int(aimframe/framerate)/60%60;
  789. hour=int(aimframe/framerate)/60/60;
  790. sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
  791. ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame); */
  792. ((CButton*)GetDlgItem(IDC_UP))->EnableWindow(FALSE);
  793. time2=(double)cvGetTickCount();
  794. time1=time2-time1;
  795. time1=time1/((double)cvGetTickFrequency()*1000.);
  796. char buf_time[20];
  797. sprintf(buf_time,"检索时间为:%fms",time1);
  798. AfxMessageBox(buf_time);
  799. }
  800. void CFaceMFC_v1Dlg::Do2DPCA(IplImage *face)
  801. {
  802. CvMat* Matface    =  cvCreateMat(100,100,CV_64FC1);
  803. CvMat* Svdface    =  cvCreateMat(100,100,CV_64FC1);
  804. CvMat* EigenVec   =  cvCreateMat(100,100,CV_64FC1);
  805. CvMat* EigenValue =  cvCreateMat(100,1,CV_64FC1);
  806. CvMat *temp1  =  cvCreateMat(100,100,CV_8UC1);
  807. CvMat *temp2  =  cvCreateMat(100,100,CV_64FC1);
  808. CvMat *temp2_T = cvCreateMat(100,100,CV_64FC1);
  809. CvMat *temp3 =   cvCreateMat(100,100,CV_64FC1);
  810. CvMat *temp4 =   cvCreateMat(100,100,CV_64FC1);
  811. CvMat *temp4_T = cvCreateMat(100,100,CV_64FC1);
  812. CvMat *temp5 =   cvCreateMat(100,100,CV_64FC1);
  813. CvMat *G =       cvCreateMat(100,100,CV_64FC1);
  814. cvGetMat(face,temp1);
  815. //cvMul(temp1,Mask,temp1);//2009/4/23添加
  816. cvConvertScale(temp1,Matface);
  817. double scale = 1.2;      
  818. MatSVD(Matface,Svdface,scale);
  819. cvAdd(Matface,Svdface,Meanface);
  820. cvConvertScale(Meanface,Meanface,0.5,0);
  821. cvSub(Matface,Meanface,temp2);
  822. cvTranspose(temp2,temp2_T);
  823. cvmMul(temp2_T,temp2,temp3);
  824. cvSub(Svdface,Meanface,temp4);
  825. cvTranspose(temp4,temp4_T);
  826. cvmMul(temp4_T,temp4,temp5);
  827. cvAdd(temp3,temp5,G);
  828. cvConvertScale(G,G,0.5,0);
  829. cvEigenVV(G,EigenVec,EigenValue,DBL_EPSILON);
  830. for (int j = 0;j < 100; j++)
  831. {
  832. cvSetReal2D(EigenFace,j,0,cvGetReal2D(EigenVec,0,j));
  833. }
  834. cvmMul(Matface,EigenFace,ProjectY);
  835. return;
  836. }
  837. void CFaceMFC_v1Dlg::MatSVD(CvMat *src, CvMat *dst, double &scale)
  838. {
  839. CvMat *W=cvCreateMat(src->rows,src->cols,CV_64FC1);
  840. CvMat *V=cvCreateMat(src->cols,src->cols,CV_64FC1);
  841. CvMat *U=cvCreateMat(src->rows,src->rows,CV_64FC1);
  842. CvMat *V_T=cvCreateMat(src->cols,src->cols,CV_64FC1);
  843. cvSVD(src,W,U,V);
  844. for(int i = 0;i < src->rows;i++)
  845. {
  846. double temp = pow (cvGetReal2D(W,i,i),scale);
  847. cvSetReal2D(W,i,i,temp);
  848. }
  849. cvmMul(U,W,dst);
  850. cvTranspose(V,V_T);
  851. cvmMul(dst,V_T,dst);
  852. }
  853. int CFaceMFC_v1Dlg::FaceRecgnition(IplImage* img,CvHaarClassifierCascade* cascade,vector<CvRect *> &faceposition)
  854. {
  855. storage = cvCreateMemStorage(0);
  856. //int scale = 1;
  857. //IplImage* temp=cvCreateImage( cvSize(img->width,img->height), 8, 1 );
  858. int N = 0;
  859. cvClearMemStorage( storage );
  860. if( cascade )
  861. {
  862. CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(20, 20));
  863. N=faces ? faces->total:0;
  864. for(int i = 0; i < (faces ? faces->total : 0); i++ )
  865. {
  866. CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
  867. faceposition.push_back( r);
  868. }
  869. }
  870. else
  871. {
  872. AfxMessageBox("无分类器!");
  873. }
  874. return N;
  875. }
  876. void CFaceMFC_v1Dlg::Findneighbors(vector<double> v,int n,vector<int> &frontposition)
  877. {
  878. if ( n > v.size())
  879. {
  880. return;
  881. }
  882. for (int i = 0;i < n ;i++)
  883. {
  884. double smallest;
  885. int pos = FindSmallest(v,smallest);
  886. frontposition.push_back(pos);
  887. frontdistances.push_back(smallest);
  888. v[pos] = maxnum;
  889. }
  890. return;
  891. }
  892. int CFaceMFC_v1Dlg::FindSmallest(vector<double> v,double &smallest)
  893. {
  894. int n = 0;
  895. smallest = v[0];
  896. for (int i = 1;i < v.size();i++)
  897. {
  898. if (v[i] < smallest)
  899. {
  900. smallest = v[i];
  901. n = i ;
  902. }
  903. }
  904. return n;
  905. }
  906. void CFaceMFC_v1Dlg::OnDown() 
  907. {
  908.  ((CButton*)GetDlgItem(IDC_DOWN))->EnableWindow(FALSE);
  909.  ((CButton*)GetDlgItem(IDC_UP))->EnableWindow(TRUE);
  910.  
  911.  for(int i=0;i<6;i++)
  912.  {
  913. ImgFaceTemp[i]=ImgFaceResult[i];
  914. ImgFaceResult[i]=ImgFaceResult[i+6];
  915.  }
  916.  for(int j=0;j<6;j++)
  917.  {
  918.  frame_time[j]=frameselect[frontposition[j]];
  919.  frameselect[frontposition[j]]=frameselect[frontposition[j+6]];
  920.  }
  921.  OnPaint();
  922. }
  923. void CFaceMFC_v1Dlg::OnUp() 
  924. {
  925.  ((CButton*)GetDlgItem(IDC_UP))->EnableWindow(FALSE);
  926.  ((CButton*)GetDlgItem(IDC_DOWN))->EnableWindow(TRUE);
  927.  for(int i=0;i<6;i++)
  928.  {
  929.  ImgFaceResult[i+6]=ImgFaceResult[i];
  930.  ImgFaceResult[i]=ImgFaceTemp[i];
  931.  }
  932.  for(int j=0;j<6;j++)
  933.  {
  934.  frameselect[frontposition[j+6]]=frameselect[frontposition[j]];
  935.  frameselect[frontposition[j]]=frame_time[j];
  936.  }
  937.  OnPaint();
  938. }
  939. void CFaceMFC_v1Dlg::OnFollow() 
  940. {
  941. cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,aimframe);
  942. //ImgTemp=cvQueryFrame(capture);
  943. ImgPause=cvQueryFrame(capture);//2009/4/25修改
  944. //DrawPicToHDC(ImgTemp,IDC_VIDEOSHOW);
  945. DrawPicToHDC(ImgPause,IDC_VIDEOSHOW);//2009/4/25修改
  946. count=aimframe;
  947. m_slider.SetPos(count);
  948. }
  949. void CFaceMFC_v1Dlg::OnRadioresult1() 
  950. {
  951. int hour,minute,second;
  952. char buf_frame[10];
  953. aimframe=frameselect[frontposition[0]];
  954. second=int(aimframe/framerate)%60;
  955. minute=int(aimframe/framerate)/60%60;
  956. hour=int(aimframe/framerate)/60/60;
  957. sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
  958. ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame); 
  959.  /*sprintf(buf_frame,"%d",aimframe);
  960.  AfxMessageBox(buf_frame);*/
  961. }
  962. void CFaceMFC_v1Dlg::OnRadioresult2() 
  963. {
  964. int hour,minute,second;
  965. char buf_frame[10];
  966. aimframe=frameselect[frontposition[1]];
  967. second=int(aimframe/framerate)%60;
  968. minute=int(aimframe/framerate)/60%60;
  969. hour=int(aimframe/framerate)/60/60;
  970. sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
  971. ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame); 
  972.  /*char buf_frame[10];
  973.  sprintf(buf_frame,"%d",aimframe);
  974.  AfxMessageBox(buf_frame);*/
  975. }
  976. void CFaceMFC_v1Dlg::OnRadioresult3() 
  977. {
  978. int hour,minute,second;
  979. char buf_frame[10];
  980. aimframe=frameselect[frontposition[2]];
  981. second=int(aimframe/framerate)%60;
  982. minute=int(aimframe/framerate)/60%60;
  983. hour=int(aimframe/framerate)/60/60;
  984. sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
  985. ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame); 
  986.  /*char buf_frame[10];
  987.  sprintf(buf_frame,"%d",aimframe);
  988.  AfxMessageBox(buf_frame);*/
  989. }
  990. void CFaceMFC_v1Dlg::OnRadioresult4() 
  991. {
  992. int hour,minute,second;
  993. char buf_frame[10];
  994. aimframe=frameselect[frontposition[3]];
  995. second=int(aimframe/framerate)%60;
  996. minute=int(aimframe/framerate)/60%60;
  997. hour=int(aimframe/framerate)/60/60;
  998. sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
  999. ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame); 
  1000. /*char buf_frame[10];
  1001. sprintf(buf_frame,"%d",aimframe);
  1002.  AfxMessageBox(buf_frame);*/
  1003. }
  1004. void CFaceMFC_v1Dlg::OnRadioresult5() 
  1005. {
  1006. int hour,minute,second;
  1007. char buf_frame[10];
  1008. aimframe=frameselect[frontposition[4]];
  1009. second=int(aimframe/framerate)%60;
  1010. minute=int(aimframe/framerate)/60%60;
  1011. hour=int(aimframe/framerate)/60/60;
  1012. sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
  1013. ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame); 
  1014. /*char buf_frame[10];
  1015. sprintf(buf_frame,"%d",aimframe);
  1016.  AfxMessageBox(buf_frame);*/
  1017. }
  1018. void CFaceMFC_v1Dlg::OnRadioresult6() 
  1019. {
  1020. int hour,minute,second;
  1021. char buf_frame[10];
  1022. aimframe=frameselect[frontposition[5]];
  1023. second=int(aimframe/framerate)%60;
  1024. minute=int(aimframe/framerate)/60%60;
  1025. hour=int(aimframe/framerate)/60/60;
  1026. sprintf(buf_frame,"%d:%d:%d",hour,minute,second);
  1027. ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT_LOCATIME,buf_frame); 
  1028.  /*char buf_frame[10];
  1029.  sprintf(buf_frame,"%d",aimframe);
  1030.  AfxMessageBox(buf_frame);*/
  1031. }
  1032. //鼠标左击开始绘制矩形
  1033. void CFaceMFC_v1Dlg::OnLButtonDown(UINT nFlags, CPoint point) 
  1034. {
  1035. if(m_MouseRect.PtInRect(point))
  1036. {
  1037. temp_p1=m_p1;
  1038. temp_p2=m_p2;
  1039. m_p1=point;
  1040. m_bIsDraging=TRUE;
  1041. //m_bDrawed=FALSE;
  1042. ::SetCursor(::LoadCursor(NULL,IDC_CROSS));
  1043. SetCapture();
  1044. switch(m_iIsDraging)
  1045. {
  1046. case 1:
  1047. ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));break;
  1048. case 2:
  1049. ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));break;
  1050. }
  1051. 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))
  1052. {
  1053. ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));
  1054. m_iIsDraging=1;
  1055. m_bIsDraging=FALSE;
  1056. }
  1057. else
  1058. m_bIsDraging=TRUE;
  1059. 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))
  1060. {
  1061. ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
  1062. m_iIsDraging=2;
  1063. m_bIsDraging=FALSE;
  1064. temp_p3=point;
  1065. }
  1066. }
  1067. CDialog::OnLButtonDown(nFlags, point);
  1068. }
  1069. //鼠标左键弹起来表示绘制图形结束
  1070. void CFaceMFC_v1Dlg::OnLButtonUp(UINT nFlags, CPoint point) 
  1071. {
  1072. if(m_bIsDraging)
  1073. {
  1074. m_p2=point;
  1075. }
  1076. ::ReleaseCapture();
  1077. m_bIsDraging=FALSE;
  1078. m_iIsDraging=0;
  1079. ::SetCursor(::LoadCursor(NULL,IDC_CROSS));
  1080. CDC* pDC=GetDC();
  1081. CPen* pPen=new CPen;
  1082. pPen->CreatePen(PS_SOLID,1,RGB(0,255,0));
  1083. CGdiObject* pOldPen=pDC->SelectObject(pPen);
  1084. pDC->Rectangle(m_p1.x-4,m_p1.y-4,m_p1.x+4,m_p1.y+4);
  1085. //pDC->Rectangle(m_p2.x-4,m_p1.y-4,m_p2.x+4,m_p1.y+4);
  1086. //pDC->Rectangle(m_p2.x-4,m_p2.y-4,m_p2.x+4,m_p2.y+4);
  1087. //pDC->Rectangle(m_p1.x-4,m_p2.y-4,m_p1.x+4,m_p2.y+4);
  1088. CDialog::OnLButtonUp(nFlags, point);
  1089. }
  1090. //通过鼠标移动来选择矩形区域
  1091. void CFaceMFC_v1Dlg::OnMouseMove(UINT nFlags, CPoint point) 
  1092. {
  1093. if(m_MouseRect.PtInRect(point))
  1094. {
  1095. ::SetCursor(::LoadCursor(NULL,IDC_CROSS));
  1096. if(m_iIsDraging==1)
  1097. ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));
  1098. if(m_iIsDraging==2)
  1099. ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
  1100. if(m_bIsDraging)
  1101. {
  1102. CDC* pDC=GetDC();
  1103. CPen* pPen=new CPen;
  1104. pPen->CreatePen(PS_SOLID,1,RGB(0,0,255));
  1105. CGdiObject* pOldPen=pDC->SelectObject(pPen);
  1106. m_p2=point;
  1107. CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
  1108. CBrush* pOldBrush=pDC->SelectObject(pBrush);
  1109. pDC->Rectangle(CRect(m_p1.x,m_p1.y,m_p2.x,m_p2.y));
  1110. pDC->SelectObject(pOldBrush);
  1111. pDC->SelectObject(pOldPen);
  1112. Invalidate(FALSE);
  1113. delete pPen;
  1114. ReleaseDC(pDC);
  1115. }
  1116. 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))
  1117. {
  1118. ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));
  1119. }
  1120. 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))
  1121. {
  1122. ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
  1123. }
  1124. if(m_iIsDraging==1)
  1125. {
  1126. Invalidate(FALSE);
  1127. ::SetCursor(::LoadCursor(NULL,IDC_SIZENWSE));
  1128. CDC* pDC=GetDC();
  1129. CPen* pPen=new CPen;
  1130. pPen->CreatePen(PS_SOLID,1,RGB(0,0,255));
  1131. CGdiObject* pOldPen=pDC->SelectObject(pPen);
  1132. m_p1=point;
  1133. CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
  1134. CBrush* pOldBrush=pDC->SelectObject(pBrush);
  1135. pDC->Rectangle(CRect(m_p1.x,m_p1.y,m_p2.x,m_p2.y));
  1136. pDC->SelectObject(pOldBrush);
  1137. pDC->SelectObject(pOldPen);
  1138. delete pPen;
  1139. ReleaseDC(pDC);
  1140. }
  1141. if(m_iIsDraging==2)
  1142. {
  1143. Invalidate(FALSE);
  1144. ::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
  1145. CDC* pDC=GetDC();
  1146. CPen* pPen=new CPen;
  1147. pPen->CreatePen(PS_SOLID,1,RGB(0,0,255));
  1148. CGdiObject* pOldPen=pDC->SelectObject(pPen);
  1149. int x1=point.x-temp_p3.x;
  1150. int y1=point.y-temp_p3.y;
  1151. m_p1=temp_p1;
  1152. m_p2=temp_p2;
  1153. m_p1.x=m_p1.x+x1;
  1154. m_p1.y=m_p1.y+y1;
  1155. m_p2.x=m_p2.x+x1;
  1156. m_p2.y=m_p2.y+y1;
  1157. temp_p1=m_p1;
  1158. temp_p2=m_p2;
  1159. temp_p3=point;
  1160. CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
  1161. CBrush* pOldBrush=pDC->SelectObject(pBrush);
  1162. pDC->Rectangle(CRect(m_p1.x,m_p1.y,m_p2.x,m_p2.y));
  1163.   pDC->SelectObject(pOldBrush);
  1164.   pDC->SelectObject(pOldPen);
  1165. delete pPen;
  1166. ReleaseDC(pDC);
  1167. }
  1168.  }
  1169.  else
  1170.  {
  1171.  if(m_bIsDraging)
  1172.  {
  1173.  
  1174.  ::SetCursor(::LoadCursor(NULL,IDC_NO));
  1175.  }
  1176.  
  1177.  }
  1178.  if(m_InfoRect.PtInRect(point))
  1179.  {
  1180.  ::SetCursor(::LoadCursor(NULL,IDC_CROSS));
  1181.  Seperator();
  1182.  }
  1183.  else
  1184.  merge();
  1185. CDialog::OnMouseMove(nFlags, point);
  1186. }
  1187. //右击选择被鼠标框中的区域
  1188. void CFaceMFC_v1Dlg::OnRButtonDown(UINT nFlags, CPoint point) 
  1189. {
  1190. if((point.x>m_p1.x)&&(point.x<m_p2.x)&&(point.y>m_p1.y)&&(point.y<m_p2.y))
  1191. {
  1192. imgwidth=m_p1.x-m_MouseRect.left;
  1193. imgheight=m_p1.y-m_MouseRect.top;
  1194. rectwidth=m_p2.x-m_p1.x;
  1195. rectheight=m_p2.y-m_p1.y;
  1196. imgwidth=int(double(imgwidth)/m_width*(mImg->width));
  1197. imgheight=int(double(imgheight)/m_height*(mImg->height));
  1198. rectwidth=int(double(rectwidth)/m_width*(mImg->width));
  1199. rectheight=int(double(rectheight)/m_height*(mImg->height));
  1200. CvRect r=cvRect(imgwidth,imgheight,rectwidth,rectheight);
  1201. cvSetImageROI(ImgData,r);
  1202. IplImage* temp=cvCreateImage(cvSize(r.width,r.height),ImgData->depth,ImgData->nChannels);
  1203. cvCopyImage(ImgData,temp);
  1204. mImg=cvCloneImage(temp);
  1205. //cvSaveImage("E:\ss.jpg",mImg);//2009/4/25修改
  1206. DrawPicToHDC(mImg,IDC_IMAGESHOW);
  1207. ImgData=cvCloneImage(mImg);
  1208. //width=mImg->width;
  1209. //height=mImg->height;
  1210. cvResetImageROI(mImg);
  1211. }
  1212. m_p1=NULL;
  1213. m_p2=NULL;
  1214. CDialog::OnRButtonDown(nFlags, point);
  1215. }
  1216. //重新加载图像
  1217. void CFaceMFC_v1Dlg::OnReload() 
  1218. {
  1219. mImg=cvCloneImage(ImgReload);
  1220. ImgData=cvCloneImage(ImgReload);
  1221. DrawPicToHDC(mImg,IDC_IMAGESHOW);
  1222. }
  1223. //显示图像的相关信息
  1224. void CFaceMFC_v1Dlg::OnImageinfo() 
  1225. {
  1226. int width_info=ImgData->width;
  1227. int height_info=ImgData->height;
  1228. int channel_info=ImgData->nChannels;
  1229. int depth_info=ImgData->depth;
  1230. char buf_info[100];
  1231. sprintf(buf_info,"图像的宽度: %dn图像的高度: %dn通道数: %dn位深: %d",width_info,height_info,channel_info,depth_info);
  1232. AfxMessageBox(buf_info);
  1233. }
  1234. //显示视频的相关信息
  1235. void CFaceMFC_v1Dlg::OnVideoinfo() 
  1236. {
  1237. int video_width=cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
  1238. int video_height=cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT);
  1239. int frame_rate=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
  1240. char video_info[100];
  1241. sprintf(video_info,"视频的宽度: %dn视频的高度: %dn帧速率: %d",video_width,video_height,frame_rate);
  1242. AfxMessageBox(video_info);
  1243. }
  1244. void CFaceMFC_v1Dlg::CreatMask(CvMat* Mask,double r)
  1245. {
  1246. //CvMat *Mask = cvCreateMat(facesize.height,facesize.width,CV_8UC1);
  1247. cvSet(Mask,cvScalarAll(1),0);
  1248. for (float i = 50; i <49 ;i ++)
  1249. {
  1250. for (float j = 0; j < 49;j++)
  1251. {
  1252. if ( pow(double((i - 50 + 1)),2) + pow(double((j- 502 +1)),2) > pow(r,2)) 
  1253. {
  1254. cvSetReal2D(Mask,i,j,0);
  1255. }
  1256. }
  1257. }
  1258. }
  1259. void CFaceMFC_v1Dlg::Seperator()  //分离
  1260. {
  1261. static CRect rectLarge;
  1262. static CRect rectSmall;
  1263. if(rectLarge.IsRectNull())
  1264. {
  1265. CRect rectSeparator;
  1266. GetWindowRect(&rectLarge);
  1267. GetDlgItem(IDC_SEPERATOR)->GetWindowRect(&rectSeparator);
  1268. rectSmall.left=rectLarge.left;
  1269. rectSmall.top=rectLarge.top;
  1270. rectSmall.right=rectLarge.right;
  1271. rectSmall.bottom=rectSeparator.bottom;
  1272. }
  1273. SetWindowPos(NULL,0,0,rectLarge.Width(),rectLarge.Height(),SWP_NOMOVE | SWP_NOZORDER);
  1274. }
  1275. void CFaceMFC_v1Dlg::merge()    //合并
  1276. {
  1277. static CRect rectLarge;
  1278. static CRect rectSmall;
  1279. if(rectLarge.IsRectNull())
  1280. {
  1281. CRect rectSeparator;
  1282. GetWindowRect(&rectLarge);
  1283. GetDlgItem(IDC_SEPERATOR)->GetWindowRect(&rectSeparator);
  1284. rectSmall.left=rectLarge.left;
  1285. rectSmall.top=rectLarge.top;
  1286. rectSmall.right=rectLarge.right;
  1287. rectSmall.bottom=rectSeparator.bottom;
  1288. }
  1289. SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),SWP_NOMOVE | SWP_NOZORDER);
  1290. }