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

📄 aimagestatistics.cpp

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*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[] =
{
    "cvCountNonZero",
    "cvSumPixels",
    "cvMinMaxLoc, cvMinMaxLocMask",
    "cvMean, cvMeanMask",
    "cvMean_StdDev, cvMean_StdDevMask",
};

static char *test_desc = "Comparing with the simple algorithm";
static const double ATS_SUCCESS_ERROR_LEVEL_FLT = 3e-5;

#define IMGSTAT_COUNT_NON_ZERO   0
#define IMGSTAT_SUM_PIXELS       1
#define IMGSTAT_MIN_MAX_LOC      2
#define IMGSTAT_MEAN             3
#define IMGSTAT_MEAN_STDDEV      4

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

/* which tests have to run */
static int fn_l = 0, fn_h = ATS_DIM(funcs)-1,
           dt_l = 0, dt_h = 2,
           ch_l = 0, ch_h = 1;

static int init_img_stat_params = 0;

static const int mask_depth = IPL_DEPTH_8U;
static const int img8u_range = 255;
static const int img8s_range = 128;
static const float img32f_range = 1000.f;
static const int img32f_bits = 23;
static const int mask_mask = 0x80;

static double rel_err( double a, double b )
{
    return fabs(a - b)/(fabs(a) + 1e-10);
}

static double rel_err2( double a, double b, double abs_a )
{
    return fabs(a - b)/(abs_a + 1e-10);
}


static void read_img_stat_params( void )
{
    if( !init_img_stat_params )
    {
        int func, data_types, channels;

        /* Determine which tests are needed to run */
        trsCaseRead( &func, "/a/cnz/sum/mml/mn/md", "a",
                     "Function type: \n"
                     "a - all\n"
                     "cnz - CountNonZero\n"
                     "sum - SumPixels\n"
                     "mml - MinMaxLoc\n"
                     "mn  - Mean\n"
                     "md  - Mean_StdDev\n" );
        if( func != 0 ) fn_l = fn_h = func - 1;

        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( &channels, "/a/1/3", "a", "a - all, 1 - single channel, 3 - three channels" );
        if( channels != 0 ) ch_l = ch_h = channels - 1;

        /* read tests params */
        trsiRead( &min_img_size, "1", "Minimal width or height of image" );
        trsiRead( &max_img_size, "1000", "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( &base_iters, "1000", "Base number of iterations" );

        init_img_stat_params = 1;
    }
}


/* ///////////////////// count_non_zero_test ///////////////////////// */

static int count_non_zero_test( void* arg )
{
    const int success_error_level = 0;
    static const int img8u_range = 3;
    static const int img8s_range = 3;
    static const float img32f_range = 10.f;
    static const int img32f_bits = 4;

    int   param     = (int)arg;
    int   depth     = param/2;
    int   channels  = (param & 1);

    int   seed      = atsGetSeed();

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

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

    IplROI       roi;
    IplImage    *img, *img2;
    AtsRandState rng_state;

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

    read_img_stat_params();

    if( !(ATS_RANGE( IMGSTAT_COUNT_NON_ZERO, fn_l, fn_h+1 ) &&
          ATS_RANGE( depth, dt_l, dt_h+1 ) &&
          ATS_RANGE( channels, ch_l, ch_h+1 ))) return TRS_UNDEF;

    depth = depth == 2 ? IPL_DEPTH_32F : depth == 1 ? IPL_DEPTH_8S : IPL_DEPTH_8U;
    channels = channels*2 + 1;

    img  = atsCreateImage( max_img_size, max_img_size, depth, channels, 0 );
    img2 = atsCreateImage( max_img_size, max_img_size, IPL_DEPTH_32F, channels, 0 );

    roi.coi = 0;
    roi.xOffset = roi.yOffset = 0;

    img->roi = img2->roi = &roi;

    for( h = min_img_size; h <= max_img_size; )
    {
        for( w = min_img_size; w <= max_img_size; )
        {
            int  denom = (w - min_img_size + 1)*(h - min_img_size + 1)*channels;
            int  iters = (base_iters*2 + denom)/(2*denom);

            roi.width = w;
            roi.height = h;

            if( iters < 1 ) iters = 1;

            for( i = 0; i < iters; i++ )
            {
                int etalon_result[3];

                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;
                }

                roi.coi = 0;
                atsFillRandomImageEx( img, &rng_state );

                if( depth != IPL_DEPTH_32F ) atsConvert( img, img2 );

                for( c = 1; c <= channels; c++ )
                {
                    roi.coi = c;
                    atsCalcImageStatistics(
                        depth == IPL_DEPTH_32F ? img : img2, 0,
                        0, 0, /* min & max vals */
                        0, 0, /* min & max locs */
                        &etalon_result[c-1], /* non_zero */
                        0, /* sum */
                        0, 0, /* mean & stddev */
                        0, 0, 0, 0 ); /* c_norm, l1_norm, l2_norm */
                }

                for( c = 1; c <= channels; c++ )
                {
                    double err0;
                    int result;

                    roi.coi = c;

                    /* //// ROI version /////// */
                    result = cvCountNonZero( img );
                    err0 = fabs( result - etalon_result[c-1] );

                    if( err0 > max_err )
                    {
                        merr_w    = w;
                        merr_h    = h;
                        merr_iter = i;
                        merr_c    = c;
                        max_err   = err0;
                        if( max_err > success_error_level ) goto test_exit;
                    }
                }
            }
            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:

    img->roi = img2->roi = 0;

    atsReleaseImage( img );
    atsReleaseImage( img2 );

    //if( code == TRS_OK )
    {
        trsWrite( ATS_LST, "Max err is %g at w = %d, h = %d, "
                           "iter = %d, c = %d, seed = %08x",
                           max_err, merr_w, merr_h, merr_iter, merr_c, 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, "
                           "iter = %d, c = %d, seed = %08x",
                           w, h, i, c, seed );
        return trsResult( TRS_FAIL, "Function returns error code" );
    }*/
}


/* ///////////////////// sum_pixels_test ///////////////////////// */

static int sum_pixels_test( void* arg )
{
    double success_error_level = 0;

    int   param     = (int)arg;
    int   depth     = param/2;
    int   channels  = (param & 1);

    int   seed     = atsGetSeed();

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

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

    IplROI       roi;
    IplImage    *img, *img2;
    AtsRandState rng_state;

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

    read_img_stat_params();

    if( !(ATS_RANGE( IMGSTAT_SUM_PIXELS, fn_l, fn_h+1 ) &&
          ATS_RANGE( depth, dt_l, dt_h+1 ) &&
          ATS_RANGE( channels, ch_l, ch_h+1 ))) return TRS_UNDEF;

    depth = depth == 2 ? IPL_DEPTH_32F : depth == 1 ? IPL_DEPTH_8S : IPL_DEPTH_8U;
    channels = channels*2 + 1;

    img  = atsCreateImage( max_img_size, max_img_size, depth, channels, 0 );
    img2 = atsCreateImage( max_img_size, max_img_size, IPL_DEPTH_32F, channels, 0 );

    roi.coi = 0;
    roi.xOffset = roi.yOffset = 0;

    img->roi = img2->roi = &roi;

    if( depth == IPL_DEPTH_32F ) success_error_level = ATS_SUCCESS_ERROR_LEVEL_FLT;

    for( h = min_img_size; h <= max_img_size; )
    {
        for( w = min_img_size; w <= max_img_size; )
        {
            int  denom = (w - min_img_size + 1)*(h - min_img_size + 1)*channels;
            int  iters = (base_iters*2 + denom)/(2*denom);

            roi.width = w;
            roi.height = h;

            if( iters < 1 ) iters = 1;

            for( i = 0; i < iters; i++ )
            {
                double  etalon_result[3];
                double  etalon_norm[3];

                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;
                }

                roi.coi = 0;

⌨️ 快捷键说明

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