📄 cvmotempl.cpp
字号:
/*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"
IPCVAPI_IMPL( CvStatus, icvUpdateMotionHistory_8u32f_C1IR,
(const uchar * silIm, int silStep, float *mhiIm, int mhiStep,
CvSize size, float timestamp, float mhi_duration),
(silIm, silStep, mhiIm, mhiStep, size, timestamp, mhi_duration) )
{
int x, y;
/* function processes floating-point images using integer arithmetics */
Cv32suf v;
int ts, delbound;
int *mhi = (int *) mhiIm;
v.f = timestamp;
ts = v.i;
if( !silIm || !mhiIm )
return CV_NULLPTR_ERR;
if( size.height <= 0 || size.width <= 0 ||
silStep < size.width || mhiStep < size.width * CV_SIZEOF_FLOAT ||
(mhiStep & (CV_SIZEOF_FLOAT - 1)) != 0 )
return CV_BADSIZE_ERR;
if( mhi_duration < 0 )
return CV_BADFACTOR_ERR;
mhi_duration = timestamp - mhi_duration;
v.f = mhi_duration;
delbound = CV_TOGGLE_FLT( v.i );
mhiStep /= sizeof(mhi[0]);
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 )
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 )
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;
}
/* motion templates */
CV_IMPL void
cvUpdateMotionHistory( const void* silhouette, void* mhimg,
double timestamp, double mhi_duration )
{
CvSize size;
CvMat silhstub, *silh = (CvMat*)silhouette;
CvMat mhistub, *mhi = (CvMat*)mhimg;
int mhi_step, silh_step;
CV_FUNCNAME( "cvUpdateMHIByTime" );
__BEGIN__;
CV_CALL( silh = cvGetMat( silh, &silhstub ));
CV_CALL( mhi = cvGetMat( mhi, &mhistub ));
if( !CV_IS_MASK_ARR( silh ))
CV_ERROR( CV_StsBadMask, "" );
if( CV_MAT_CN( mhi->type ) > 1 )
CV_ERROR( CV_BadNumChannels, "" );
if( CV_MAT_DEPTH( mhi->type ) != CV_32F )
CV_ERROR( CV_BadDepth, "" );
if( !CV_ARE_SIZES_EQ( mhi, silh ))
CV_ERROR( CV_StsUnmatchedSizes, "" );
size = cvGetMatSize( mhi );
mhi_step = mhi->step;
silh_step = silh->step;
if( CV_IS_MAT_CONT( mhi->type & silh->type ))
{
size.width *= size.height;
mhi_step = silh_step = CV_STUB_STEP;
size.height = 1;
}
IPPI_CALL( icvUpdateMotionHistory_8u32f_C1IR( (const uchar*)(silh->data.ptr), silh_step,
mhi->data.fl, mhi_step, size,
(float)timestamp, (float)mhi_duration ));
__END__;
}
CV_IMPL void
cvCalcMotionGradient( const CvArr* mhiimg, CvArr* maskimg,
CvArr* orientation,
double delta1, double delta2,
int aperture_size )
{
CvMat *dX_min = 0, *dY_max = 0;
IplConvKernel* el = 0;
CV_FUNCNAME( "cvCalcMotionGradient" );
__BEGIN__;
CvMat mhistub, *mhi = (CvMat*)mhiimg;
CvMat maskstub, *mask = (CvMat*)maskimg;
CvMat orientstub, *orient = (CvMat*)orientation;
CvMat dX_min_row, dY_max_row, orient_row, mask_row;
CvSize size;
int x, y;
float gradient_epsilon = 1e-4f * aperture_size * aperture_size;
float min_delta, max_delta;
CV_CALL( mhi = cvGetMat( mhi, &mhistub ));
CV_CALL( mask = cvGetMat( mask, &maskstub ));
CV_CALL( orient = cvGetMat( orient, &orientstub ));
if( !CV_IS_MASK_ARR( mask ))
CV_ERROR( CV_StsBadMask, "" );
if( aperture_size < 3 || aperture_size > 7 || (aperture_size & 1) == 0 )
CV_ERROR( CV_StsOutOfRange, "aperture_size must be 3, 5 or 7" );
if( delta1 <= 0 || delta2 <= 0 )
CV_ERROR( CV_StsOutOfRange, "both delta's must be positive" );
if( CV_MAT_TYPE( mhi->type ) != CV_32FC1 || CV_MAT_TYPE( orient->type ) != CV_32FC1 )
CV_ERROR( CV_StsUnsupportedFormat,
"MHI and orientation must be single-channel floating-point images" );
if( !CV_ARE_SIZES_EQ( mhi, mask ) || !CV_ARE_SIZES_EQ( orient, mhi ))
CV_ERROR( CV_StsUnmatchedSizes, "" );
if( orient->data.ptr == mhi->data.ptr )
CV_ERROR( CV_StsInplaceNotSupported, "orientation image must be different from MHI" );
if( delta1 > delta2 )
{
double t;
CV_SWAP( delta1, delta2, t );
}
size = cvGetMatSize( mhi );
min_delta = (float)delta1;
max_delta = (float)delta2;
CV_CALL( dX_min = cvCreateMat( mhi->rows, mhi->cols, CV_32F ));
CV_CALL( dY_max = cvCreateMat( mhi->rows, mhi->cols, CV_32F ));
/* calc Dx and Dy */
CV_CALL( cvSobel( mhi, dX_min, 1, 0, aperture_size ));
CV_CALL( cvSobel( mhi, dY_max, 0, 1, aperture_size ));
cvGetRow( dX_min, &dX_min_row, 0 );
cvGetRow( dY_max, &dY_max_row, 0 );
cvGetRow( orient, &orient_row, 0 );
cvGetRow( mask, &mask_row, 0 );
/* calc gradient */
for( y = 0; y < size.height; y++ )
{
dX_min_row.data.ptr = dX_min->data.ptr + y*dX_min->step;
dY_max_row.data.ptr = dY_max->data.ptr + y*dY_max->step;
orient_row.data.ptr = orient->data.ptr + y*orient->step;
mask_row.data.ptr = mask->data.ptr + y*mask->step;
cvCartToPolar( &dX_min_row, &dY_max_row, 0, &orient_row, 1 );
/* make orientation zero where the gradient is very small */
for( x = 0; x < size.width; x++ )
{
float dY = dY_max_row.data.fl[x];
float dX = dX_min_row.data.fl[x];
if( fabs(dX) < gradient_epsilon && fabs(dY) < gradient_epsilon )
{
mask_row.data.ptr[x] = 0;
orient_row.data.i[x] = 0;
}
else
mask_row.data.ptr[x] = 1;
}
}
CV_CALL( el = cvCreateStructuringElementEx( aperture_size, aperture_size,
aperture_size/2, aperture_size/2, CV_SHAPE_RECT ));
cvErode( mhi, dX_min, el );
cvDilate( mhi, dY_max, el );
/* mask off pixels which have little motion difference in their neighborhood */
for( y = 0; y < size.height; y++ )
{
dX_min_row.data.ptr = dX_min->data.ptr + y*dX_min->step;
dY_max_row.data.ptr = dY_max->data.ptr + y*dY_max->step;
mask_row.data.ptr = mask->data.ptr + y*mask->step;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -