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

📄 atemplmatch.cpp

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

#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <float.h>

#include "CvTest.h"

static char* funcs[] =
{
    "cvMatchTemplate"
};

static char *test_desc = "Test for template matching functions";

/* actual parameters */
static int min_img_size, max_img_size;
static int img_size_delta_type, img_size_delta;
static int min_templ_size;
static int templ_size_delta_type, templ_size_delta;
static int base_iters;

/* which tests have to run */
static int dt_l = 0, dt_h = 2,
           meth_l = 0, meth_h = 5;

static int init_templ_match_params = 0;

static const int img8u_range = 255;
static const int img8s_range = 128;
static const float img32f_range = 100.f;
static const int img32f_bits = 23;

static void read_templ_match_params( void )
{
    if( !init_templ_match_params )
    {
        int  data_types, method;

        /* Determine which tests are needed to run */
        trsCaseRead( &data_types,"/a/8u/8s/32f", "a",
            "a - all, 8u - unsigned char, 8s - signed char, 32f - float" );
        if( data_types != 0 ) dt_l = dt_h = data_types - 1;

        trsCaseRead( &method,"/a/sd/sdn/cr/crn/cf/cfn", "a",
            "\n\ta   - all"
            "\n\tsd  - squared diff"
            "\n\tsdn - normed squared diff"
            "\n\tcr  - cross correlation"
            "\n\tcrn - normed cross correlation"
            "\n\tcf  - correlation coefficient"
            "\n\tcrn - normed correlation coefficient");
        if( method != 0 ) meth_l = meth_h = method - 1;

        /* read tests params */
        trsiRead( &min_img_size, "1", "Minimal width or height of image" );
        trsiRead( &max_img_size, "32", "Maximal width or height of image" );
        trsCaseRead( &img_size_delta_type,"/a/m", "m", "a - add, m - multiply" );
        trsiRead( &img_size_delta, "3", "Image size step(factor)" );
        trsiRead( &min_templ_size, "1", "Minimal width or height of image" );
        trsCaseRead( &templ_size_delta_type,"/a/m", "a", "a - add, m - multiply" );
        trsiRead( &templ_size_delta, "3", "Image size step(factor)" );
        trsiRead( &base_iters, "1000", "Base number of iterations" );

        init_templ_match_params = 1;
    }
}


static double calc_mean( IplImage* src )
{
    double mean;
    atsCalcImageStatistics( src, 0, 0, 0, 0, 0,
                            0, 0, &mean, 0, 0, 0, 0, 0 );
    return mean;
}


static void match_templ_etalon( IplImage* src, IplImage* templ, IplImage* dst,
                                CvTemplMatchMethod method )
{
    float*   dst_data = 0;
    int      dst_step = 0;
    int      depth = 0;
    int      i, j;
    CvSize  src_sz, templ_sz, dst_sz;
    IplROI*  src_roi = src->roi;
    IplROI   tsrc_roi;

    int      is_normed, is_centered;
    double   nrmT = 1, deltaT = 0;

    atsGetImageInfo( src,   0, 0, &src_sz, &depth, 0, 0 );
    atsGetImageInfo( templ, 0, 0, &templ_sz, 0, 0, 0 );
    atsGetImageInfo( dst, (void**)&dst_data, &dst_step, &dst_sz, 0, 0, 0 );

    assert( depth == IPL_DEPTH_32F );
    assert( templ_sz.width <= src_sz.width &&
            templ_sz.height <= src_sz.height &&
            dst_sz.width == src_sz.width - templ_sz.width + 1 &&
            dst_sz.height == src_sz.height - templ_sz.height + 1 );

    src->roi = &tsrc_roi;
    tsrc_roi.coi = 0;
    tsrc_roi.width = templ_sz.width;
    tsrc_roi.height = templ_sz.height;

    is_normed = method == CV_TM_SQDIFF_NORMED ||
                method == CV_TM_CCORR_NORMED ||
                method == CV_TM_CCOEFF_NORMED;

    is_centered = method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED;

    if( is_centered ) deltaT = calc_mean( templ );
    if( is_normed ) nrmT = atsCrossCorr( templ, templ, deltaT, deltaT );
    dst_step /= 4;

    for( i = 0; i < dst_sz.height; i++ )
    {
        for( j = 0; j < dst_sz.width; j++ )
        {
            double val = 0, nrmS = 1, deltaS = 0;

            tsrc_roi.xOffset = j;
            tsrc_roi.yOffset = i;

            if( is_centered ) deltaS = calc_mean( src );
            if( is_normed ) nrmS = atsCrossCorr( src, src, deltaS, deltaS );
            
            switch( method )
            {
            case CV_TM_SQDIFF_NORMED:
            case CV_TM_SQDIFF:
                val = iplNorm( src, templ, IPL_L2 );
                val *= val;
                break;
            default:
                val = atsCrossCorr( src, templ, deltaS, deltaT );
            }

            if( is_normed )
            {
                val /= (sqrt(nrmS + FLT_EPSILON)*sqrt(nrmT + FLT_EPSILON));
            }
            dst_data[i*dst_step + j] = (float)val;
        }
    }

    src->roi = src_roi;
}


/* ///////////////////// match_templ_test ///////////////////////// */

static int match_templ_test( void* arg )
{
    const double success_error_level = 1e-6;
    const double error_level_scale = 1e-3;

    int   param     = (int)arg;
    int   depth     = param/6;
    int   method    = param%6;

    int   seed = atsGetSeed();

    /* position where the maximum error occured */
    int   merr_w = 0, merr_h = 0, merr_tw = 0, merr_th = 0, merr_iter = 0;

    /* test parameters */
    int     w = 0, h = 0, tw = 0, th = 0, i = 0;
    double  max_err = 0.;
    //int     code = TRS_OK;

    IplROI       src_roi, templ_roi, dst_roi;
    IplImage    *src_img, *templ_img, *dst_img, *dst2_img;

⌨️ 快捷键说明

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