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

📄 atemplmatch.cpp

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    IplImage    *srcfl_img = 0, *templfl_img = 0;
    AtsRandState rng_state;

    atsRandInit( &rng_state, 0, 1, seed );

    read_templ_match_params();

    if( !(ATS_RANGE( depth, dt_l, dt_h+1 ) &&
          ATS_RANGE( method, meth_l, meth_h+1 ))) return TRS_UNDEF;

    depth = depth == 2 ? IPL_DEPTH_32F : depth == 1 ? IPL_DEPTH_8S : IPL_DEPTH_8U;

    src_img = atsCreateImage( max_img_size, max_img_size, depth, 1, 0 );
    templ_img = atsCreateImage( max_img_size, max_img_size, depth, 1, 0 );
    dst_img = atsCreateImage( max_img_size, max_img_size, IPL_DEPTH_32F, 1, 0 );
    dst2_img = atsCreateImage( max_img_size, max_img_size, IPL_DEPTH_32F, 1, 0 );

    if( depth != IPL_DEPTH_32F )
    {
        srcfl_img = atsCreateImage( max_img_size, max_img_size, IPL_DEPTH_32F, 1, 0 );
        templfl_img = atsCreateImage( max_img_size, max_img_size, IPL_DEPTH_32F, 1, 0 );
    }
    else
    {
        srcfl_img = src_img;
        templfl_img = templ_img;
    }
    
    src_img->roi = srcfl_img->roi = &src_roi;
    templ_img->roi = templfl_img->roi = &templ_roi;
    dst_img->roi = dst2_img->roi = &dst_roi;

    src_roi.coi = dst_roi.coi = templ_roi.coi = 0;

    src_roi.xOffset = src_roi.yOffset =
    templ_roi.xOffset = templ_roi.yOffset =     
    dst_roi.xOffset = dst_roi.yOffset = 0;

    switch( depth )
    {
    case IPL_DEPTH_8U:
        atsRandSetBounds( &rng_state, 0, img8u_range );
        break;
    case IPL_DEPTH_8S:
        atsRandSetBounds( &rng_state, -img8s_range, img8s_range );
        break;
    case IPL_DEPTH_32F:
        atsRandSetBounds( &rng_state, -img32f_range, img32f_range );
        atsRandSetFloatBits( &rng_state, img32f_bits );
        break;
    }

    for( h = min_img_size; h <= max_img_size; )
    {
        for( w = min_img_size; w <= max_img_size; )
        {
            int th_limit = h < 50 ? h : h/2;
            int tw_limit = w < 50 ? w : w/2;

            src_roi.width = w;
            src_roi.height = h;

            atsFillRandomImageEx( src_img, &rng_state );
            
            for( th = min_templ_size; th <= th_limit; )
            {
                for( tw = min_templ_size; tw <= tw_limit; )
                {
                    int  denom = (w - min_img_size + 1)*(h - min_img_size + 1)*
                                 (tw - min_templ_size+1)*(th - min_templ_size+1);
                    int  iters = (base_iters*2 + denom)/(2*denom);

                    templ_roi.width = tw;
                    templ_roi.height = th;

                    dst_roi.width = w - tw + 1;
                    dst_roi.height = h - th + 1;

                    if( iters < 1 ) iters = 1;

                    for( i = 0; i < iters; i++ )
                    {
                        double err;
                        atsFillRandomImageEx( templ_img, &rng_state );

                        if( depth != IPL_DEPTH_32F )
                        {
                            atsConvert( src_img, srcfl_img );
                            atsConvert( templ_img, templfl_img );
                        }

                        match_templ_etalon( srcfl_img, templfl_img, dst2_img,
                                            (CvTemplMatchMethod)method );

                        /* call the library function */
                        cvMatchTemplate( src_img, templ_img, dst_img,
                                         (CvTemplMatchMethod)method );

                        err = iplNorm( dst_img, dst2_img, IPL_C );

                        if( method == 5 && tw == 1 && th == 1 )
                        {
                            err *= error_level_scale;
                        }

                        if( err > max_err )
                        {
                            merr_w    = w;
                            merr_h    = h;
                            merr_tw   = tw;
                            merr_th   = th;
                            merr_iter = i;
                            max_err   = err;
                            if( max_err > success_error_level ) goto test_exit;
                        }
                    }
                    ATS_INCREASE( tw, templ_size_delta_type, templ_size_delta );
                }
                ATS_INCREASE( th, templ_size_delta_type, templ_size_delta );
            }
            ATS_INCREASE( w, img_size_delta_type, img_size_delta );
        } /* end of the loop by w */
        ATS_INCREASE( h, img_size_delta_type, img_size_delta );
    }  /* end of the loop by h */

test_exit:

    dst_img->roi = dst2_img->roi = 0;
    atsReleaseImage( dst_img );
    
    src_img->roi = srcfl_img->roi = 0;
    atsReleaseImage( src_img );

    templ_img->roi = templfl_img->roi = 0;
    atsReleaseImage( templ_img );

    if( depth != IPL_DEPTH_32F )
    {
        atsReleaseImage( srcfl_img );
        atsReleaseImage( templfl_img );
    }

    //if( code == TRS_OK )
    {
        trsWrite( ATS_LST, "Max err is %g at w = %d, h = %d, "
                           "tw = %d, th = %d, iter = %d, seed = %08x",
                           max_err, merr_w, merr_h, merr_tw, merr_th,
                           merr_iter, seed );

        return max_err <= success_error_level ?
            trsResult( TRS_OK, "No errors" ) :
            trsResult( TRS_FAIL, "Bad accuracy" );
    }
    /*else
    {
        trsWrite( ATS_LST, "Fatal error at w = %d, h = %d, "
                           "tw = %d, th = %d, iter = %d, seed = %08x",
                           w, h, tw, th, i, seed );
        return trsResult( TRS_FAIL, "Function returns error code" );
    }*/
}


#define MTEMPL_SQDIFF_8U          0
#define MTEMPL_SQDIFF_NORMED_8U   1
#define MTEMPL_CCORR_8U           2
#define MTEMPL_CCORR_NORMED_8U    3
#define MTEMPL_CCOEFF_8U          4
#define MTEMPL_CCOEFF_NORMED_8U   5

#define MTEMPL_SQDIFF_8S          6
#define MTEMPL_SQDIFF_NORMED_8S   7
#define MTEMPL_CCORR_8S           8
#define MTEMPL_CCORR_NORMED_8S    9
#define MTEMPL_CCOEFF_8S         10
#define MTEMPL_CCOEFF_NORMED_8S  11

#define MTEMPL_SQDIFF_32F        12
#define MTEMPL_SQDIFF_NORMED_32F 13
#define MTEMPL_CCORR_32F         14
#define MTEMPL_CCORR_NORMED_32F  15
#define MTEMPL_CCOEFF_32F        16
#define MTEMPL_CCOEFF_NORMED_32F 17


void InitAMatchTemplate( void )
{
    /* Registering test functions */
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_SQDIFF_8U );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_SQDIFF_NORMED_8U );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCORR_8U );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCORR_NORMED_8U );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCOEFF_8U );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCOEFF_NORMED_8U );

    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_SQDIFF_8S );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_SQDIFF_NORMED_8S );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCORR_8S );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCORR_NORMED_8S );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCOEFF_8S );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCOEFF_NORMED_8S );

    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_SQDIFF_32F );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_SQDIFF_NORMED_32F );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCORR_32F );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCORR_NORMED_32F );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCOEFF_32F );
    trsRegArg( funcs[0], test_desc, atsAlgoClass, match_templ_test, MTEMPL_CCOEFF_NORMED_32F );
} /* InitAMatchTemplate */


/* End of file. */

⌨️ 快捷键说明

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