cvcalceigenobjects.cpp.svn-base

来自「非结构化路识别」· SVN-BASE 代码 · 共 507 行 · 第 1/2 页

SVN-BASE
507
字号
/*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 "_cv.h"

static int
icvDefaultBufferSize( void )
{
    return 10 * 1024 * 1024;
}

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Name: icvCalcEigenObjects_8u32fR
//    Purpose: The function calculates an orthonormal eigen basis and a mean (averaged)
//             object for a group of input objects (images, vectors, etc.). ROI supported.
//    Context:
//    Parameters: nObjects  - number of source objects
//                input     - pointer either to array of pointers to input objects
//                            or to read callback function (depending on ioFlags)
//                imgStep   - full width of each source object row in bytes
//                output    - pointer either to array of pointers to output eigen objects
//                            or to write callback function (depending on ioFlags)
//                eigStep   - full width of each eigenobject row in bytes
//                size      - ROI size of each source object
//                ioFlags   - input/output flags (see Notes)
//                ioBufSize - input/output buffer size
//                userData  - pointer to the structure which contains all necessary
//                            data for the callback functions
//                calcLimit - determines the calculation finish conditions
//                avg       - pointer to averaged object (has the same size as ROI)
//                avgStep   - full width of averaged object row in bytes
//                eigVals   - pointer to corresponding eigenvalues (array of <nObjects>
//                            elements in descending order)
//
//    Returns: CV_NO_ERR or error code
//
//    Notes: 1. input/output data (that is, input objects and eigen ones) may either
//              be allocated in the RAM or be read from/written to the HDD (or any
//              other device) by read/write callback functions. It depends on the
//              value of ioFlags paramater, which may be the following:
//                  CV_EIGOBJ_NO_CALLBACK, or 0;
//                  CV_EIGOBJ_INPUT_CALLBACK;
//                  CV_EIGOBJ_OUTPUT_CALLBACK;
//                  CV_EIGOBJ_BOTH_CALLBACK, or
//                            CV_EIGOBJ_INPUT_CALLBACK | CV_EIGOBJ_OUTPUT_CALLBACK.
//              The callback functions as well as the user data structure must be
//              developed by the user.
//
//           2. If ioBufSize = 0, or it's too large, the function dermines buffer size
//              itself.
//
//           3. Depending on calcLimit parameter, calculations are finished either if
//              eigenfaces number comes up to certain value or the relation of the
//              current eigenvalue and the largest one comes down to certain value
//              (or any of the above conditions takes place). The calcLimit->type value
//              must be CV_TERMCRIT_NUMB, CV_TERMCRIT_EPS or
//              CV_TERMCRIT_NUMB | CV_TERMCRIT_EPS. The function returns the real
//              values calcLimit->maxIter and calcLimit->epsilon.
//
//           4. eigVals may be equal to NULL (if you don't need eigen values in further).
//
//F*/

IPCVAPI_IMPL( CvStatus, icvCalcEigenObjects_8u32fR, (int    nObjects,
                                                     void*  input,
                                                     int    objStep,
                                                     void*  output,
                                                     int    eigStep,
                                                     CvSize size,
                                                     int    ioFlags,
                                                     int    ioBufSize,
                                                     void*  userData,
                                                     CvTermCriteria* calcLimit,
                                                     float* avg,
                                                     int    avgStep, 
                                                     float  *eigVals) )
{
    int i, j, n, iev = 0, m1 = nObjects - 1, objStep1 = objStep, eigStep1 = eigStep / 4;
    CvSize objSize, eigSize, avgSize;
    float *c = 0;
    float *ev = 0;
    float *bf = 0;
    uchar *buf = 0;
    void *buffer = 0;
    float m = 1.0f / (float) nObjects;
    CvStatus r;

    if( m1 > calcLimit->maxIter && calcLimit->type != CV_TERMCRIT_EPS )
        m1 = calcLimit->maxIter;

    /* ---- TEST OF PARAMETERS ---- */

    if( nObjects < 2 )
        return CV_BADFACTOR_ERR;
    if( ioFlags < 0 || ioFlags > 3 )
        return CV_BADFACTOR_ERR;
    if( input == NULL || output == NULL || avg == NULL )
        return CV_NULLPTR_ERR;
    if( size.width > objStep || 4 * size.width > eigStep ||
        4 * size.width > avgStep || size.height < 1 )
        return CV_BADSIZE_ERR;
    if( !(ioFlags & CV_EIGOBJ_INPUT_CALLBACK) )
        for( i = 0; i < nObjects; i++ )
            if( ((uchar **) input)[i] == NULL )
                return CV_NULLPTR_ERR;
    if( !(ioFlags & CV_EIGOBJ_OUTPUT_CALLBACK) )
        for( i = 0; i < m1; i++ )
            if( ((float **) output)[i] == NULL )
                return CV_NULLPTR_ERR;

    avgStep /= 4;
    eigStep /= 4;

    if( objStep == size.width && eigStep == size.width && avgStep == size.width )
    {
        size.width *= size.height;
        size.height = 1;
        objStep = objStep1 = eigStep = eigStep1 = avgStep = size.width;
    }
    objSize = eigSize = avgSize = size;

    if( ioFlags & CV_EIGOBJ_INPUT_CALLBACK )
    {
        objSize.width *= objSize.height;
        objSize.height = 1;
        objStep = objSize.width;
        objStep1 = size.width;
    }

    if( ioFlags & CV_EIGOBJ_OUTPUT_CALLBACK )
    {
        eigSize.width *= eigSize.height;
        eigSize.height = 1;
        eigStep = eigSize.width;
        eigStep1 = size.width;
    }

    n = objSize.height * objSize.width * (ioFlags & CV_EIGOBJ_INPUT_CALLBACK) +
        2 * eigSize.height * eigSize.width * (ioFlags & CV_EIGOBJ_OUTPUT_CALLBACK);

    /* Buffer size determination */
    if( ioFlags )
    {
        int size = icvDefaultBufferSize();
        ioBufSize = MIN( size, n );
    }

    /* memory allocation (if necesseay) */

    if( ioFlags & CV_EIGOBJ_INPUT_CALLBACK )
    {
        buf = (uchar *) icvAlloc( sizeof( uchar ) * objSize.width );
        if( buf == NULL )
            return CV_OUTOFMEM_ERR;
    }

    if( ioFlags )
    {
        buffer = (void *) icvAlloc( ioBufSize );
        if( buffer == NULL )
        {
            if( buf )
                icvFree( (void **) &buf );
            return CV_OUTOFMEM_ERR;
        }
    }

    /* Calculation of averaged object */
    bf = avg;
    for( i = 0; i < avgSize.height; i++, bf += avgStep )
        for( j = 0; j < avgSize.width; j++ )
            bf[j] = 0.f;

    for( i = 0; i < nObjects; i++ )
    {
        int k, l;
        uchar *bu = (ioFlags & CV_EIGOBJ_INPUT_CALLBACK) ? buf : ((uchar **) input)[i];

        if( ioFlags & CV_EIGOBJ_INPUT_CALLBACK )
        {
            CvCallback read_callback = ((CvInput *) & input)->callback;

            r = read_callback( i, (void *) buf, userData );
            if( r )
            {
                if( buffer )
                    icvFree( (void **) &buffer );
                if( buf )
                    icvFree( (void **) &buf );
                return r;
            }
        }

        bf = avg;
        for( k = 0; k < avgSize.height; k++, bf += avgStep, bu += objStep1 )
            for( l = 0; l < avgSize.width; l++ )
                bf[l] += bu[l];
    }

    bf = avg;
    for( i = 0; i < avgSize.height; i++, bf += avgStep )
        for( j = 0; j < avgSize.width; j++ )
            bf[j] *= m;

    /* Calculation of covariance matrix */
    c = (float *) icvAlloc( sizeof( float ) * nObjects * nObjects );

    if( c == NULL )
    {
        if( buffer )
            icvFree( (void **) &buffer );
        if( buf )
            icvFree( (void **) &buf );
        return CV_OUTOFMEM_ERR;
    }

⌨️ 快捷键说明

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