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

📄 acontourcollection.cpp

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*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*/

#define __COLLECTION__
#include "time.h"
#include "CvTest.h"

#define _CV_LINE_PUTSTARTPT 0x1
#define _CV_LINE_PUTENDPT   0x2

#define _MAX_BUFF_SIZE__  10

#define PI() (acos(-1))

/*****************************************************************************/
/*                          Secondaty function                               */
/*****************************************************************************/

double GetVald( double MaxVal, double Dispacement = 0. )
{
    ///////
    static double buff[ _MAX_BUFF_SIZE__ ];
    static bool   first = true;
    ///////
    bool   flag;
    double val = 0;
    int i;

    srand( (unsigned)time( NULL ) );

    srand( (unsigned)time( NULL ) );
    flag = true;
    while( flag )
    {
        val  = (double)rand() * MaxVal / RAND_MAX;
        val += Dispacement;

        if( first )
        {
            flag = false;
            first = false;
            for( i = 0 ; i < _MAX_BUFF_SIZE__ ; i++ )
            {
                buff[i] = static_cast<int>(val);
            }
        }
        else
        {
            flag = false;
            for( i = 0 ; i < _MAX_BUFF_SIZE__ ; i++ )
            {
                if( buff[i] == val ) flag = true;
            }
        }
    }

    for( i = _MAX_BUFF_SIZE__ - 1; i > 0 ; i-- )
    {
        buff[ i ] = buff[ i - 1 ];
    }

    buff[0] = static_cast<int>(val);
    return val;
} // double GetVald( double MaxVal, double Dispacement = 0. )

int GetVali( double MaxVal, double Dispacement = 0. )
{
    ////////
    static int buff[ _MAX_BUFF_SIZE__ ];
    static bool   first = true;
    static int    maxloop = 1000;
    ////////
    bool   flag;
    double val = 0;
    int i;

    srand( (unsigned)time( NULL ) );
    flag = true;
    int loop = maxloop;
    while( flag & ( loop != 0 ) )
    {
        loop--;
        val  = (  (double)rand() * MaxVal ) / (double)RAND_MAX;
        val += Dispacement;

        if( first )
        {
            flag = false;
            first = false;
            for( i = 0 ; i < _MAX_BUFF_SIZE__ ; i++ )
            {
                buff[i] = static_cast<int>(val);
            }
        }
        else
        {
            flag = false;
            for( i = 0 ; i < _MAX_BUFF_SIZE__ ; i++ )
            {
                if( buff[i] == static_cast<int>(val) ) flag = true;
            }
        }
    }

    for( i = _MAX_BUFF_SIZE__ - 1; i > 0 ; i-- )
    {
        buff[ i ] = buff[ i - 1 ];
    }

    buff[0] = static_cast<int>(val);

    return static_cast<int>(val);
} // GetVali( double MaxVal, double Dispacement = 0. )


bool CreateContour( CvSeq** ppSeq, int flags, 
                    CvMemStorage** ppStorage )
{
    ///////////////////////
    if( (ppSeq == NULL) || (ppStorage==NULL) )
    {
        return false;
    } // if( (ppSeq == NULL) || (ppStorage==NULL) || (pWriter==NULL) )

    ///////////////////////

    *ppSeq = cvCreateSeq( flags, sizeof(CvContour), sizeof(CvPoint), *ppStorage);
    if( *ppSeq == NULL )
    {
        return false;
    } // if( *ppSeq == NULL )

    return true;
} // CreateContour


void Line( CvPoint StartPt, CvPoint EndPt, CvSeq* Seq,
           int flags = _CV_LINE_PUTSTARTPT | _CV_LINE_PUTENDPT )
{
    //////////////// variables ///////////////
    CvPoint Pt;
    int dx = abs( StartPt.x - EndPt.x );
    int dy = abs( StartPt.y - EndPt.y );
    int sx = ( ( StartPt.x > EndPt.x ) ? -1 : 1 );
    int sy = ( ( StartPt.y > EndPt.y ) ? -1 : 1 );
    int x, y;
    int d, d1, d2;
    int i;

    ////////////// put start point //////////////////
    if( flags & _CV_LINE_PUTSTARTPT )
    {
        cvSeqPush( Seq, &StartPt );
    }

    /////////// drow line ///////////////////
    if( dy < dx )
    {
        d2 = 2*( dy - dx );
        d1 = 2*dy;
        d  = d1 - dx;
        
        for( x = StartPt.x, y = StartPt.y, i=1; i < dx ; i++ )
        {
            if( d > 0 )
            {
                d += d2;
                y += sy;
            } // if( d > 0 )
            else
            {
                d += d1;
            } // if( d > 0 ) else

            x += sx;
            Pt.x = x;
            Pt.y = y;
            cvSeqPush( Seq, &Pt );
        }
    } // if( dy < dx )
    else
    {
        d2 = 2*( dx - dy );
        d1 = 2*dx;
        d  = d1 - dy;

        for( y = StartPt.y, x = StartPt.x, i=1; i < dy ; i++ )
        {
            if( d > 0 )
            {
                d += d2;
                x += sx;
            } // if( d > 0 )
            else
            {
                d += d1;
            } // if( d > 0 ) else

            y += sy;
            Pt.x = x;
            Pt.y = y;
            cvSeqPush( Seq, &Pt );
        } // for( y = StartPt.y, x = StartPt.x, i=1; i < dy ; i++ )
    } // if( dy < dx ) else
    
    //////////////  put end point //////////////////
    if( flags & _CV_LINE_PUTENDPT )
    {
            cvSeqPush( Seq, &EndPt );
    } // if( flags & _CV_LINE_PUTENDPT )

    return;
} // Line

/*****************************************************************************/
/*****************************************************************************/
/***                          Collection                                   ***/
/*****************************************************************************/
/*****************************************************************************/

/****************************************************************/
/****************       PolyLine        *************************/
/****************************************************************/
CvSeq* PolyLine( CvMemStorage* Storage )
{
    ///////////////////////
    int Max_dY = 50;
    int Max_dX = 50;
    int Max_Piece = 100;
    //////
    CvPoint FirstPt,SecondPt;
    int x,y;
    int P;
    /////
    CvSeq*        Seq     = NULL;

    ///////////// create contour //////////
    if( !CreateContour( &Seq,CV_SEQ_POLYGON,&Storage) )
    {
        return NULL;
    }
    
    /////////////// initialising /////////
    P = GetVali( Max_Piece );

    FirstPt.x = FirstPt.y = 0;
    cvSeqPush( Seq, &FirstPt );

    ////////// make contour ////////////
    for( int i = 0 ; i < P ; i++ )
    {
        x = y = 0;
        while( ( x == 0 ) || ( y == 0 ) )
        {
            x = GetVali( Max_dX );
            y = GetVali( Max_dY );
        }

        SecondPt.x = FirstPt.x;
        SecondPt.y = FirstPt.y;
        FirstPt.x += x;
        FirstPt.y += y;
        Line( SecondPt,FirstPt,Seq,_CV_LINE_PUTENDPT);
    }
    
    //////////////////////////

    return Seq;
} // CvSeq* PolyLine()

/****************************************************************/
/****************        arc cos        *************************/
/****************************************************************/
CvSeq* arccos( CvMemStorage* Storage )
{
    /////
    int X_Mul_Max = 10;
    int Y_Mul_Max = 10;
    /////
    int Y_Mul = 1;
    int X_Mul = 1;
    double displacement = 0.1;
    double interval     = 10;
    double t, t_step;
    /////
    CvPoint FirstPt, SecondPt;
    /////
    CvSeq*        Seq     = NULL;

    ///////////// create contour //////////
    if( !CreateContour( &Seq,CV_SEQ_POLYGON,&Storage) )
    {
        return NULL;
    }

    /////////////// initialising /////////
    t_step = 0;
    while( t_step == 0. )
    {
        Y_Mul = GetVali( Y_Mul_Max ,1);
        X_Mul = GetVali( X_Mul_Max ,1);

        interval = GetVald( 2 * PI()  );
        displacement = GetVald( 2 * PI() - interval );

        t_step = (int)( interval * X_Mul );
        t_step = interval / t_step;
    }

    //////////// create contour //////////////////////////
    FirstPt.x = static_cast<int>( displacement * X_Mul );
    FirstPt.y = static_cast<int>( acos( displacement ) * Y_Mul );
    cvSeqPush( Seq, &FirstPt );

    for( t = displacement - PI() ; t < interval + displacement ; t += t_step )
    {
        SecondPt.x = FirstPt.x;
        SecondPt.y = FirstPt.y;

        FirstPt.x = static_cast<int>( t * X_Mul );
        FirstPt.y = static_cast<int>( acos( t ) * Y_Mul );

        Line( SecondPt,FirstPt,Seq,_CV_LINE_PUTENDPT);
    }

    return Seq;
} //CvSeq* arccos()

/****************************************************************/
/****************        arc tangent    *************************/
/****************************************************************/
CvSeq* arctan( CvMemStorage* Storage )
{
    /////
    int X_Mul_Max = 5;
    int Y_Mul_Max = 5;
    /////
    int Y_Mul = 1;
    int X_Mul = 1;
    double displacement = 0.1;
    double interval     = 0.1;
    double t, t_step;
    /////
    CvPoint FirstPt, SecondPt;
    /////
    CvSeq*        Seq     = NULL;

    ///////////// create contour //////////
    if( !CreateContour( &Seq,CV_SEQ_POLYGON,&Storage) )
    {
        return NULL;
    }

    /////////////// initialising /////////
    t_step = 0;
    while( t_step == 0. )
    {
        Y_Mul = GetVali( Y_Mul_Max ,1);
        X_Mul = GetVali( X_Mul_Max ,1);

        interval = GetVald( 2 * PI() -0.1, 0.1 );
        displacement = GetVald( 2 * PI() - interval - 0.1, 0.1);

        t_step = (int)( interval * X_Mul );

⌨️ 快捷键说明

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