cvmotempl.cpp.svn-base

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

SVN-BASE
722
字号
/*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"

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Name:    icvUpdateMHIByTime32fC1R
//    Purpose: The function updates motion history image.
//    Context:
//    Parameters:
//        silIm         - silhouette image
//        silStep       - its step
//        mhiIm         - motion history image
//        mhiStep       - its step
//        size          - size of both images (in pixels)
//        timestamp     - current system time (in seconds)
//        mhi_duration  - maximal duration of motion track before it will
//                        be removed (in seconds too)
//    Returns:
//        CV_OK or error code:
//           CV_NULLPTR_ERR - silIm or mhiIm pointer are null
//           CV_BADSIZE_ERR   - width or height is negative or steps less than width
//           CV_BADFACTOR_ERR - mhi_duration is non positive
//    Notes:
//F*/
IPCVAPI_IMPL( CvStatus, icvUpdateMotionHistory_8u32f_C1IR, (const uchar * silIm,
                                                            int silStep,
                                                            float *mhiIm,
                                                            int mhiStep,
                                                            CvSize size,
                                                            float timestamp,
                                                            float mhi_duration) )
{
    int y;
    int delbound;

    /* function processes floating-point images using integer arithmetics */
    int ts = *((int *) &timestamp);
    int *mhi = (int *) mhiIm;

    if( !silIm || !mhiIm )
        return CV_NULLPTR_ERR;

    if( size.height <= 0 || size.width <= 0 ||
        silStep < size.width || mhiStep < size.width * sizeof_float ||
        (mhiStep & (sizeof_float - 1)) != 0 )
        return CV_BADSIZE_ERR;

    if( mhi_duration < 0 )
        return CV_BADFACTOR_ERR;

    mhi_duration = timestamp - mhi_duration;
    delbound = CV_TOGGLE_FLT( (*(int *) &mhi_duration) );

    mhiStep /= sizeof_float;

    if( mhiStep == size.width && silStep == size.width )
    {
        size.width *= size.height;
        size.height = 1;
    }

    if( delbound > 0 )
        for( y = 0; y < size.height; y++, silIm += silStep, mhi += mhiStep )
        {
            int x;

            for( x = 0; x < size.width; x++ )
            {
                int val = mhi[x];

                /* val = silIm[x] ? ts : val < delbound ? 0 : val; */
                val &= (val < delbound) - 1;
                val ^= (ts ^ val) & ((silIm[x] == 0) - 1);
                mhi[x] = val;
            }
        }
    else
        for( y = 0; y < size.height; y++, silIm += silStep, mhi += mhiStep )
        {
            int x;

            for( x = 0; x < size.width; x++ )
            {
                int val = mhi[x];

                /* val = silIm[x] ? ts : val < delbound ? 0 : val; */
                val &= (CV_TOGGLE_FLT( val ) < delbound) - 1;
                val ^= (ts ^ val) & ((silIm[x] == 0) - 1);
                mhi[x] = val;
            }
        }

    return CV_OK;
}


/*F///////////////////////////////////////////////////////////////////////////////////////
//    Name:    icvCalcMotionGradient32fC1R
//    Purpose: The functions calculates motion gradient and mask where it is valid
//    Context:
//    Parameters:
//       mhi         - motion history image
//       mhiStep     - its step
//       mask        - mask(out): indicates where <orient> data is valid
//       maskStep    - its step
//       orient      - image containing gradient orientation(out) (in degrees)
//       orientStep  - its step
//       size        - size of the all images in pixels
//       aperture_size - size of the filters for Dx & Dy
//       maxTDelta   - gradient bounds.
//       minTDelta   _/
//       origin      - 0 - image data start from geometrical top (y coordinate increases)
//                     1 - image data start from geometrical bottom (y decreases)
//    Returns:
//        CV_OK or error code:
//        CV_NULLPTR_ERR - orient or mask or mhi pointers are null
//        CV_BADSIZE_ERR   - width or height is negative or steps less than width
//        CV_BADFACTOR_ERR - if sobel_order parameter is invalid (<2 or >8 or uneven) or
//                            minDelta >= maxDelta.
//    Notes:
//F*/
static CvStatus
icvCalcMotionGradient32fC1R( float *mhi, int mhiStep,
                             uchar * mask, int maskStep,
                             float *orient, int orientStep,
                             CvSize roi, int apertureSize,
                             float maxTDelta, float minTDelta, int origin )
{
    float gradientEps = 1e-4f * apertureSize;
    int iGradientEps = (int &) gradientEps;
    int iMinDelta = *(int *) &minTDelta;
    int iMaxDelta = *(int *) &maxTDelta;
    int x, y;
    CvMorphState *morph_filter;
    float *drvX_min;
    float *drvY_max;
    int tempStep;
    int tempSize;
    _CvConvState *pX;
    _CvConvState *pY;

    CvStatus result = CV_OK;

    /* check parameters */
    if( !orient || !mask || !mhi )
        return CV_NULLPTR_ERR;

    if( orient == mhi )
        return CV_INPLACE_NOT_SUPPORTED_ERR;

    if( roi.height <= 0 || roi.width <= 0 || orientStep < roi.width * 4 ||
        maskStep < roi.width || mhiStep < roi.width * 4 )
        return CV_BADSIZE_ERR;
    if( ((mhiStep | orientStep) & 3) != 0 )
        return CV_BADSIZE_ERR;

    tempStep = icvAlign(roi.width,2) * sizeof( float );

    tempSize = tempStep * roi.height;

    drvX_min = (float *) icvAlloc( tempSize );
    drvY_max = (float *) icvAlloc( tempSize );
    if( !drvX_min || !drvY_max )
    {
        result = CV_OUTOFMEM_ERR;
        goto func_exit;
    }

    icvMorphologyInitAlloc( roi.width, cv32f, 1,
                             cvSize( apertureSize, apertureSize ),
                             cvPoint( apertureSize / 2, apertureSize / 2 ),
                             CV_SHAPE_RECT, 0, &morph_filter );

    /* calc Dx and Dy */
    icvSobelInitAlloc( roi.width, cv32f, apertureSize, origin, 1, 0, &pX );
    result = icvSobel_32f_C1R( mhi, mhiStep, drvX_min, tempStep, &roi, pX, 0 );
    icvFilterFree( &pX );

    if( result < 0 )
        goto func_exit;
    icvSobelInitAlloc( roi.width, cv32f, apertureSize, origin, 0, 1, &pY );
    result = icvSobel_32f_C1R( mhi, mhiStep, drvY_max, tempStep, &roi, pY, 0 );
    icvFilterFree( &pY );

    if( result < 0 )
        goto func_exit;

    /* calc gradient */
    for( y = 0; y < roi.height; y++, drvX_min += tempStep / sizeof( float ),
         drvY_max += tempStep / sizeof( float ), orient += orientStep / sizeof( float ))
    {
        icvbFastArctan_32f( drvY_max, drvX_min, orient, roi.width );

        /* make orientation zero where the gradient is very small */
        for( x = 0; x < roi.width; x++ )
        {
            int dY = ((int *) drvY_max)[x] & 0x7fffffff;
            int dX = ((int *) drvX_min)[x] & 0x7fffffff;

            ((int *) orient)[x] &= ((dX < iGradientEps) && (dY < iGradientEps)) - 1;
        }
    }

    drvX_min -= tempSize / sizeof( float );
    drvY_max -= tempSize / sizeof( float );
    orient -= (orientStep / sizeof( float )) * roi.height;

    result = icvErodeStrip_Rect_32f_C1R( mhi, mhiStep, drvX_min, tempStep,
                                          &roi, morph_filter, 0 );
    if( result < 0 )
        goto func_exit;

    result = icvDilateStrip_Rect_32f_C1R( mhi, mhiStep, drvY_max, tempStep,
                                           &roi, morph_filter, 0 );
    if( result < 0 )
        goto func_exit;

    /* mask off pixels which have little motion difference in their neighborhood */
    for( y = 0; y < roi.height; y++, drvX_min += tempStep / sizeof( float ),
         drvY_max += tempStep / sizeof( float ),
         orient += orientStep / sizeof( float ), mask += maskStep )
    {
        for( x = 0; x <= roi.width - 4; x += 4 )
        {
            float d0 = drvY_max[x] - drvX_min[x];
            float d1 = drvY_max[x + 1] - drvX_min[x + 1];
            float d2 = drvY_max[x + 2] - drvX_min[x + 2];
            float d3 = drvY_max[x + 3] - drvX_min[x + 3];

            mask[x] = (uchar) ((*(int *) &d0 >= iMinDelta) && (*(int *) &d0 <= iMaxDelta));
            mask[x + 1] = (uchar) ((*(int *) &d1 >= iMinDelta) && (*(int *) &d1 <= iMaxDelta));
            mask[x + 2] = (uchar) ((*(int *) &d2 >= iMinDelta) && (*(int *) &d2 <= iMaxDelta));
            mask[x + 3] = (uchar) ((*(int *) &d3 >= iMinDelta) && (*(int *) &d3 <= iMaxDelta));
            /*((int*)orient)[x] &= (mask[x] == 0) - 1;
               ((int*)orient)[x + 1] &= (mask[x + 1] == 0) - 1;
               ((int*)orient)[x + 2] &= (mask[x + 2] == 0) - 1;
               ((int*)orient)[x + 3] &= (mask[x + 3] == 0) - 1; */
        }

        for( ; x < roi.width; x++ )
        {
            float delta = drvY_max[x] - drvX_min[x];

            mask[x] = (uchar) ((delta >= minTDelta) && (delta <= maxTDelta));
            /*((int*)orient)[x] &= (mask[x] == 0) - 1; */
        }
    }


    drvX_min -= tempSize / sizeof( float );
    drvY_max -= tempSize / sizeof( float );

  func_exit:

    icvMorphologyFree( &morph_filter );
    icvFree( &drvX_min );
    icvFree( &drvY_max );

    return result;
}


/*F///////////////////////////////////////////////////////////////////////////////////////
//    Name:    icvCalcGlobalOrientation32fC1R
//    Purpose: The function calculates general motion direction in the selected region.
//    Context:
//    Parameters:
//         orient             - orientation image
//         orientStep         - its step
//         mask               - region mask
//         maskStep           - its step
//         mhi                - motion history image
//         mhiStep            - its step
//         size               - size of the all images in pixels
//         curr_mhi_timestamp - the last timestamp when mhi was updated
//         mhi_duration       - maximal motion track duration.
//         angle              - result: motion direction of the region
//    Returns:
//        CV_OK or error code:
//           CV_NULLPTR_ERR - orient or mask or mhi pointers are null
//           CV_BADSIZE_ERR   - width or height is negative or steps less than width
//           CV_BADFACTOR_ERR - mhi_duration is non positive
//    Notes:
//F*/
static CvStatus
icvCalcGlobalOrientation32fC1R( float *orient, int orientStep,
                                uchar * mask, int maskStep,
                                float *mhi, int mhiStep,
                                CvSize size,
                                float curr_mhi_timestamp, float mhi_duration, float *angle )
{
#define _CV_MT2_HIST_SIZE  12
    const double hist_scale = _CV_MT2_HIST_SIZE / 360.;
    float delbound = curr_mhi_timestamp - mhi_duration;
    int orient_hist[_CV_MT2_HIST_SIZE];
    int y;
    int base_orient = 0;

    if( !orient || !mask || !mhi || !angle )
        return CV_NULLPTR_ERR;

    if( orient == mhi )
        return CV_INPLACE_NOT_SUPPORTED_ERR;

    if( size.height <= 0 || size.width <= 0 ||
        orientStep < size.width * 4 || maskStep < size.width || mhiStep < size.width * 4 )
        return CV_BADSIZE_ERR;
    if( mhi_duration <= 0 )
        return CV_BADFACTOR_ERR;

    orientStep /= 4;
    mhiStep /= 4;

    memset( orient_hist, 0, sizeof( orient_hist ));

    /* 1. Calc orientation histogram */
    for( y = 0; y < size.height; y++ )
    {
        int x;

        for( x = 0; x < size.width; x++ )
            if( mask[x] != 0 )

⌨️ 快捷键说明

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