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

📄 mainfrm.cpp

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 CPP
📖 第 1 页 / 共 2 页
字号:

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

#endif //_DEBUG

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


void CMainFrame::OnDestroy() 
{
    DestroyFilterGraph();

    SafeRelease( m_FilterGraph );
    SafeRelease( m_CamShift );
    SafeRelease( m_CamShiftProp );
    SafeRelease( m_SourceFilter );

    CoUninitialize();
    CFrameWnd::OnDestroy();
}

void CMainFrame::StartGraph() 
{
    if( m_MediaControl )
    {
        CWnd* av = GetActiveView();

        m_VideoWindow->put_Owner((OAHWND)av->m_hWnd);
        m_VideoWindow->put_WindowStyle(WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN);
        m_VideoWindow->put_MessageDrain((OAHWND)av->m_hWnd);
        //m_CamShiftProp->StartTracking();

        SetVideoWindowSize(); 
        m_MediaControl->Run();
    }
}

void CMainFrame::StopGraph() 
{
    if( m_CamShiftProp ) m_CamShiftProp->StopTracking();
    if( m_MediaControl )
    {
        m_MediaControl->Stop();
        m_VideoWindow->put_Visible(OAFALSE);
        m_VideoWindow->put_Owner(NULL);
        m_VideoWindow->put_MessageDrain(0);
    }
}

void CMainFrame::OnSize(UINT nType, int cx, int cy) 
{
    CFrameWnd::OnSize(nType, cx, cy);
    SetVideoWindowSize();
}


CRect  CMainFrame::StartTrackerInit()
{
    CRect r( 0, 0, 320, 240 );
    if( m_MediaControl )
    {
        AM_MEDIA_TYPE am;
        OAFilterState fs = State_Stopped;
        m_MediaControl->GetState( 0, &fs );

        if( fs == State_Running )
        {
            if( m_input != 0 )
                m_MediaControl->Pause();
            else
                m_MediaControl->Stop();
        }
        
        IPin* pCamShiftIn = get_pin( m_CamShift, PINDIR_INPUT );
        pCamShiftIn->ConnectionMediaType( &am );

        if( am.pbFormat )
        {
            r.right  = ((VIDEOINFOHEADER*)am.pbFormat)->bmiHeader.biWidth;
            r.bottom = ((VIDEOINFOHEADER*)am.pbFormat)->bmiHeader.biHeight;
            r.bottom = abs( r.bottom );
        }
        else
        {
            r.SetRect(0,0,0,0);
        }

        pCamShiftIn->Release();
    }

    m_src_rect = r;

    return r;
}


void  CMainFrame::EndTrackerInit( CRect r )
{
    if( m_MediaControl && m_CamShiftProp &&
        !r.IsRectEmpty() && !m_src_rect.IsRectEmpty())
    {
        CvCamShiftParams params;
        m_wndBar.ReadParamsFromControls( params );

        params.x = ((float)r.left)/MAX(m_src_rect.Width(),1);
        params.y = ((float)r.top)/MAX(m_src_rect.Height(),1);
        params.width = ((float)r.Width())/MAX(m_src_rect.Width(),1);
        params.height = ((float)r.Height())/MAX(m_src_rect.Height(),1);

        m_CamShiftProp->SetParams( &params ); 
        if( !r.IsRectEmpty() )
            m_CamShiftProp->StartTracking();
        else
            m_CamShiftProp->StopTracking();

        m_MediaControl->Run();
    }
}

void  CMainFrame::OnCamera()
{
    m_input = 0;
    ChangeSource();
}

void CMainFrame::OnUpdateCamera(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck( m_input == 0 );
}


void  CMainFrame::OnBall()
{
    m_input = 1;
    ChangeSource();
}

void CMainFrame::OnUpdateBall(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck( m_input == 1 );
}

void CMainFrame::OnFile() 
{
    m_input = 2;
    ChangeSource();
}


void CMainFrame::OnUpdateFile(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck( m_input == 2 );
}

void CMainFrame::ChangeSource()
{
    StopGraph();
    DestroyFilterGraph();

    if( m_input < 2 )
    {
        switch( m_input )
        {
        case 0: /* camera */ CreateCamera();
            break;
        default: /* ball */ CreateBouncingBall();
            break;
        }

        if( m_SourceFilter == 0 )
        {
            m_input = -1;
            return;
        }
    }

    if( !CreateFilterGraph() && m_input == 0 )
    {
        for( int i = 1; i <= 10; i++ )
        {
            CreateCamera(i);
            if( CreateFilterGraph())
                break;
        }
    }

    if( m_SourceFilter )
        show_prop_page( m_SourceFilter, L"Video Source Properties" );

    StartGraph();
}


void CMainFrame::OnPlayPause() 
{
    if( m_MediaControl )
    {
        OAFilterState fs = State_Stopped;
        if( m_MediaControl->GetState( 0, &fs ) == S_OK )
        {
            if( fs == State_Running )
                m_MediaControl->Pause();
            else if( fs == State_Paused )
                m_MediaControl->Run();
        }
    }
}


void CMainFrame::OnUpdatePlayPause(CCmdUI* pCmdUI) 
{
    OAFilterState fs = State_Stopped;
    if( m_MediaControl )
    {
        m_MediaControl->GetState( 0, &fs );
    }
    pCmdUI->SetCheck( fs == State_Running );
    pCmdUI->Enable( fs == State_Running || fs == State_Paused );
}


void CMainFrame::GetScaleFactor( int& sx, int &sy )
{
    sx = ((CSliderCtrl*)m_wndBar.GetDlgItem(IDC_WIN_WIDTH))->GetPos();
    sy = ((CSliderCtrl*)m_wndBar.GetDlgItem(IDC_WIN_HEIGHT))->GetPos();
}

CRect CMainFrame::GetVideoWindowSize() 
{
    CWnd* av = GetActiveView();
    CRect rc;
    if( av )
    {
        int width, height;
        GetScaleFactor( width, height );

        av->GetClientRect( &rc );

        rc.right  = rc.right * width / 100;
        rc.bottom = rc.bottom * height / 100;
    }

    return rc;
}


void CMainFrame::SetVideoWindowSize() 
{
    CRect rc = GetVideoWindowSize();
    if( m_VideoWindow )
    {
        m_VideoWindow->SetWindowPosition( rc.left, rc.top, rc.right, rc.bottom );
    }
}


/////////////////////////////////////////////////////////////////////////////
// CBar dialog


CBar::CBar() : CDialogBar()
{
	//{{AFX_DATA_INIT(CBar)
	m_Threshold = 0;
	m_Smin = 0;
    m_Vmax = 0;
	m_Vmin = 0;
	m_Bins = 0;
    m_ShowPicture = TRUE;
    m_ShowBackProject = FALSE;
    m_ShowHistogram = FALSE;
	//}}AFX_DATA_INIT
}


void CBar::DoDataExchange(CDataExchange* pDX)
{
	CDialogBar::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBar)
	DDX_Check(pDX,  IDC_PICTURE, m_ShowPicture );
    DDX_Check(pDX,  IDC_BACKPROJECT, m_ShowBackProject );
    DDX_Check(pDX,  IDC_HISTOGRAM, m_ShowHistogram );
    DDX_Slider(pDX, IDC_SMIN, m_Smin);
	DDX_Slider(pDX, IDC_THRESHOLD, m_Threshold);
	DDX_Slider(pDX, IDC_VMAX, m_Vmax);
	DDX_Slider(pDX, IDC_VMIN, m_Vmin);
	DDX_Slider(pDX, IDC_BINS, m_Bins);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBar, CDialogBar)
	//{{AFX_MSG_MAP(CBar)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_PICTURE, OnPicture)
	ON_BN_CLICKED(IDC_BACKPROJECT, OnBackProject)
	ON_BN_CLICKED(IDC_HISTOGRAM, OnHistogram)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBar message handlers

void CBar::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
    int  code = pScrollBar->GetDlgCtrlID();
    CMainFrame* mainframe = (CMainFrame*)AfxGetMainWnd();

    UpdateData(1);
    
    switch( code )
    {
    case IDC_SMIN:
    case IDC_VMIN:
    case IDC_VMAX:
    case IDC_THRESHOLD:
    case IDC_BINS:
        if( mainframe->m_CamShiftProp )
        {
            CvCamShiftParams params;
            mainframe->m_CamShiftProp->GetParams( &params );
            int bins = params.bins;
            ReadParamsFromControls( params );
            mainframe->m_CamShiftProp->SetParams( &params );
            if( bins != params.bins )
            {
                mainframe->m_CamShiftProp->StartTracking();
            }
        }
        break;
    case IDC_WIN_WIDTH:
    case IDC_WIN_HEIGHT:
        mainframe->SetVideoWindowSize();
        break;
    default:
        ASSERT(0);
    }
}

void CBar::OnPicture() 
{
    UpdateData(1);
    CMainFrame* mainframe = (CMainFrame*)AfxGetMainWnd();
    if( mainframe->m_CamShiftProp )
    {
        CvCamShiftParams params;
        mainframe->m_CamShiftProp->GetParams( &params );
        params.view = 0;
        mainframe->m_CamShiftProp->SetParams( &params );
    }
}

void CBar::OnBackProject() 
{
    UpdateData(1);
    CMainFrame* mainframe = (CMainFrame*)AfxGetMainWnd();
    if( mainframe->m_CamShiftProp )
    {
        CvCamShiftParams params;
        mainframe->m_CamShiftProp->GetParams( &params );
        params.view = 1;
        mainframe->m_CamShiftProp->SetParams( &params );
    }
}

void CBar::OnHistogram() 
{
    UpdateData(1);
    CMainFrame* mainframe = (CMainFrame*)AfxGetMainWnd();
    if( mainframe->m_CamShiftProp )
    {
        CvCamShiftParams params;
        mainframe->m_CamShiftProp->GetParams( &params );
        params.view = 2;
        mainframe->m_CamShiftProp->SetParams( &params );
    }
}


CBar::ReadParamsFromControls( CvCamShiftParams& params )
{
    CSliderCtrl* threshold = (CSliderCtrl*)GetDlgItem(IDC_THRESHOLD);
    CSliderCtrl* bins      = (CSliderCtrl*)GetDlgItem(IDC_BINS);
    CSliderCtrl* smin      = (CSliderCtrl*)GetDlgItem(IDC_SMIN);
    CSliderCtrl* vmin      = (CSliderCtrl*)GetDlgItem(IDC_VMIN);
    CSliderCtrl* vmax      = (CSliderCtrl*)GetDlgItem(IDC_VMAX);
    params.view = ((CButton*)GetDlgItem(IDC_PICTURE))->GetCheck() ? 0 :
                  ((CButton*)GetDlgItem(IDC_BACKPROJECT))->GetCheck() ? 1 : 2;
    
    params.Smin = smin->GetPos();
    params.Vmin = vmin->GetPos();
    params.Vmax = vmax->GetPos();

    params.threshold = threshold->GetPos();

    params.bins = bins->GetPos();
}


CBar::WriteParamsToControls( CvCamShiftParams& params )
{
    CSliderCtrl* threshold = (CSliderCtrl*)GetDlgItem(IDC_THRESHOLD);
    CSliderCtrl* bins      = (CSliderCtrl*)GetDlgItem(IDC_BINS);
    CSliderCtrl* smin      = (CSliderCtrl*)GetDlgItem(IDC_SMIN);
    CSliderCtrl* vmin      = (CSliderCtrl*)GetDlgItem(IDC_VMIN);
    CSliderCtrl* vmax      = (CSliderCtrl*)GetDlgItem(IDC_VMAX);
    
    smin->SetPos( params.Smin );

    vmin->SetRange( 0, 255 );
    vmin->SetPos( params.Vmin );

    vmax->SetRange( 0, 255 );
    vmax->SetPos( params.Vmax );

    threshold->SetRange( 0, 255 );
    threshold->SetPos( params.threshold );
    threshold->SetTicFreq( 16 );

    bins->SetRange( 2, 255 );
    bins->SetPos( params.bins );
    bins->SetTicFreq( 10 );

    ((CButton*)GetDlgItem(IDC_PICTURE))->SetCheck( params.view == 0 );
    ((CButton*)GetDlgItem(IDC_BACKPROJECT))->SetCheck( params.view == 1 );
    ((CButton*)GetDlgItem(IDC_HISTOGRAM))->SetCheck( params.view == 2 );
}

⌨️ 快捷键说明

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