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

📄 ahistograms.cpp

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        count = 0;
        sum = 0;
        
        /* Filling dimension's array */
        for( i = 0; i <= CV_HIST_MAX_DIM; i++ ) d[i] = dims;
        
        /*Creating histogram*/
        hist1 = cvCreateHist( c_dims, d, type );
        hist2 = cvCreateHist( c_dims, d, type );
        
        /*Initializing histogram*/

        for( i = 0; i < CV_HIST_MAX_DIM; i++ ) d[i] = 0;
        for( i = 0; i < c_dims; )
        {
            v = (float)atsInitRandom( 0, 100 );
            *cvGetHistValue_nD( hist1, d ) = v;
            *cvGetHistValue_nD( hist2, d ) = v;
            count++;
            sum += v;
            for( i = 0; d[i]++, d[i] >= dims && i < CV_HIST_MAX_DIM; i++ ) d[i] = 0;
        }

        /*Checking values*/
        cvThreshHist( hist1, thresh );
        for( i = 0; i < c_dims; i++ ) d[i] = 0;
        for( i = 0; i < c_dims; )
        {
            if( cvQueryHistValue_nD( hist1, d ) != 0 && cvQueryHistValue_nD( hist2, d ) < thresh ||
                cvQueryHistValue_nD( hist1, d ) != cvQueryHistValue_nD( hist2, d ) &&
                cvQueryHistValue_nD( hist2, d ) >= thresh )
            {
                trsWrite( ATS_CON | ATS_LST, "Error: act %f   exp %f address  ",
                          cvQueryHistValue_nD( hist1, d ),
                          cvQueryHistValue_nD( hist2, d ) >= thresh ?
                          cvQueryHistValue_nD( hist2, d ) : 0.0f );
                for( j = 0; j < c_dims; j++ ) trsWrite(ATS_CON | ATS_LST, "%d  ", d[j] );
                trsWrite(ATS_CON | ATS_LST, "\n");
                errors++;
            }
            for( i = 0; d[i]++, d[i] >= dims && i < CV_HIST_MAX_DIM; i++ ) d[i] = 0;
        }

        cvReleaseHist( &hist1 );
        cvReleaseHist( &hist2 );

    }

    return errors ? trsResult( TRS_FAIL, "Fixed %d errors", errors ) : TRS_OK;
}


static int foaHistCompare(void* _type)
{
    CvHistType type = (CvHistType)(int)_type;
    static int c_dimss;
    static int dims;
    static int init = 0;
    static float thresh;

    CvHistogram* hist1;
    CvHistogram* hist2;

    int c_dims;
    int d[CV_HIST_MAX_DIM + 1];
    double intersect, exp_intersect;
    double correl, exp_correl;
    double chisqr, exp_chisqr;
    double m1, m2, m3, mn1, mn2;
    int size;
    int i;
    
    int errors = 0;

    if( !init )
    {
        init = 1;
        trsiRead( &c_dimss, "6", "Number of dimensions" );
        trsiRead( &dims, "10", "Size of every dimension" );
        trssRead( &thresh, "0.0001", "Max error value" );
    }

    for( c_dims = 1; c_dims <= c_dimss; c_dims++ )
    {
        /*Creating histograms*/
        for( i = 0; i < c_dims; i++ ) d[i] = dims;
        hist1 = cvCreateHist( c_dims, d, type );
        hist2 = cvCreateHist( c_dims, d, type );

        /*Filling histograms*/
        /*hist1: y = x / size*/
        /*hist2: y = 1 - x / size*/
        size = hist1->mdims[0] * hist1->dims[0];
        for( i = 0, mn1 = mn2 = 0; i < size; i++ )
        {
            *cvGetHistValue_1D(hist1, i) = (float)i / (size - 1);
            *cvGetHistValue_1D(hist2, i) = 1 - (float)i / (size - 1);
            mn1 += cvQueryHistValue_1D(hist1, i);
            mn2 += cvQueryHistValue_1D(hist2, i);
        }

        mn1 /= size;
        mn2 /= size;

        intersect = cvCompareHist( hist1, hist2, CV_COMP_INTERSECT );
        correl = cvCompareHist( hist1, hist2, CV_COMP_CORREL );
        chisqr = cvCompareHist( hist1, hist2, CV_COMP_CHISQR );

        for( i = 0, exp_intersect = 0, exp_chisqr = 0,
             m1 = m2 = m3 = 0; i < size; i++ )
        {
            exp_intersect += MIN( cvQueryHistValue_1D(hist1, i), cvQueryHistValue_1D(hist2, i) );
            exp_chisqr += (cvQueryHistValue_1D(hist1, i) - cvQueryHistValue_1D(hist2, i)) *
                          (cvQueryHistValue_1D(hist1, i) - cvQueryHistValue_1D(hist2, i)) /
                          (cvQueryHistValue_1D(hist1, i) + cvQueryHistValue_1D(hist2, i));
            m1 += (cvQueryHistValue_1D(hist1, i) - mn1) * (cvQueryHistValue_1D(hist2, i) - mn2);
            m2 += (cvQueryHistValue_1D(hist2, i) - mn2) * (cvQueryHistValue_1D(hist2, i) - mn2);
            m3 += (cvQueryHistValue_1D(hist1, i) - mn1) * (cvQueryHistValue_1D(hist1, i) - mn1);
        }

        exp_correl = m1 / sqrt( m2 * m3 );

        if( fabs( intersect - exp_intersect ) > thresh * exp_intersect )
        {
            trsWrite( ATS_CON | ATS_LST, "Intersection error: act %f   exp %f\n",
                      (float)intersect, (float)exp_intersect );
            trsWrite( ATS_CON | ATS_LST, "Size% " );
            for( i = 0; i < c_dims; i++ ) trsWrite( ATS_CON | ATS_LST, "%d  ", d[i] );
            trsWrite( ATS_CON | ATS_LST, "\n" );
            errors++;
        }

        if( fabs( correl - exp_correl ) > thresh )
        {
            trsWrite( ATS_CON | ATS_LST, "Correlation error: act %f   exp %f\n",
                      (float)correl, (float)exp_correl );
            trsWrite( ATS_CON | ATS_LST, "Size% " );
            for( i = 0; i < c_dims; i++ ) trsWrite( ATS_CON | ATS_LST, "%d  ", d[i] );
            trsWrite( ATS_CON | ATS_LST, "\n" );
            errors++;
        }

        if( fabs( chisqr - exp_chisqr ) > thresh )
        {
            trsWrite( ATS_CON | ATS_LST, "ChiSqr error: act %f   exp %f\n",
                      (float)chisqr, (float)exp_chisqr );
            trsWrite( ATS_CON | ATS_LST, "Size% " );
            for( i = 0; i < c_dims; i++ ) trsWrite( ATS_CON | ATS_LST, "%d  ", d[i] );
            trsWrite( ATS_CON | ATS_LST, "\n" );
            errors++;
        }

        cvReleaseHist( &hist1 );
        cvReleaseHist( &hist2 );
    }

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


static int foaCopyHist(void* _type)
{
    CvHistType type = (CvHistType)(int)_type;
    static int c_dimss;
    static int dims;
    static int init = 0;
    static float thresh;

    CvHistogram* hist1;
    CvHistogram* hist2;

    int c_dims;
    int d[CV_HIST_MAX_DIM + 1];
    int size;
    int i;
    
    int errors = 0;

    if( !init )
    {
        init = 1;
        trsiRead( &c_dimss, "6", "Number of dimensions" );
        trsiRead( &dims, "10", "Size of every dimension" );
        trssRead( &thresh, "0.00001", "Max error value" );
    }

    for( c_dims = 1; c_dims <= c_dimss; c_dims++ )
    {
        /*Creating histograms*/
        for( i = 0; i < c_dims; i++ ) d[i] = dims;
        hist1 = cvCreateHist( c_dims, d, type );
        hist2 = 0;

        /*Filling histograms*/
        /*hist1: y = x / size*/
        size = hist1->mdims[0] * hist1->dims[0];
        for( i = 0; i < size; i++ )
            *cvGetHistValue_1D(hist1, i) = (float)i / (size - 1);
        cvCopyHist( hist1, &hist2 );

        if( cvCompareHist( hist1, hist2, CV_COMP_CHISQR ) != 0 )
        {
            trsWrite( ATS_CON | ATS_LST, "Intersection error: act %f   exp 0 - "
                                         "histograms not equals\n",
                      (float)cvCompareHist( hist1, hist2, CV_COMP_CHISQR ) );
            trsWrite( ATS_CON | ATS_LST, "Size% " );
            for( i = 0; i < c_dims; i++ ) trsWrite( ATS_CON | ATS_LST, "%d  ", d[i] );
            trsWrite( ATS_CON | ATS_LST, "\n" );
            errors++;
        }

        cvReleaseHist( &hist1 );
        cvReleaseHist( &hist2 );
    }

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


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

    CvHistogram* hist1;
    CvHistogram* hist2;
    CvHistogram* hist3;

    int    c_dims;
    int    d[CV_HIST_MAX_DIM + 1];
    float* thresh[CV_HIST_MAX_DIM];
    float* threshe[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];
    int    step8u, step8s, step32f;
    
    int errors = 0;

    if( !init )
    {
        init = 1;
        trsiRead( &c_dimss, "6", "Number of dimensions" );
        trsiRead( &dims, "10", "Size of every dimension" );
        trsiRead( &width, "160", "Width of source picture" );
        trsiRead( &height, "160", "Height of source picture" );
    }

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

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

        cvSetHistThresh( hist2, thresh, 0 );
        cvSetHistThresh( hist3, threshe, 1 );

        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);
        }
        
        step8u = src8u[0]->widthStep;
        step8s = src8s[0]->widthStep;
        step32f = src32f[0]->widthStep;

        for( i = 0; i < c_dims; i++ )
            for( y = 0; y < height; y++ )
                for( x = 0; x < width; x++ )
                {
                    ((float*)(src32f[i]->imageData))[y * step32f / 4 + x] =
                        src8u[i]->imageData[y * step8s + x] =
                        src8s[i]->imageData[y * step8u + x] = (char)x;
                }

        /* Filling control histogram */
        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( hist1, d )) = (float)(fl * height);
            for( i = 0; d[i]++, d[i] >= dims && i < CV_HIST_MAX_DIM; i++ ) d[i] = 0;
        }

        /* Clearing histograms */
        if(type == CV_HIST_ARRAY)
        {
            memset(hist2->array, 1, hist2->dims[0] * hist2->mdims[0] * sizeof(*hist2->array));
            memset(hist3->array, 1, hist2->dims[0] * hist2->mdims[0] * sizeof(*hist2->array));
        }

        /* Calculating histograms 8u */
        cvCalcHist( src8u, hist2, 0 );
        cvCalcHist( src8u, hist3, 0 );
        /* Checking histogram */
        if( cvCompareHist( hist1, hist2, CV_COMP_CHISQR ) != 0 )
        {
            trsWrite( ATS_CON | ATS_LST, "Non uniform 8u calc, Intersection error: act %f   exp 0 - "
                                         "histograms not equals\n",
                      (float)cvCompareHist( hist1, hist2, CV_COMP_CHISQR ) );
            trsWrite( ATS_CON | ATS_LST, "Size " );
            for( i = 0; i < c_dims; i++ )
                trsWrite( ATS_CON | ATS_LST, "%d  ", hist1->dims[i] );
            trsWrite( ATS_CON | ATS_LST, "\n" );
            errors++;
        }
        if( cvCompareHist( hist1, hist3, CV_COMP_CHISQR ) != 0 )
        {

⌨️ 快捷键说明

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