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

📄 condens.cpp

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include <windows.h>
#include <streams.h>
#include <initguid.h>
#include <olectl.h>
#if (1100 > _MSC_VER)
#include <olectlid.h>
#endif
#include "iCondens.h"
#include "Condensprop.h"
#include "Condens.h"
#include "Condensuids.h"


// setup data
const AMOVIESETUP_MEDIATYPE sudPinTypes =
{
    &MEDIATYPE_Video,       // Major type
    &MEDIASUBTYPE_NULL      // Minor type
};

const AMOVIESETUP_PIN psudPins[] =
{
    {
        L"Input",           // String pin name
        FALSE,              // Is it rendered
        FALSE,              // Is it an output
        FALSE,              // Allowed none
        FALSE,              // Allowed many
        &CLSID_NULL,        // Connects to filter
        L"Output",          // Connects to pin
        1,                  // Number of types
        &sudPinTypes },     // The pin details
      { L"Output",          // String pin name
        FALSE,              // Is it rendered
        TRUE,               // Is it an output
        FALSE,              // Allowed none
        FALSE,              // Allowed many
        &CLSID_NULL,        // Connects to filter
        L"Input",           // Connects to pin
        1,                  // Number of types
        &sudPinTypes        // The pin details
    }
};


const AMOVIESETUP_FILTER sudCCondens =
{
    &CLSID_CCondens,        // Filter CLSID
    L"Condens",                    // Filter name
    MERIT_DO_NOT_USE,               // Its merit
    2,                              // Number of pins
    psudPins                        // Pin details
};


// List of class IDs and creator functions for the class factory. This
// provides the link between the OLE entry point in the DLL and an object
// being created. The class factory will call the static CreateInstance

CFactoryTemplate g_Templates[2] = {

    { L"Condens"
    , &CLSID_CCondens
    , CCondens::CreateInstance
    , NULL
    , &sudCCondens }
  ,
    { L"Condens Property Page"
    , &CLSID_CCondensPropertyPage
    , CCondensProperties::CreateInstance }
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);



//
// Constructor
//
CCondens::CCondens(TCHAR *tszName,LPUNKNOWN punk,HRESULT *phr) :
    CTransInPlaceFilter(tszName, punk, CLSID_CCondens,phr)    
   
{
    m_params.nSamples = 64;
    m_params.x = 0.4f; 
    m_params.y = 0.3f;
    m_params.width = 0.2f;
    m_params.height = 0.3f;

    m_params.Smin = 20;
    m_params.Vmin = 40;
    m_params.view = 0;

    m_Indicat1.x = 0; 
    m_Indicat1.y = 0;
    m_Indicat1.width = 100;
    m_Indicat1.height = 60;
    IsTracking = false;
    IsInit =false;
    ConDens=cvCreateConDensation(4,4,m_params.nSamples);
    CvMat Dyn = cvMat(4,4,CV_MAT4x4_32F,ConDens->DynamMatr);
    cvmSetIdentity(&Dyn);
    cvmSet(&Dyn,0,1,0.9);
    cvmSet(&Dyn,2,3,0.9);
    ASSERT(tszName);
    ASSERT(phr);
} // CCondens
CCondens::~CCondens()
{
    cvReleaseConDensation(&ConDens);
} // ~CCondens


//
// CreateInstance
//
// Provide the way for COM to create a CCondens object
//
CUnknown * WINAPI CCondens::CreateInstance(LPUNKNOWN punk, HRESULT *phr) {

    CCondens *pNewObject = new CCondens(NAME("Condens"), punk, phr);
    if (pNewObject == NULL) {
        *phr = E_OUTOFMEMORY;
    }
    return pNewObject;

} // CreateInstance


//
// NonDelegatingQueryInterface
//
// Reveals ICCondens and ISpecifyPropertyPages
//
STDMETHODIMP CCondens::NonDelegatingQueryInterface(REFIID riid, void **ppv)
{
    CheckPointer(ppv,E_POINTER);

    if (riid == IID_ICCondens) {
        return GetInterface((ICCondens *) this, ppv);
    } else if (riid == IID_ISpecifyPropertyPages) {
        return GetInterface((ISpecifyPropertyPages *) this, ppv);
    } else {
        return CTransInPlaceFilter::NonDelegatingQueryInterface(riid, ppv);
    }

} // NonDelegatingQueryInterface



void  CCondens::ApplyCamShift( CvImage* image, bool initialize )
{
    CvSize size;
    int bins = 20;

    m_cCamShift.set_hist_dims( 1, &bins );
    m_cCamShift.set_thresh( 0, 1, 180 );
    m_cCamShift.set_min_ch_val( 1, m_params.Smin );
    m_cCamShift.set_max_ch_val( 1, 255 );
    m_cCamShift.set_min_ch_val( 2, m_params.Vmin );
    m_cCamShift.set_max_ch_val( 2, 255 );
    
    cvGetImageRawData( image, 0, 0, &size );
	
    if( m_object.x > size.width - m_object.width - 1 )
        m_object.x = size.width - m_object.width - 1;
	if( m_object.x < 0 ) m_object.x = 0;
    
    if( m_object.y > size.height - m_object.height - 1 )
        m_object.y = size.height - m_object.height - 1;
	if( m_object.y < 0 ) m_object.y = 0;
    m_cCamShift.set_window(m_object);
    
    if( initialize )
    {
        m_cCamShift.reset_histogram();
        m_cCamShift.update_histogram( image );
    }

    m_cCamShift.track_object( image );
    m_object = m_cCamShift.get_window();

    LBound[0] = (float)m_object.x;
    LBound[1] = (float)-m_object.width*0.5f;
    LBound[2] = (float)m_object.y;
    LBound[3] = (float)- m_object.height*0.5f;
    UBound[0] = (float)m_object.x + m_object.width;
    UBound[1] = (float)m_object.width*0.5f;
    UBound[2] = (float)m_object.y + m_object.height;
    UBound[3] = (float)m_object.height*0.5f;
    Measurement[0] = (float)m_object.x+m_object.width*0.5f;
    Measurement[1] = initialize ? 0 : (float)(Measurement[0] - m_Old.x);
    Measurement[2] = (float)m_object.y+m_object.height*0.5f;
    Measurement[3] = initialize ? 0 : (float)(Measurement[2] - m_Old.y);
    m_Old.x = cvRound( Measurement[0] );
    m_Old.y = cvRound( Measurement[2] );
    if( initialize )
    {
        CvMat LB = cvMat(4,1,CV_MAT4x1_32F,LBound);
        CvMat UB = cvMat(4,1,CV_MAT4x1_32F,UBound);
        cvConDensInitSampleSet(ConDens,&LB,&UB);
    }
    XCor = 1.5f/m_object.width;
    VXCor = 3.0f/m_object.width;
    YCor = 1.5f/m_object.height;
    VYCor = 3.0f/m_object.height;
    CondProbDens(ConDens,Measurement);

    m_Old.x = cvRound( Measurement[0] );
    m_Old.y = cvRound( Measurement[2] );
}


void  CCondens::CheckBackProject( CvImage* image )
{
    if( m_params.view != 0 )
    {
        IplImage* src = m_cCamShift.get_back_project();
        if( src && src->imageData && image )
        {
            iplGrayToColor( src, image, 0, 0, 0 );
        }
    }
}


//
// Transform
// Transform the  sample 'in place'
//
HRESULT CCondens::Transform(IMediaSample *pSample)
{
	BYTE*   pData;
    CvImage image;

	IplImage* image2;    //ianni  <======

	pSample->GetPointer(&pData);

    AM_MEDIA_TYPE* pType = &m_pInput->CurrentMediaType();
    VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *) pType->pbFormat;

    // Get the image properties from the BITMAPINFOHEADER
    CvSize size = cvSize( pvi->bmiHeader.biWidth, pvi->bmiHeader.biHeight );
    int stride = (size.width * 3 + 3) & -4;

	image2=cvCreateImage(cvSize(100,100),IPL_DEPTH_8U, 3);  //ianni <========
	cvReleaseImage(&image2); //ianni <===========
        
    cvInitImageHeader( &image, size, IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4, 1 );
    cvSetImageData( &image, pData,stride );

⌨️ 快捷键说明

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