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

📄 ahistograms.cpp

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    IplImage* dst32f;
    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, "16", "Width of source picture" );
        trsiRead( &height, "16", "Height of source picture" );
    }

    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;
        hist2 = cvCreateHist( c_dims, d, type );
        hist3 = cvCreateHist( c_dims, d, type );

        for( i = 0; i < c_dims; i++ )
        {
            thresh[i] = (float*)icvAlloc( (hist2->dims[i] + 1) * sizeof(**hist2->thresh));
            threshe[i] = (float*)icvAlloc( 2 * sizeof(**hist2->thresh));
            thresh[i][0] = threshe[i][0] = 0.5;
            threshe[i][1] = thresh[i][hist2->dims[i]] = 0.5f + hist2->dims[i];
            for( j = 1; j < hist2->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;

        dst8u = cvCreateImage( cvSize(width, height), IPL_DEPTH_8U, 1 );
        dst8s = cvCreateImage( cvSize(width, height), IPL_DEPTH_8S, 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 * step32f / 4 + x] =
                        src8u[i]->imageData[y * step8s + x] =
                        src8s[i]->imageData[y * step8u + 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( hist2, d )) = (*cvGetHistValue_nD( hist3, d )) =
                (float)(fl * height);
            for( i = 0; d[i]++, d[i] >= dims && i < CV_HIST_MAX_DIM; i++ ) d[i] = 0;
        }

        /* Calculating back project 8u non uniform */
        cvCalcBackProject( src8u, dst8u, hist2 );
        /* Checking results */
        for( y = 0; y < height; y++ )
        {
            for( x = 0; x < width; x++ )
            {
                if( (x > dims || x < 1) && dst8u->imageData[y * step8u + x] ||
                    (x <= dims && x >= 1) && dst8u->imageData[y * step8u + x] != height )
                {
                    trsWrite( ATS_CON | ATS_LST, "Bad destination image 8u (non uniform) act: %d, exp: %d,"
                                                 " x=%d  y=%d\n",
                                                 (int)dst8u->imageData[y * step8u + x],
                                                 x > dims || x < 1 ? 0 : height,
                                                 x, y );
                    errors++;
                }
            }
        }

        /* Calculating back project 8u uniform */
        cvCalcBackProject( src8u, dst8u, hist3 );
        /* Checking results */
        for( y = 0; y < height; y++ )
        {
            for( x = 0; x < width; x++ )
            {
                if( (x > dims || x < 1) && dst8u->imageData[y * step8u + x] ||
                    (x <= dims && x >= 1) && dst8u->imageData[y * step8u + x] != height )
                {
                    trsWrite( ATS_CON | ATS_LST, "Bad destination image 8u (uniform) act: %d, exp: %d,"
                                                 " x=%d  y=%d\n",
                                                 (int)dst8u->imageData[y * step8u + x],
                                                 x > dims || x < 1 ? 0 : height,
                                                 x, y );
                    errors++;
                }
            }
        }

        /* Calculating back project 8s non uniform */
        cvCalcBackProject( src8s, dst8s, hist2 );
        /* Checking results */
        for( y = 0; y < height; y++ )
        {
            for( x = 0; x < width; x++ )
            {
                if( (x > dims || x < 1) && dst8s->imageData[y * step8s + x] ||
                    (x <= dims && x >= 1) && dst8s->imageData[y * step8s + x] != height )
                {
                    trsWrite( ATS_CON | ATS_LST, "Bad destination image 8s (non uniform) act: %d, exp: %d,"
                                                 " x=%d  y=%d\n",
                                                 (int)dst8s->imageData[y * step8s + x],
                                                 x > dims || x < 1 ? 0 : height,
                                                 x, y );
                    errors++;
                }
            }
        }

        /* Calculating back project 8s uniform */
        cvCalcBackProject( src8s, dst8s, hist3 );
        /* Checking results */
        for( y = 0; y < height; y++ )
        {
            for( x = 0; x < width; x++ )
            {
                if( (x > dims || x < 1) && dst8s->imageData[y * step8s + x] ||
                    (x <= dims && x >= 1) && dst8s->imageData[y * step8s + x] != height )
                {
                    trsWrite( ATS_CON | ATS_LST, "Bad destination image 8s (uniform) act: %d, exp: %d,"
                                                 " x=%d  y=%d\n",
                                                 (int)dst8s->imageData[y * step8s + x],
                                                 x > dims || x < 1 ? 0 : height,
                                                 x, y );
                    errors++;
                }
            }
        }

        /* Calculating back project 32f non uniform */
        cvCalcBackProject( src32f, dst32f, hist2 );
        /* Checking results */
        for( y = 0; y < height; y++ )
        {
            for( x = 0; x < width; x++ )
            {
                if( (x > dims || x < 1) && ((float*)(dst32f->imageData))[y * step32f / 4 + x] ||
                    (x <= dims && x >= 1) && ((float*)(dst32f->imageData))[y * step32f / 4 + x] != height )
                {
                    trsWrite( ATS_CON | ATS_LST, "Bad destination image 32f (non uniform) act: %d, exp: %d,"
                                                 " x=%d  y=%d\n",
                                                 (int)((float*)(dst32f->imageData))[y * step32f / 4 + x],
                                                 x > dims || x < 1 ? 0 : height,
                                                 x, y );
                    errors++;
                }
            }
        }

        /* Calculating back project 32f uniform */
        cvCalcBackProject( src32f, dst32f, hist3 );
        /* Checking results */
        for( y = 0; y < height; y++ )
        {
            for( x = 0; x < width; x++ )
            {
                if( (x > dims || x < 1) && ((float*)(dst32f->imageData))[y * step32f / 4 + x] ||
                    (x <= dims && x >= 1) && ((float*)(dst32f->imageData))[y * step32f / 4 + x] != height )
                {
                    trsWrite( ATS_CON | ATS_LST, "Bad destination image 32f (uniform) act: %d, exp: %d,"
                                                 " x=%d  y=%d\n",
                                                 (int)((float*)(dst32f->imageData))[y * step32f / 4 + x],
                                                 x > dims || x < 1 ? 0 : height,
                                                 x, y );
                    errors++;
                }
            }
        }

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

        cvReleaseImage( &dst8u );
        cvReleaseImage( &dst8s );
        cvReleaseImage( &dst32f );

        cvReleaseHist( &hist2 );
        cvReleaseHist( &hist3 );

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


static int myBackProjectPatch(IplImage** src8u, IplImage** src8s, IplImage** src32f,
                              CvHistogram* hist, IplImage* _dst8u, IplImage* _dst8s, IplImage* _dst32f,
                              float norm_factor, CvCompareMethod method,
                              CvSize range)
{
    CvSize roi = cvSize(src8u[0]->width, src8u[0]->height);
    int    step = (((roi.width + range.width * 2 + 1) * 4) | 15) + 1;

    uchar* _test8u[CV_HIST_MAX_DIM];
    char*  _test8s[CV_HIST_MAX_DIM];
    float* _test32f[CV_HIST_MAX_DIM];
    uchar* test8u[CV_HIST_MAX_DIM];
    char*  test8s[CV_HIST_MAX_DIM];
    float* test32f[CV_HIST_MAX_DIM];

    IplImage** img8u;
    IplImage** img8s;
    IplImage** img32f;

    float* dst8u = (float*)_dst8u->imageData;
    float* dst8s = (float*)_dst8s->imageData;
    float* dst32f = (float*)_dst32f->imageData;

    int    dst_step = _dst8u->widthStep;

    int i, x, y;
    CvHistogram* model = 0;

    img8u = (IplImage**)icvAlloc( hist->c_dims * sizeof(img8u[0]));
    img8s = (IplImage**)icvAlloc( hist->c_dims * sizeof(img8s[0]));
    img32f= (IplImage**)icvAlloc( hist->c_dims * sizeof(img32f[0]));

    for(i = 0; i < hist->c_dims; i++)
    {
        CvSize img_size = cvSize(range.width * 2 + 1, range.height * 2 + 1);
        
        _test8u[i] = (uchar*)icvAlloc(step * (roi.height + range.height * 2 + 1));
        _test8s[i] = (char*)icvAlloc(step * (roi.height + range.height * 2 + 1));
        _test32f[i] = (float*)icvAlloc(step * (roi.height + range.height * 2 + 1));

        memset(_test8u[i], 0, step * (roi.height + range.height * 2 + 1));
        memset(_test8s[i], 0, step * (roi.height + range.height * 2 + 1));
        memset(_test32f[i], 0, step * (roi.height + range.height * 2 + 1));

        img8u[i] = cvCreateImageHeader( img_size, IPL_DEPTH_8U, 1 );
        img8s[i] = cvCreateImageHeader( img_size, IPL_DEPTH_8S, 1 );
        img32f[i] = cvCreateImageHeader( img_size, IPL_DEPTH_32F, 1 );


    }

    for(i = 0; i < hist->c_dims; i++)
    {
        for(y = 0; y < roi.height; y++)
            for(x = 0; x < roi.width; x++)
            {
                _test8u[i][(y + range.height) * step + x + range.width] =
                    src8u[i]->imageData[y * src8u[i]->widthStep + x];

                _test8s[i][(y + range.height) * step + x + range.width] =
                    src8s[i]->imageData[y * src8s[i]->widthStep + x];

                _test32f[i][(y + range.height) * step / 4 + x + range.width] =
                    ((float*)(src32f[i]->imageData))[y * src32f[i]->widthStep / 4 + x];
            }
        test8u[i] = _test8u[i];
        test8s[i] = _test8s[i];
        test32f[i] = _test32f[i];
    }

    cvCopyHist(hist, &model);

    for(y = 0; y < roi.height; y++, dst8u += dst_step / 4, dst8s += dst_step / 4,
                                    dst32f += dst_step / 4)
    {
        for(x = 0; x < roi.width; x++)
        {
            for(i = 0; i < hist->c_dims; i++)
            {
                cvSetImageData( img8u[i], test8u[i], step );
                cvSetImageData( img8s[i], test8s[i], step );
                cvSetImageData( img32f[i], test32f[i], step );
            }
            
            cvCalcHist( img8u, model, 0);
            cvNormalizeHist(model, norm_factor);
            dst8u[x] = (float)cvCompareHist(hist, model, method);
            cvCalcHist( img8s, model, 0);
            cvNormalizeHist(model, norm_factor);
            dst8s[x] = (float)cvCompareHist(hist, model, method);
            cvCalcHist( img32f, model, 0);
            cvNormalizeHist(model, norm_factor);
            dst32f[x] = (float)cvCompareHist(hist, model, method);

            for(i = 0; i < hist->c_dims; i++)
            {
                test8u[i]++; test8s[i]++; test32f[i]++;
            }
        }
        for(i = 0; i < hist->c_dims; i++)
        {
            test8u[i] += step - roi.width;
            test8s[i] += step - roi.width;
            test32f[i] += step / 4 - roi.width;
        }
    }
    cvReleaseHist(&model);
    for(i = 0; i < hist->c_dims; i++)
    {
        icvFree(&_test8u[i]);
        icvFree(&_test8s[i]);
        icvFree(&_test32f[i]);
        cvReleaseImageHeader( img8u + i );
        cvReleaseImageHeader( img8s + i );
        cvReleaseImageHeader( img32f + i );
    }

    icvFree( &img8u );

⌨️ 快捷键说明

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