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

📄 ahistograms.cpp

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    icvFree( &img8s );
    icvFree( &img32f );

    return TRS_OK;
}


static int foaBackProjectPatch(void* _type)
{
    CvHistType type = (CvHistType)(int)_type;
    static int c_dimss;
    static int dims;
    static int init = 0;
    static int width;
    static int height;
    static int range_width;
    static int range_height;

    CvHistogram* hist;

    int    c_dims;
    int    d[CV_HIST_MAX_DIM + 1];
    float* thresh[CV_HIST_MAX_DIM];
    int    i, j, x, y, fl;
    IplImage* src8u[CV_HIST_MAX_DIM];
    IplImage* src8s[CV_HIST_MAX_DIM];
    IplImage* src32f[CV_HIST_MAX_DIM];
    IplImage* dst8u;
    IplImage* dst8s;
    IplImage* dst32f;
    IplImage* _dst8u;
    IplImage* _dst8s;
    IplImage* _dst32f;
    
    int errors = 0;
    int l_err;

    if( !init )
    {
        init = 1;
        trsiRead( &c_dimss, "4", "Number of dimensions" );
        trsiRead( &dims, "10", "Size of every dimension" );
        trsiRead( &width, "16", "Width of source picture" );
        trsiRead( &height, "16", "Height of source picture" );
        trsiRead( &range_width, "3", "Width of range" );
        trsiRead( &range_height, "3", "Height of range" );
    }

    for( c_dims = 1; c_dims <= c_dimss; c_dims++ )
    {
        trsWrite( ATS_CON | ATS_LST, "Dimension %d\n", c_dims );
        /*Creating & filling histograms*/
        for( i = 0; i < c_dims; i++ ) d[i] = dims;
        hist = cvCreateHist( c_dims, d, type );

        for( i = 0; i < c_dims; i++ )
        {
            thresh[i] = (float*)icvAlloc( (hist->dims[i] + 1) * sizeof(**hist->thresh));
            thresh[i][0] = 0.5;
            for( j = 1; j <= hist->dims[i]; j++ ) thresh[i][j] = 0.5f + j;
        }

        cvSetHistThresh( hist, thresh, 0 );

        for( i = 0; i < c_dims; i++ )
        {
            src8u[i] = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
            src8s[i] = cvCreateImage(cvSize(width, height), IPL_DEPTH_8S, 1);
            src32f[i] = cvCreateImage(cvSize(width, height), IPL_DEPTH_32F, 1);
        }

        dst8u = cvCreateImage(cvSize(width, height), IPL_DEPTH_32F, 1);
        dst8s = cvCreateImage(cvSize(width, height), IPL_DEPTH_32F, 1);
        dst32f = cvCreateImage(cvSize(width, height), IPL_DEPTH_32F, 1);

        _dst8u = cvCreateImage(cvSize(width, height), IPL_DEPTH_32F, 1);
        _dst8s = cvCreateImage(cvSize(width, height), IPL_DEPTH_32F, 1);
        _dst32f = cvCreateImage(cvSize(width, height), IPL_DEPTH_32F, 1);
        
        for( i = 0; i < c_dims; i++ )
            for( y = 0; y < height; y++ )
                for( x = 0; x < width; x++ )
                {
                    ((float*)(src32f[i]->imageData))[y * src32f[i]->widthStep / 4 + x] =
                        src8u[i]->imageData[y * src8u[i]->widthStep + x] =
                        src8s[i]->imageData[y * src8s[i]->widthStep + x] = (char)x;
                }

        /* Filling control histograms */
        for( i = 0; i <= CV_HIST_MAX_DIM; i++ ) d[i] = 0;
        for( i = 0; i < c_dims; )
        {
            for( j = 1, fl = 1; j < c_dims; j++ ) fl &= (d[j-1] == d[j]);
            (*cvGetHistValue_nD( hist, d )) = (float)(fl * height);
            for( i = 0; d[i]++, d[i] >= dims && i < CV_HIST_MAX_DIM; i++ ) d[i] = 0;
        }

        myBackProjectPatch(src8u, src8s, src32f, hist, _dst8u, _dst8s, _dst32f, 
                           1, CV_COMP_CHISQR,
                           cvSize(range_width,range_height));

        cvCalcBackProjectPatch( src8u, dst8u, cvSize(range_width,range_height),
                                hist, CV_COMP_CHISQR, 1 );
        /* Checking results */
        l_err = atsCompare2Dfl((float*)dst8u->imageData, (float*)_dst8u->imageData,
                               cvSize(width, height), dst8u->widthStep, 0);
        errors += l_err;
        if(l_err)
            trsWrite( ATS_CON | ATS_LST, "cvCalcBackProjectPatch(8u) - error" );

        cvCalcBackProjectPatch( src8s, dst8s, cvSize(range_width,range_height),
                                hist, CV_COMP_CHISQR, 1 );
        /* Checking results */
        l_err = atsCompare2Dfl((float*)dst8s->imageData, (float*)_dst8s->imageData,
                               cvSize(width, height), dst8s->widthStep, 0);
        errors += l_err;
        if(l_err)
            trsWrite( ATS_CON | ATS_LST, "cvCalcBackProjectPatch(8s) - error" );

        cvCalcBackProjectPatch( src32f, dst32f, cvSize(range_width,range_height),
                                hist, CV_COMP_CHISQR, 1 );
        /* Checking results */
        l_err = atsCompare2Dfl((float*)dst32f->imageData, (float*)_dst32f->imageData,
                               cvSize(width, height), dst32f->widthStep, 0);
        errors += l_err;
        if(l_err)
            trsWrite( ATS_CON | ATS_LST, "cvCalcBackProjectPatch(32f) - error" );

        for( i = 0; i < c_dims; i++ )
        {
            cvReleaseImage( &src8u[i] );
            cvReleaseImage( &src8s[i] );
            cvReleaseImage( &src32f[i] );
            icvFree( &thresh[i] );
        }

        cvReleaseImage( &_dst8u );
        cvReleaseImage( &_dst8s );
        cvReleaseImage( &_dst32f );
        cvReleaseImage( &dst8u );
        cvReleaseImage( &dst8s );
        cvReleaseImage( &dst32f );

        cvReleaseHist( &hist );

    }
    return errors ? trsResult( TRS_FAIL, "Total fixed %d errors", errors ) : TRS_OK;
}/* foaBackProjectPatch */


static int foaBayesianProb(void* _type)
{
    CvHistType type = (CvHistType)(int)_type;
    static int c_dimss;
    static int dims;
    static int init = 0;
    static int width;
    static int height;
    static int range_width;
    static int range_height;
    static double acc;

    int    c_dims;
    int    d[CV_HIST_MAX_DIM + 1];
    CvHistogram* src[10];
    CvHistogram* dst[10];
    int    i, j;
    float* sum = 0;

    int errors = 0;

    if( !init )
    {
        init = 1;
        trsiRead( &c_dimss, "5", "Number of dimensions" );
        trsiRead( &dims, "10", "Size of every dimension" );
        trsdRead( &acc, "0.01", "Accuracy" );
    }

    AtsRandState state;
    int tickcount = (int)atsGetTickCount();
    atsRandInit( &state, -100, 100, tickcount );

    trsWrite( ATS_CON | ATS_LST, "seed = %d\n", tickcount );

    for( c_dims = 1; c_dims <= c_dimss; c_dims++ )
    {
        trsWrite( ATS_CON | ATS_LST, "Dimension %d\n", c_dims );
        /*Creating & filling histograms*/
        for( i = 0; i < c_dims; i++ ) d[i] = dims;

        for( i = 0; i < 10; i++ )
        {
            src[i] = cvCreateHist( c_dims, d, type );
            dst[i] = cvCreateHist( c_dims, d, type );
        }

        int size = src[0]->dims[0] * src[0]->mdims[0];

        sum = (float*)malloc( size * sizeof(float) );
        memset( sum, 0, size * sizeof(float) );

        for( i = 0; i < size; i++ )
            for( j = 0; j < 10; j++ )
            {
                float val = atsRand32f( &state );
                *cvGetHistValue_1D( src[j], i ) = val;
                sum[i] += val;
            }

        // run function
        cvCalcBayesianProb( src, 10, dst );

        // check results
        for( i = 0; i < size; i++ )
            for( j = 0; j < 10; j++ )
            {
                if( atsCompSingle( cvQueryHistValue_1D( dst[j], i ) * sum[i],
                                   cvQueryHistValue_1D( src[j], i ),
                                   acc ) )
                {
                    trsWrite( ATS_CON | ATS_LST, "error: hist %d pos %d, act %f  exp %f\n",
                              j, i,
                              cvQueryHistValue_1D( dst[j], i ) * sum[i],
                              cvQueryHistValue_1D( src[j], i ) );
                    errors++;
                }
            }


        free( sum );
        for( i = 0; i < 10; i++ )
        {
            cvReleaseHist( &src[i] );
            cvReleaseHist( &dst[i] );
        }
    }
    return errors ? trsResult( TRS_FAIL, "Total fixed %d errors", errors ) : TRS_OK;
}/* foaBayesianProb */


void InitAHistograms()
{
    trsRegArg( "cvGetHistValue_1D, cvGetHistValue_2D, cvGetHistValue_3D, cvGetHistValue_nD, "
               "cvQueryHistValue_1D, cvQueryHistValue_2D, cvQueryHistValue_3D, cvQueryHistValue_nD",
               "Histogram Get/Query functions algorithm test",
               "Algorithm", foaHistGetQueryValue, CV_HIST_ARRAY );
    trsRegArg( "cvGetHistValue_1D, cvGetHistValue_2D, cvGetHistValue_3D, cvGetHistValue_nD, "
               "cvQueryHistValue_1D, cvQueryHistValue_2D, cvQueryHistValue_3D, cvQueryHistValue_nD",
               "Histogram Get/Query functions algorithm test",
               "Algorithm", foaHistGetQueryValue, CV_HIST_TREE );

    trsRegArg( "cvGetMinMaxHistValue",
               "Histogram cvGetMinMaxHistValue function algorithm test",
               "Algorithm", foaHistMinMaxValue, CV_HIST_ARRAY );
    trsRegArg( "cvGetMinMaxHistValue",
               "Histogram cvGetMinMaxHistValue function algorithm test",
               "Algorithm", foaHistMinMaxValue, CV_HIST_TREE );

    trsRegArg( "cvNormalizeHist",
               "Histogram cvNormalizeHist function algorithm test",
               "Algorithm", foaNormalizeHist, CV_HIST_ARRAY );
    trsRegArg( "cvNormalizeHist",
               "Histogram cvNormalizeHist function algorithm test",
               "Algorithm", foaNormalizeHist, CV_HIST_TREE );

    trsRegArg( "cvThreshHist",
               "Histogram cvThreshHist function algorithm test",
               "Algorithm", foaThreshHist, CV_HIST_ARRAY );
    trsRegArg( "cvThreshHist",
               "Histogram cvThreshHist function algorithm test",
               "Algorithm", foaThreshHist, CV_HIST_TREE );

    trsRegArg( "cvCompareHist",
               "Histogram comparing function algorithm test",
               "Algorithm", foaHistCompare, CV_HIST_ARRAY );
    trsRegArg( "cvCompareHist",
               "Histogram comparing function algorithm test",
               "Algorithm", foaHistCompare, CV_HIST_TREE );

    trsRegArg( "cvCopyHist",
               "Histogram copying function algorithm test",
               "Algorithm", foaCopyHist, CV_HIST_ARRAY );
    trsRegArg( "cvCopyHist",
               "Histogram copying function algorithm test",
               "Algorithm", foaCopyHist, CV_HIST_TREE );

    trsRegArg( "cvCalcHist",
               "Histogram calculation function algorithm test",
               "Algorithm", foaCalcHist, CV_HIST_ARRAY );
    trsRegArg( "cvCalcHist8uC1R",
               "Histogram calculation function algorithm test",
               "Algorithm", foaCalcHist, CV_HIST_TREE );

    trsRegArg( "cvCalcHistMask",
               "Histogram calculation function algorithm test",
               "Algorithm", foaCalcHistMask, CV_HIST_ARRAY );
    trsRegArg( "cvCalcHistMask8uC1R",
               "Histogram calculation function algorithm test",
               "Algorithm", foaCalcHistMask, CV_HIST_TREE );

    trsRegArg( "cvCalcBackProject",
               "Back project calculation function algorithm test",
               "Algorithm", foaBackProject, CV_HIST_ARRAY );
    trsRegArg( "cvCalcBackProject",
               "Back project calculation function algorithm test",
               "Algorithm", foaBackProject, CV_HIST_TREE );

    trsRegArg( "cvCalcBackProjectPatch",
               "Back project patch calculation function algorithm test",
               "Algorithm", foaBackProjectPatch, CV_HIST_ARRAY );
    trsRegArg( "cvCalcBackProjectPatch",
               "Back project patch calculation function algorithm test",
               "Algorithm", foaBackProjectPatch, CV_HIST_TREE );

    trsRegArg( "cvCalcBayesianProb",
               "Bayesian Prob calculation function algorithm test",
               "Algorithm", foaBayesianProb, CV_HIST_ARRAY );
    trsRegArg( "cvCalcBayesianProb",
               "Bayesian Prob calculation function algorithm test",
               "Algorithm", foaBayesianProb, CV_HIST_TREE );
}


/* End Of File */

⌨️ 快捷键说明

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