⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 face detection opencv
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
/*                                                                                                   
                                                                                                    
                                                                                                    
                                                                                            ;;      
                                                                                          wwww;;    
                                                                                        kkkkkk""    
                            ;;;;                                                    ;;kkkkkkkk""    
          ""tt    kkkktt;;wwkkJJ                                        ""tt        kkkkkkkkkk""    
        ""kkkktt  kkkktt;;kkkkJJ              ;;;;      ""ttJJ;;kkkk""ttkkkktt      JJkkkkkkkk""    
        ;;kkkkJJ  kkkktt;;kkkkJJ""JJ""      kkkkkkkk;;  JJkkkkkkkkkkkkkkkkkkkk          kkkkkk""    
        ;;kkkkkk  kkkktt;;kkkkJJkkkkkk;;  kkkkkkkkkkkk  JJkkkkttkkkkkk;;kkkkkk;;        JJkkkk""    
        ;;kkkkkk;;kkkktt;;kkkkkkkkkkkktt  kkkkttJJkkkk;;ttkkkk;;JJkkkk;;kkkkkk;;        JJkkkk""    
        ;;kkkkkk""kkkktt;;kkkkJJ""kkkktt""kkkkttttkkkk""ttkkkk;;JJkkkk;;kkkkkk;;        JJkkkk""    
        ;;kkkkkkttkkkktt;;kkkktt""kkkktt""kkkkttttkkkk""ttkkkk;;JJkkkk;;kkkkJJ;;        JJkkkk""    
        ;;kkkkkkJJkkkktt;;kkkktt""kkkktt""kkkkttttkkkk""ttkkkk;;JJkkkk;;kkkkJJ;;        JJkkkk""    
        ;;kkJJJJJJJJkktt;;kkJJtt""kkkktt""kkJJttttJJkk""ttkkkk;;JJkkkk;;kkJJJJ;;        JJkkkk""    
        ;;kkJJJJJJJJkktt;;kkJJtt""JJJJtt""JJJJttttJJJJ""ttJJkk;;JJJJkk;;kkJJJJ;;        JJJJkk""    
        ;;kkJJttJJJJJJtt;;JJJJtt""JJJJtt""JJJJttttJJJJ""ttJJkk;;JJJJJJ;;kkJJJJ;;        JJJJkk""    
        ;;JJJJttJJJJJJtt;;JJJJtt""JJJJtt""JJJJttttJJJJ""ttJJJJ;;JJJJkk;;kkJJJJ;;        JJJJkk""    
        ;;JJJJ""JJJJJJtt;;JJJJtt""JJJJtt""JJJJttttJJJJ""ttJJJJ;;JJJJJJ;;JJJJJJ;;        JJJJJJ""    
        ;;JJJJ;;JJJJJJtt;;JJJJtt""JJJJtt""JJJJttJJJJJJ;;ttJJJJ;;JJJJJJ;;kkJJJJ;;        JJJJJJ""    
        ;;JJJJ""ttJJJJtt;;JJJJtt""JJJJtt  kkJJJJJJJJtt;;ttJJJJ;;JJJJJJ;;JJJJtt;;          ;;;;;;    
        ;;JJJJ""""JJJJtt;;JJJJtt""JJJJtt  ""kkJJJJtt""  ;;tttt""  ;;;;;;  ;;                        
        ;;JJJJ"";;JJJJtt;;JJJJtt""tttttt      """";;                                                
        ;;JJtt""  """"""    ;;;;                                                                    
                                                                                                    
                                                                                                    
                                                                                                   
*/

#include "stdafx.h"
#include "Nhom1_CT901.h"

#include "MainFrm.h"

#include "cv.h" 
#include "highgui.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_XemAnh, OnXemAnh)
	ON_COMMAND(ID_TimKhuonMatTrongAnh, OnTimKhuonMatTrongAnh)
	ON_COMMAND(ID_XemVideo, OnXemVideo)
	ON_COMMAND(ID_TimKhuonMatTrongVideo, OnTimKhuonMatTrongVideo)
	ON_COMMAND(ID_Canny, OnCanny)
	ON_COMMAND(ID_Sobel, OnSobel)
	ON_COMMAND(ID_Laplace, OnLaplace)
	ON_COMMAND(ID_Gray, OnGray)
	ON_COMMAND(ID_About, OnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

//---------------------------------------------------------------------------

void CMainFrame::OnXemAnh() 
{
	// TODO: Add your command handler code here
	IplImage* newImg;
	newImg = cvLoadImage("Input.jpg",1);
	//create a new window
	cvNamedWindow("Xem Anh", 1); 
	//display the image in the window
	cvShowImage("Xem Anh", newImg);
	//wait for key to close the window
	cvWaitKey(0);
	cvDestroyWindow( "Xem Anh" ); //destroy the window
	//cvReleaseImage( &newImg ); //release the memory for the image
}

//---------------------------------------------------------------------------

void CMainFrame::OnTimKhuonMatTrongAnh() 
{
	// TODO: Add your command handler code here
	//declarations
	CvHaarClassifierCascade* cascade;
	CvMemStorage* storage;
	IplImage *image;
	CvSeq* faces;
	const char* file_name;
	int i;

	//initializations
	storage=cvCreateMemStorage(0);
	cvFirstType();
	file_name="haarcascade_frontalface_alt.xml";
	cascade = (CvHaarClassifierCascade*)cvLoad(file_name,NULL, NULL, NULL);
	image = cvLoadImage("Input.jpg", 1 );
	faces = cvHaarDetectObjects( image, cascade, storage, 1.2, 2,
                CV_HAAR_DO_CANNY_PRUNING,cvSize(0, 0));
	
	//draw rectangles
	for(i=0;i<(faces ? faces->total:0); i++ )
	{
		CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
		CvPoint pt1 = { r->x, r->y };
		CvPoint pt2 = { r->x + r->width, r->y + r->height };
		cvRectangle( image, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
	}

	// create window and show the image with outlined faces
	cvNamedWindow( "Ket qua tim khuon mat trong anh", 1 );
	cvShowImage( "Ket qua tim khuon mat trong anh", image );
	cvSaveImage("Result_TimKhuonMat.jpg", image);

	cvWaitKey();
	cvDestroyWindow( "Ket qua tim khuon mat trong anh" ); //destroy the window
	//cvReleaseImage( &image );      // after a key pressed, release data
    cvReleaseHaarClassifierCascade( &cascade );
    cvReleaseMemStorage( &storage );
}

//---------------------------------------------------------------------------

void CMainFrame::OnXemVideo() 
{
	// TODO: Add your command handler code here
	IplImage*       tmp_frame = NULL;
    CvCapture*      cap = NULL;

    cap = cvCaptureFromFile("video.avi");
    tmp_frame = cvQueryFrame(cap); // Lay so frame cua video
    if(!tmp_frame)
    {
        printf("Khong mo duoc video \n");
        exit(0);
    }

    cvNamedWindow("Xem Video", 1);

    for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(cap), fr++ )
    {
        double t = (double)cvGetTickCount();
        t = (double)cvGetTickCount() - t;
		//printf( "%.1f\n", t/(cvGetTickFrequency()*1000.) );
        cvShowImage("Xem Video",tmp_frame);
        char k = cvWaitKey(50);          
    }

    cvReleaseCapture(&cap); 
}

//---------------------------------------------------------------------------

void FaceDetection(IplImage* frame, CvMemStorage* storage,CvHaarClassifierCascade* classifier)
{
        // "Resets" the memory but does not deallocate it.
        cvClearMemStorage(storage);

        CvSeq* faces = cvHaarDetectObjects(frame, classifier, storage, 1.1,2, CV_HAAR_DO_CANNY_PRUNING, cvSize(20, 20));

        // If any faces were detected, draw rectangles around them.
        if (faces)
        {
                for(int i = 0; i < faces->total; ++i)
                {
                        // Setup two points that define the extremes of the rectangle,
                        // then draw it to the image..
                        CvPoint point1, point2;
                        CvRect* rectangle = (CvRect*)cvGetSeqElem(faces, i);
                        point1.x = rectangle->x;
                        point2.x = (rectangle->x + rectangle->width);
                        point1.y = rectangle->y;
                        point2.y = (rectangle->y + rectangle->height);
                        cvRectangle(frame, point1, point2, CV_RGB(255,0,0), 3, 8, 0);
                }
        }
} 

//---------------------------------------------------------------------------

void CMainFrame::OnTimKhuonMatTrongVideo() 
{
	
		// Setup either a camera or input video to use for face detection.
//		CvCapture* inputSource = cvCaptureFromCAM(0); // Lay anh tu camera
        CvCapture* inputSource = cvCaptureFromAVI("video.avi");

        // Load the pre-trained Haar classifier data.
        CvHaarClassifierCascade* classifier = (CvHaarClassifierCascade*)cvLoad("haarcascade_frontalface_alt.xml", 0, 0, 0);

        // Quit the application if the input source or the classifier data
        // failed to load properly.
		if(!inputSource || !classifier)
        {
		  MessageBox("Co loi xay ra!");              
        }

        // Create a CvMemStorage object for use by the face detection function.
        CvMemStorage* facesMemStorage = cvCreateMemStorage(0);

        // Setup a window to display out results.
        cvNamedWindow("Ket qua tim khuon mat trong video", 1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -