--- /dev/null
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+// By downloading, copying, installing or using the software you agree to this license.\r
+// If you do not agree to this license, do not download, install,\r
+// copy or use the software.\r
+//\r
+//\r
+// Intel License Agreement\r
+// For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000, Intel Corporation, all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+// * Redistribution's of source code must retain the above copyright notice,\r
+// this list of conditions and the following disclaimer.\r
+//\r
+// * Redistribution's in binary form must reproduce the above copyright notice,\r
+// this list of conditions and the following disclaimer in the documentation\r
+// and/or other materials provided with the distribution.\r
+//\r
+// * The name of Intel Corporation may not be used to endorse or promote products\r
+// derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "_cv.h"\r
+\r
+static CvStatus CV_STDCALL icvUpdateMotionHistory_8u32f_C1IR\r
+ (const uchar * silIm, int silStep, float *mhiIm, int mhiStep,\r
+ CvSize size, float timestamp, float mhi_duration)\r
+{\r
+ int x, y;\r
+\r
+ /* function processes floating-point images using integer arithmetics */\r
+ Cv32suf v;\r
+ int ts, delbound;\r
+ int *mhi = (int *) mhiIm;\r
+\r
+ v.f = timestamp;\r
+ ts = v.i;\r
+\r
+ if( !silIm || !mhiIm )\r
+ return CV_NULLPTR_ERR;\r
+\r
+ if( size.height <= 0 || size.width <= 0 ||\r
+ silStep < size.width || mhiStep < size.width * CV_SIZEOF_FLOAT ||\r
+ (mhiStep & (CV_SIZEOF_FLOAT - 1)) != 0 )\r
+ return CV_BADSIZE_ERR;\r
+\r
+ if( mhi_duration < 0 )\r
+ return CV_BADFACTOR_ERR;\r
+\r
+ mhi_duration = timestamp - mhi_duration;\r
+\r
+ v.f = mhi_duration;\r
+ delbound = CV_TOGGLE_FLT( v.i );\r
+\r
+ mhiStep /= sizeof(mhi[0]);\r
+\r
+ if( mhiStep == size.width && silStep == size.width )\r
+ {\r
+ size.width *= size.height;\r
+ size.height = 1;\r
+ }\r
+\r
+ if( delbound > 0 )\r
+ for( y = 0; y < size.height; y++, silIm += silStep, mhi += mhiStep )\r
+ for( x = 0; x < size.width; x++ )\r
+ {\r
+ int val = mhi[x];\r
+\r
+ /* val = silIm[x] ? ts : val < delbound ? 0 : val; */\r
+ val &= (val < delbound) - 1;\r
+ val ^= (ts ^ val) & ((silIm[x] == 0) - 1);\r
+ mhi[x] = val;\r
+ }\r
+ else\r
+ for( y = 0; y < size.height; y++, silIm += silStep, mhi += mhiStep )\r
+ for( x = 0; x < size.width; x++ )\r
+ {\r
+ int val = mhi[x];\r
+\r
+ /* val = silIm[x] ? ts : val < delbound ? 0 : val; */\r
+ val &= (CV_TOGGLE_FLT( val ) < delbound) - 1;\r
+ val ^= (ts ^ val) & ((silIm[x] == 0) - 1);\r
+ mhi[x] = val;\r
+ }\r
+\r
+ return CV_OK;\r
+}\r
+\r
+\r
+/* motion templates */\r
+CV_IMPL void\r
+cvUpdateMotionHistory( const void* silhouette, void* mhimg,\r
+ double timestamp, double mhi_duration )\r
+{\r
+ CvSize size;\r
+ CvMat silhstub, *silh = (CvMat*)silhouette;\r
+ CvMat mhistub, *mhi = (CvMat*)mhimg;\r
+ int mhi_step, silh_step;\r
+\r
+ CV_FUNCNAME( "cvUpdateMHIByTime" );\r
+\r
+ __BEGIN__;\r
+\r
+ CV_CALL( silh = cvGetMat( silh, &silhstub ));\r
+ CV_CALL( mhi = cvGetMat( mhi, &mhistub ));\r
+\r
+ if( !CV_IS_MASK_ARR( silh ))\r
+ CV_ERROR( CV_StsBadMask, "" );\r
+\r
+ if( CV_MAT_CN( mhi->type ) > 1 )\r
+ CV_ERROR( CV_BadNumChannels, "" );\r
+\r
+ if( CV_MAT_DEPTH( mhi->type ) != CV_32F )\r
+ CV_ERROR( CV_BadDepth, "" );\r
+\r
+ if( !CV_ARE_SIZES_EQ( mhi, silh ))\r
+ CV_ERROR( CV_StsUnmatchedSizes, "" );\r
+\r
+ size = cvGetMatSize( mhi );\r
+\r
+ mhi_step = mhi->step;\r
+ silh_step = silh->step;\r
+\r
+ if( CV_IS_MAT_CONT( mhi->type & silh->type ))\r
+ {\r
+ size.width *= size.height;\r
+ mhi_step = silh_step = CV_STUB_STEP;\r
+ size.height = 1;\r
+ }\r
+\r
+ IPPI_CALL( icvUpdateMotionHistory_8u32f_C1IR( (const uchar*)(silh->data.ptr), silh_step,\r
+ mhi->data.fl, mhi_step, size,\r
+ (float)timestamp, (float)mhi_duration ));\r
+ __END__;\r
+}\r
+\r
+\r
+CV_IMPL void\r
+cvCalcMotionGradient( const CvArr* mhiimg, CvArr* maskimg,\r
+ CvArr* orientation,\r
+ double delta1, double delta2,\r
+ int aperture_size )\r
+{\r
+ CvMat *dX_min = 0, *dY_max = 0;\r
+ IplConvKernel* el = 0;\r
+\r
+ CV_FUNCNAME( "cvCalcMotionGradient" );\r
+\r
+ __BEGIN__;\r
+\r
+ CvMat mhistub, *mhi = (CvMat*)mhiimg;\r
+ CvMat maskstub, *mask = (CvMat*)maskimg;\r
+ CvMat orientstub, *orient = (CvMat*)orientation;\r
+ CvMat dX_min_row, dY_max_row, orient_row, mask_row;\r
+ CvSize size;\r
+ int x, y;\r
+\r
+ float gradient_epsilon = 1e-4f * aperture_size * aperture_size;\r
+ float min_delta, max_delta;\r
+\r
+ CV_CALL( mhi = cvGetMat( mhi, &mhistub ));\r
+ CV_CALL( mask = cvGetMat( mask, &maskstub ));\r
+ CV_CALL( orient = cvGetMat( orient, &orientstub ));\r
+\r
+ if( !CV_IS_MASK_ARR( mask ))\r
+ CV_ERROR( CV_StsBadMask, "" );\r
+\r
+ if( aperture_size < 3 || aperture_size > 7 || (aperture_size & 1) == 0 )\r
+ CV_ERROR( CV_StsOutOfRange, "aperture_size must be 3, 5 or 7" );\r
+\r
+ if( delta1 <= 0 || delta2 <= 0 )\r
+ CV_ERROR( CV_StsOutOfRange, "both delta's must be positive" );\r
+\r
+ if( CV_MAT_TYPE( mhi->type ) != CV_32FC1 || CV_MAT_TYPE( orient->type ) != CV_32FC1 )\r
+ CV_ERROR( CV_StsUnsupportedFormat,\r
+ "MHI and orientation must be single-channel floating-point images" );\r
+\r
+ if( !CV_ARE_SIZES_EQ( mhi, mask ) || !CV_ARE_SIZES_EQ( orient, mhi ))\r
+ CV_ERROR( CV_StsUnmatchedSizes, "" );\r
+\r
+ if( orient->data.ptr == mhi->data.ptr )\r
+ CV_ERROR( CV_StsInplaceNotSupported, "orientation image must be different from MHI" );\r
+\r
+ if( delta1 > delta2 )\r
+ {\r
+ double t;\r
+ CV_SWAP( delta1, delta2, t );\r
+ }\r
+\r
+ size = cvGetMatSize( mhi );\r
+ min_delta = (float)delta1;\r
+ max_delta = (float)delta2;\r
+ CV_CALL( dX_min = cvCreateMat( mhi->rows, mhi->cols, CV_32F ));\r
+ CV_CALL( dY_max = cvCreateMat( mhi->rows, mhi->cols, CV_32F ));\r
+\r
+ /* calc Dx and Dy */\r
+ CV_CALL( cvSobel( mhi, dX_min, 1, 0, aperture_size ));\r
+ CV_CALL( cvSobel( mhi, dY_max, 0, 1, aperture_size ));\r
+ cvGetRow( dX_min, &dX_min_row, 0 );\r
+ cvGetRow( dY_max, &dY_max_row, 0 );\r
+ cvGetRow( orient, &orient_row, 0 );\r
+ cvGetRow( mask, &mask_row, 0 );\r
+\r
+ /* calc gradient */\r
+ for( y = 0; y < size.height; y++ )\r
+ {\r
+ dX_min_row.data.ptr = dX_min->data.ptr + y*dX_min->step;\r
+ dY_max_row.data.ptr = dY_max->data.ptr + y*dY_max->step;\r
+ orient_row.data.ptr = orient->data.ptr + y*orient->step;\r
+ mask_row.data.ptr = mask->data.ptr + y*mask->step;\r
+ cvCartToPolar( &dX_min_row, &dY_max_row, 0, &orient_row, 1 );\r
+\r
+ /* make orientation zero where the gradient is very small */\r
+ for( x = 0; x < size.width; x++ )\r
+ {\r
+ float dY = dY_max_row.data.fl[x];\r
+ float dX = dX_min_row.data.fl[x];\r
+\r
+ if( fabs(dX) < gradient_epsilon && fabs(dY) < gradient_epsilon )\r
+ {\r
+ mask_row.data.ptr[x] = 0;\r
+ orient_row.data.i[x] = 0;\r
+ }\r
+ else\r
+ mask_row.data.ptr[x] = 1;\r
+ }\r
+ }\r
+\r
+ CV_CALL( el = cvCreateStructuringElementEx( aperture_size, aperture_size,\r
+ aperture_size/2, aperture_size/2, CV_SHAPE_RECT ));\r
+ cvErode( mhi, dX_min, el );\r
+ cvDilate( mhi, dY_max, el );\r
+\r
+ /* mask off pixels which have little motion difference in their neighborhood */\r
+ for( y = 0; y < size.height; y++ )\r
+ {\r
+ dX_min_row.data.ptr = dX_min->data.ptr + y*dX_min->step;\r
+ dY_max_row.data.ptr = dY_max->data.ptr + y*dY_max->step;\r
+ mask_row.data.ptr = mask->data.ptr + y*mask->step;\r
+ orient_row.data.ptr = orient->data.ptr + y*orient->step;\r
+ \r
+ for( x = 0; x < size.width; x++ )\r
+ {\r
+ float d0 = dY_max_row.data.fl[x] - dX_min_row.data.fl[x];\r
+\r
+ if( mask_row.data.ptr[x] == 0 || d0 < min_delta || max_delta < d0 )\r
+ {\r
+ mask_row.data.ptr[x] = 0;\r
+ orient_row.data.i[x] = 0;\r
+ }\r
+ }\r
+ }\r
+\r
+ __END__;\r
+\r
+ cvReleaseMat( &dX_min );\r
+ cvReleaseMat( &dY_max );\r
+ cvReleaseStructuringElement( &el );\r
+}\r
+\r
+\r
+CV_IMPL double\r
+cvCalcGlobalOrientation( const void* orientation, const void* maskimg, const void* mhiimg,\r
+ double curr_mhi_timestamp, double mhi_duration )\r
+{\r
+ double angle = 0;\r
+ int hist_size = 12;\r
+ CvHistogram* hist = 0;\r
+\r
+ CV_FUNCNAME( "cvCalcGlobalOrientation" );\r
+\r
+ __BEGIN__;\r
+\r
+ CvMat mhistub, *mhi = (CvMat*)mhiimg;\r
+ CvMat maskstub, *mask = (CvMat*)maskimg;\r
+ CvMat orientstub, *orient = (CvMat*)orientation;\r
+ void* _orient;\r
+ float _ranges[] = { 0, 360 };\r
+ float* ranges = _ranges;\r
+ int base_orient;\r
+ double shift_orient = 0, shift_weight = 0, fbase_orient;\r
+ double a, b;\r
+ float delbound;\r
+ CvMat mhi_row, mask_row, orient_row;\r
+ int x, y, mhi_rows, mhi_cols;\r
+\r
+ CV_CALL( mhi = cvGetMat( mhi, &mhistub ));\r
+ CV_CALL( mask = cvGetMat( mask, &maskstub ));\r
+ CV_CALL( orient = cvGetMat( orient, &orientstub ));\r
+\r
+ if( !CV_IS_MASK_ARR( mask ))\r
+ CV_ERROR( CV_StsBadMask, "" );\r
+\r
+ if( CV_MAT_TYPE( mhi->type ) != CV_32FC1 || CV_MAT_TYPE( orient->type ) != CV_32FC1 )\r
+ CV_ERROR( CV_StsUnsupportedFormat,\r
+ "MHI and orientation must be single-channel floating-point images" );\r
+\r
+ if( !CV_ARE_SIZES_EQ( mhi, mask ) || !CV_ARE_SIZES_EQ( orient, mhi ))\r
+ CV_ERROR( CV_StsUnmatchedSizes, "" );\r
+\r
+ if( mhi_duration <= 0 )\r
+ CV_ERROR( CV_StsOutOfRange, "MHI duration must be positive" );\r
+\r
+ if( orient->data.ptr == mhi->data.ptr )\r
+ CV_ERROR( CV_StsInplaceNotSupported, "orientation image must be different from MHI" );\r
+\r
+ // calculate histogram of different orientation values\r
+ CV_CALL( hist = cvCreateHist( 1, &hist_size, CV_HIST_ARRAY, &ranges ));\r
+ _orient = orient;\r
+ cvCalcArrHist( &_orient, hist, 0, mask );\r
+\r
+ // find the maximum index (the dominant orientation)\r
+ cvGetMinMaxHistValue( hist, 0, 0, 0, &base_orient );\r
+ base_orient *= 360/hist_size;\r
+\r
+ // override timestamp with the maximum value in MHI\r
+ cvMinMaxLoc( mhi, 0, &curr_mhi_timestamp, 0, 0, mask );\r
+\r
+ // find the shift relative to the dominant orientation as weighted sum of relative angles\r
+ a = 254. / 255. / mhi_duration;\r
+ b = 1. - curr_mhi_timestamp * a;\r
+ fbase_orient = base_orient;\r
+ delbound = (float)(curr_mhi_timestamp - mhi_duration);\r
+ mhi_rows = mhi->rows;\r
+ mhi_cols = mhi->cols;\r
+\r
+ if( CV_IS_MAT_CONT( mhi->type & mask->type & orient->type ))\r
+ {\r
+ mhi_cols *= mhi_rows;\r
+ mhi_rows = 1;\r
+ }\r
+\r
+ cvGetRow( mhi, &mhi_row, 0 );\r
+ cvGetRow( mask, &mask_row, 0 );\r
+ cvGetRow( orient, &orient_row, 0 );\r
+\r
+ /*\r
+ a = 254/(255*dt)\r
+ b = 1 - t*a = 1 - 254*t/(255*dur) =\r
+ (255*dt - 254*t)/(255*dt) =\r
+ (dt - (t - dt)*254)/(255*dt);\r
+ --------------------------------------------------------\r
+ ax + b = 254*x/(255*dt) + (dt - (t - dt)*254)/(255*dt) =\r
+ (254*x + dt - (t - dt)*254)/(255*dt) =\r
+ ((x - (t - dt))*254 + dt)/(255*dt) =\r
+ (((x - (t - dt))/dt)*254 + 1)/255 = (((x - low_time)/dt)*254 + 1)/255\r
+ */\r
+ for( y = 0; y < mhi_rows; y++ )\r
+ {\r
+ mhi_row.data.ptr = mhi->data.ptr + mhi->step*y;\r
+ mask_row.data.ptr = mask->data.ptr + mask->step*y;\r
+ orient_row.data.ptr = orient->data.ptr + orient->step*y;\r
+\r
+ for( x = 0; x < mhi_cols; x++ )\r
+ if( mask_row.data.ptr[x] != 0 && mhi_row.data.fl[x] > delbound )\r
+ {\r
+ /*\r
+ orient in 0..360, base_orient in 0..360\r
+ -> (rel_angle = orient - base_orient) in -360..360.\r
+ rel_angle is translated to -180..180\r
+ */\r
+ double weight = mhi_row.data.fl[x] * a + b;\r
+ int rel_angle = cvRound( orient_row.data.fl[x] - fbase_orient );\r
+\r
+ rel_angle += (rel_angle < -180 ? 360 : 0);\r
+ rel_angle += (rel_angle > 180 ? -360 : 0);\r
+\r
+ if( abs(rel_angle) < 90 )\r
+ {\r
+ shift_orient += weight * rel_angle;\r
+ shift_weight += weight;\r
+ }\r
+ }\r
+ }\r
+\r
+ // add the dominant orientation and the relative shift\r
+ if( shift_weight == 0 )\r
+ shift_weight = 0.01;\r
+\r
+ base_orient = base_orient + cvRound( shift_orient / shift_weight );\r
+ base_orient -= (base_orient < 360 ? 0 : 360);\r
+ base_orient += (base_orient >= 0 ? 0 : 360);\r
+\r
+ angle = base_orient;\r
+\r
+ __END__;\r
+\r
+ cvReleaseHist( &hist );\r
+ return angle;\r
+}\r
+\r
+\r
+CV_IMPL CvSeq*\r
+cvSegmentMotion( const CvArr* mhiimg, CvArr* segmask, CvMemStorage* storage,\r
+ double timestamp, double seg_thresh )\r
+{\r
+ CvSeq* components = 0;\r
+ CvMat* mask8u = 0;\r
+\r
+ CV_FUNCNAME( "cvSegmentMotion" );\r
+\r
+ __BEGIN__;\r
+\r
+ CvMat mhistub, *mhi = (CvMat*)mhiimg;\r
+ CvMat maskstub, *mask = (CvMat*)segmask;\r
+ Cv32suf v, comp_idx;\r
+ int stub_val, ts;\r
+ int x, y;\r
+\r
+ if( !storage )\r
+ CV_ERROR( CV_StsNullPtr, "NULL memory storage" );\r
+\r
+ CV_CALL( mhi = cvGetMat( mhi, &mhistub ));\r
+ CV_CALL( mask = cvGetMat( mask, &maskstub ));\r
+\r
+ if( CV_MAT_TYPE( mhi->type ) != CV_32FC1 || CV_MAT_TYPE( mask->type ) != CV_32FC1 )\r
+ CV_ERROR( CV_BadDepth, "Both MHI and the destination mask" );\r
+\r
+ if( !CV_ARE_SIZES_EQ( mhi, mask ))\r
+ CV_ERROR( CV_StsUnmatchedSizes, "" );\r
+\r
+ CV_CALL( mask8u = cvCreateMat( mhi->rows + 2, mhi->cols + 2, CV_8UC1 ));\r
+ cvZero( mask8u );\r
+ cvZero( mask );\r
+ CV_CALL( components = cvCreateSeq( CV_SEQ_KIND_GENERIC, sizeof(CvSeq),\r
+ sizeof(CvConnectedComp), storage ));\r
+ \r
+ v.f = (float)timestamp; ts = v.i;\r
+ v.f = FLT_MAX*0.1f; stub_val = v.i;\r
+ comp_idx.f = 1;\r
+\r
+ for( y = 0; y < mhi->rows; y++ )\r
+ {\r
+ int* mhi_row = (int*)(mhi->data.ptr + y*mhi->step);\r
+ for( x = 0; x < mhi->cols; x++ )\r
+ {\r
+ if( mhi_row[x] == 0 )\r
+ mhi_row[x] = stub_val;\r
+ }\r
+ }\r
+\r
+ for( y = 0; y < mhi->rows; y++ )\r
+ {\r
+ int* mhi_row = (int*)(mhi->data.ptr + y*mhi->step);\r
+ uchar* mask8u_row = mask8u->data.ptr + (y+1)*mask8u->step + 1;\r
+\r
+ for( x = 0; x < mhi->cols; x++ )\r
+ {\r
+ if( mhi_row[x] == ts && mask8u_row[x] == 0 )\r
+ {\r
+ CvConnectedComp comp;\r
+ int x1, y1;\r
+ CvScalar _seg_thresh = cvRealScalar(seg_thresh);\r
+ CvPoint seed = cvPoint(x,y);\r
+\r
+ CV_CALL( cvFloodFill( mhi, seed, cvRealScalar(0), _seg_thresh, _seg_thresh,\r
+ &comp, CV_FLOODFILL_MASK_ONLY + 2*256 + 4, mask8u ));\r
+\r
+ for( y1 = 0; y1 < comp.rect.height; y1++ )\r
+ {\r
+ int* mask_row1 = (int*)(mask->data.ptr +\r
+ (comp.rect.y + y1)*mask->step) + comp.rect.x;\r
+ uchar* mask8u_row1 = mask8u->data.ptr +\r
+ (comp.rect.y + y1+1)*mask8u->step + comp.rect.x+1;\r
+\r
+ for( x1 = 0; x1 < comp.rect.width; x1++ )\r
+ {\r
+ if( mask8u_row1[x1] > 1 )\r
+ {\r
+ mask8u_row1[x1] = 1;\r
+ mask_row1[x1] = comp_idx.i;\r
+ }\r
+ }\r
+ }\r
+ comp_idx.f++;\r
+ cvSeqPush( components, &comp );\r
+ }\r
+ }\r
+ }\r
+\r
+ for( y = 0; y < mhi->rows; y++ )\r
+ {\r
+ int* mhi_row = (int*)(mhi->data.ptr + y*mhi->step);\r
+ for( x = 0; x < mhi->cols; x++ )\r
+ {\r
+ if( mhi_row[x] == stub_val )\r
+ mhi_row[x] = 0;\r
+ }\r
+ }\r
+\r
+ __END__;\r
+\r
+ cvReleaseMat( &mask8u );\r
+ return components;\r
+}\r
+\r
+\r
+void cv::updateMotionHistory( const Mat& silhouette, Mat& mhi,\r
+ double timestamp, double duration )\r
+{\r
+ CvMat _silhouette = silhouette, _mhi = mhi;\r
+ cvUpdateMotionHistory( &_silhouette, &_mhi, timestamp, duration );\r
+}\r
+\r
+void cv::calcMotionGradient( const Mat& mhi, Mat& mask,\r
+ Mat& orientation,\r
+ double delta1, double delta2,\r
+ int aperture_size )\r
+{\r
+ mask.create(mhi.size(), CV_8U);\r
+ orientation.create(mhi.size(), CV_32F);\r
+ CvMat _mhi = mhi, _mask = mask, _orientation = orientation;\r
+ cvCalcMotionGradient(&_mhi, &_mask, &_orientation, delta1, delta2, aperture_size);\r
+}\r
+\r
+double cv::calcGlobalOrientation( const Mat& orientation, const Mat& mask,\r
+ const Mat& mhi, double timestamp,\r
+ double duration )\r
+{\r
+ CvMat _orientation = orientation, _mask = mask, _mhi = mhi;\r
+ return cvCalcGlobalOrientation(&_orientation, &_mask, &_mhi, timestamp, duration);\r
+}\r
+\r
+/* End of file. */\r