1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
44 IPCVAPI_IMPL( CvStatus, icvUpdateMotionHistory_8u32f_C1IR,
45 (const uchar * silIm, int silStep, float *mhiIm, int mhiStep,
46 CvSize size, float timestamp, float mhi_duration),
47 (silIm, silStep, mhiIm, mhiStep, size, timestamp, mhi_duration) )
51 /* function processes floating-point images using integer arithmetics */
54 int *mhi = (int *) mhiIm;
59 if( !silIm || !mhiIm )
60 return CV_NULLPTR_ERR;
62 if( size.height <= 0 || size.width <= 0 ||
63 silStep < size.width || mhiStep < size.width * CV_SIZEOF_FLOAT ||
64 (mhiStep & (CV_SIZEOF_FLOAT - 1)) != 0 )
65 return CV_BADSIZE_ERR;
67 if( mhi_duration < 0 )
68 return CV_BADFACTOR_ERR;
70 mhi_duration = timestamp - mhi_duration;
73 delbound = CV_TOGGLE_FLT( v.i );
75 mhiStep /= sizeof(mhi[0]);
77 if( mhiStep == size.width && silStep == size.width )
79 size.width *= size.height;
84 for( y = 0; y < size.height; y++, silIm += silStep, mhi += mhiStep )
85 for( x = 0; x < size.width; x++ )
89 /* val = silIm[x] ? ts : val < delbound ? 0 : val; */
90 val &= (val < delbound) - 1;
91 val ^= (ts ^ val) & ((silIm[x] == 0) - 1);
95 for( y = 0; y < size.height; y++, silIm += silStep, mhi += mhiStep )
96 for( x = 0; x < size.width; x++ )
100 /* val = silIm[x] ? ts : val < delbound ? 0 : val; */
101 val &= (CV_TOGGLE_FLT( val ) < delbound) - 1;
102 val ^= (ts ^ val) & ((silIm[x] == 0) - 1);
110 /* motion templates */
112 cvUpdateMotionHistory( const void* silhouette, void* mhimg,
113 double timestamp, double mhi_duration )
116 CvMat silhstub, *silh = (CvMat*)silhouette;
117 CvMat mhistub, *mhi = (CvMat*)mhimg;
118 int mhi_step, silh_step;
120 CV_FUNCNAME( "cvUpdateMHIByTime" );
124 CV_CALL( silh = cvGetMat( silh, &silhstub ));
125 CV_CALL( mhi = cvGetMat( mhi, &mhistub ));
127 if( !CV_IS_MASK_ARR( silh ))
128 CV_ERROR( CV_StsBadMask, "" );
130 if( CV_MAT_CN( mhi->type ) > 1 )
131 CV_ERROR( CV_BadNumChannels, "" );
133 if( CV_MAT_DEPTH( mhi->type ) != CV_32F )
134 CV_ERROR( CV_BadDepth, "" );
136 if( !CV_ARE_SIZES_EQ( mhi, silh ))
137 CV_ERROR( CV_StsUnmatchedSizes, "" );
139 size = cvGetMatSize( mhi );
141 mhi_step = mhi->step;
142 silh_step = silh->step;
144 if( CV_IS_MAT_CONT( mhi->type & silh->type ))
146 size.width *= size.height;
147 mhi_step = silh_step = CV_STUB_STEP;
151 IPPI_CALL( icvUpdateMotionHistory_8u32f_C1IR( (const uchar*)(silh->data.ptr), silh_step,
152 mhi->data.fl, mhi_step, size,
153 (float)timestamp, (float)mhi_duration ));
159 cvCalcMotionGradient( const CvArr* mhiimg, CvArr* maskimg,
161 double delta1, double delta2,
164 CvMat *dX_min = 0, *dY_max = 0;
165 IplConvKernel* el = 0;
167 CV_FUNCNAME( "cvCalcMotionGradient" );
171 CvMat mhistub, *mhi = (CvMat*)mhiimg;
172 CvMat maskstub, *mask = (CvMat*)maskimg;
173 CvMat orientstub, *orient = (CvMat*)orientation;
174 CvMat dX_min_row, dY_max_row, orient_row, mask_row;
178 float gradient_epsilon = 1e-4f * aperture_size * aperture_size;
179 float min_delta, max_delta;
181 CV_CALL( mhi = cvGetMat( mhi, &mhistub ));
182 CV_CALL( mask = cvGetMat( mask, &maskstub ));
183 CV_CALL( orient = cvGetMat( orient, &orientstub ));
185 if( !CV_IS_MASK_ARR( mask ))
186 CV_ERROR( CV_StsBadMask, "" );
188 if( aperture_size < 3 || aperture_size > 7 || (aperture_size & 1) == 0 )
189 CV_ERROR( CV_StsOutOfRange, "aperture_size must be 3, 5 or 7" );
191 if( delta1 <= 0 || delta2 <= 0 )
192 CV_ERROR( CV_StsOutOfRange, "both delta's must be positive" );
194 if( CV_MAT_TYPE( mhi->type ) != CV_32FC1 || CV_MAT_TYPE( orient->type ) != CV_32FC1 )
195 CV_ERROR( CV_StsUnsupportedFormat,
196 "MHI and orientation must be single-channel floating-point images" );
198 if( !CV_ARE_SIZES_EQ( mhi, mask ) || !CV_ARE_SIZES_EQ( orient, mhi ))
199 CV_ERROR( CV_StsUnmatchedSizes, "" );
201 if( orient->data.ptr == mhi->data.ptr )
202 CV_ERROR( CV_StsInplaceNotSupported, "orientation image must be different from MHI" );
204 if( delta1 > delta2 )
207 CV_SWAP( delta1, delta2, t );
210 size = cvGetMatSize( mhi );
211 min_delta = (float)delta1;
212 max_delta = (float)delta2;
213 CV_CALL( dX_min = cvCreateMat( mhi->rows, mhi->cols, CV_32F ));
214 CV_CALL( dY_max = cvCreateMat( mhi->rows, mhi->cols, CV_32F ));
217 CV_CALL( cvSobel( mhi, dX_min, 1, 0, aperture_size ));
218 CV_CALL( cvSobel( mhi, dY_max, 0, 1, aperture_size ));
219 cvGetRow( dX_min, &dX_min_row, 0 );
220 cvGetRow( dY_max, &dY_max_row, 0 );
221 cvGetRow( orient, &orient_row, 0 );
222 cvGetRow( mask, &mask_row, 0 );
225 for( y = 0; y < size.height; y++ )
227 dX_min_row.data.ptr = dX_min->data.ptr + y*dX_min->step;
228 dY_max_row.data.ptr = dY_max->data.ptr + y*dY_max->step;
229 orient_row.data.ptr = orient->data.ptr + y*orient->step;
230 mask_row.data.ptr = mask->data.ptr + y*mask->step;
231 cvCartToPolar( &dX_min_row, &dY_max_row, 0, &orient_row, 1 );
233 /* make orientation zero where the gradient is very small */
234 for( x = 0; x < size.width; x++ )
236 float dY = dY_max_row.data.fl[x];
237 float dX = dX_min_row.data.fl[x];
239 if( fabs(dX) < gradient_epsilon && fabs(dY) < gradient_epsilon )
241 mask_row.data.ptr[x] = 0;
242 orient_row.data.i[x] = 0;
245 mask_row.data.ptr[x] = 1;
249 CV_CALL( el = cvCreateStructuringElementEx( aperture_size, aperture_size,
250 aperture_size/2, aperture_size/2, CV_SHAPE_RECT ));
251 cvErode( mhi, dX_min, el );
252 cvDilate( mhi, dY_max, el );
254 /* mask off pixels which have little motion difference in their neighborhood */
255 for( y = 0; y < size.height; y++ )
257 dX_min_row.data.ptr = dX_min->data.ptr + y*dX_min->step;
258 dY_max_row.data.ptr = dY_max->data.ptr + y*dY_max->step;
259 mask_row.data.ptr = mask->data.ptr + y*mask->step;
260 orient_row.data.ptr = orient->data.ptr + y*orient->step;
262 for( x = 0; x < size.width; x++ )
264 float d0 = dY_max_row.data.fl[x] - dX_min_row.data.fl[x];
266 if( mask_row.data.ptr[x] == 0 || d0 < min_delta || max_delta < d0 )
268 mask_row.data.ptr[x] = 0;
269 orient_row.data.i[x] = 0;
276 cvReleaseMat( &dX_min );
277 cvReleaseMat( &dY_max );
278 cvReleaseStructuringElement( &el );
283 cvCalcGlobalOrientation( const void* orientation, const void* maskimg, const void* mhiimg,
284 double curr_mhi_timestamp, double mhi_duration )
288 CvHistogram* hist = 0;
290 CV_FUNCNAME( "cvCalcGlobalOrientation" );
294 CvMat mhistub, *mhi = (CvMat*)mhiimg;
295 CvMat maskstub, *mask = (CvMat*)maskimg;
296 CvMat orientstub, *orient = (CvMat*)orientation;
298 float _ranges[] = { 0, 360 };
299 float* ranges = _ranges;
301 double shift_orient = 0, shift_weight = 0, fbase_orient;
304 CvMat mhi_row, mask_row, orient_row;
305 int x, y, mhi_rows, mhi_cols;
307 CV_CALL( mhi = cvGetMat( mhi, &mhistub ));
308 CV_CALL( mask = cvGetMat( mask, &maskstub ));
309 CV_CALL( orient = cvGetMat( orient, &orientstub ));
311 if( !CV_IS_MASK_ARR( mask ))
312 CV_ERROR( CV_StsBadMask, "" );
314 if( CV_MAT_TYPE( mhi->type ) != CV_32FC1 || CV_MAT_TYPE( orient->type ) != CV_32FC1 )
315 CV_ERROR( CV_StsUnsupportedFormat,
316 "MHI and orientation must be single-channel floating-point images" );
318 if( !CV_ARE_SIZES_EQ( mhi, mask ) || !CV_ARE_SIZES_EQ( orient, mhi ))
319 CV_ERROR( CV_StsUnmatchedSizes, "" );
321 if( mhi_duration <= 0 )
322 CV_ERROR( CV_StsOutOfRange, "MHI duration must be positive" );
324 if( orient->data.ptr == mhi->data.ptr )
325 CV_ERROR( CV_StsInplaceNotSupported, "orientation image must be different from MHI" );
327 // calculate histogram of different orientation values
328 CV_CALL( hist = cvCreateHist( 1, &hist_size, CV_HIST_ARRAY, &ranges ));
330 cvCalcArrHist( &_orient, hist, 0, mask );
332 // find the maximum index (the dominant orientation)
333 cvGetMinMaxHistValue( hist, 0, 0, 0, &base_orient );
334 base_orient *= 360/hist_size;
336 // override timestamp with the maximum value in MHI
337 cvMinMaxLoc( mhi, 0, &curr_mhi_timestamp, 0, 0, mask );
339 // find the shift relative to the dominant orientation as weighted sum of relative angles
340 a = 254. / 255. / mhi_duration;
341 b = 1. - curr_mhi_timestamp * a;
342 fbase_orient = base_orient;
343 delbound = (float)(curr_mhi_timestamp - mhi_duration);
344 mhi_rows = mhi->rows;
345 mhi_cols = mhi->cols;
347 if( CV_IS_MAT_CONT( mhi->type & mask->type & orient->type ))
349 mhi_cols *= mhi_rows;
353 cvGetRow( mhi, &mhi_row, 0 );
354 cvGetRow( mask, &mask_row, 0 );
355 cvGetRow( orient, &orient_row, 0 );
359 b = 1 - t*a = 1 - 254*t/(255*dur) =
360 (255*dt - 254*t)/(255*dt) =
361 (dt - (t - dt)*254)/(255*dt);
362 --------------------------------------------------------
363 ax + b = 254*x/(255*dt) + (dt - (t - dt)*254)/(255*dt) =
364 (254*x + dt - (t - dt)*254)/(255*dt) =
365 ((x - (t - dt))*254 + dt)/(255*dt) =
366 (((x - (t - dt))/dt)*254 + 1)/255 = (((x - low_time)/dt)*254 + 1)/255
368 for( y = 0; y < mhi_rows; y++ )
370 mhi_row.data.ptr = mhi->data.ptr + mhi->step*y;
371 mask_row.data.ptr = mask->data.ptr + mask->step*y;
372 orient_row.data.ptr = orient->data.ptr + orient->step*y;
374 for( x = 0; x < mhi_cols; x++ )
375 if( mask_row.data.ptr[x] != 0 && mhi_row.data.fl[x] > delbound )
378 orient in 0..360, base_orient in 0..360
379 -> (rel_angle = orient - base_orient) in -360..360.
380 rel_angle is translated to -180..180
382 double weight = mhi_row.data.fl[x] * a + b;
383 int rel_angle = cvRound( orient_row.data.fl[x] - fbase_orient );
385 rel_angle += (rel_angle < -180 ? 360 : 0);
386 rel_angle += (rel_angle > 180 ? -360 : 0);
388 if( abs(rel_angle) < 90 )
390 shift_orient += weight * rel_angle;
391 shift_weight += weight;
396 // add the dominant orientation and the relative shift
397 if( shift_weight == 0 )
400 base_orient = base_orient + cvRound( shift_orient / shift_weight );
401 base_orient -= (base_orient < 360 ? 0 : 360);
402 base_orient += (base_orient >= 0 ? 0 : 360);
408 cvReleaseHist( &hist );
414 cvSegmentMotion( const CvArr* mhiimg, CvArr* segmask, CvMemStorage* storage,
415 double timestamp, double seg_thresh )
417 CvSeq* components = 0;
420 CV_FUNCNAME( "cvSegmentMotion" );
424 CvMat mhistub, *mhi = (CvMat*)mhiimg;
425 CvMat maskstub, *mask = (CvMat*)segmask;
431 CV_ERROR( CV_StsNullPtr, "NULL memory storage" );
433 CV_CALL( mhi = cvGetMat( mhi, &mhistub ));
434 CV_CALL( mask = cvGetMat( mask, &maskstub ));
436 if( CV_MAT_TYPE( mhi->type ) != CV_32FC1 || CV_MAT_TYPE( mask->type ) != CV_32FC1 )
437 CV_ERROR( CV_BadDepth, "Both MHI and the destination mask" );
439 if( !CV_ARE_SIZES_EQ( mhi, mask ))
440 CV_ERROR( CV_StsUnmatchedSizes, "" );
442 CV_CALL( mask8u = cvCreateMat( mhi->rows + 2, mhi->cols + 2, CV_8UC1 ));
445 CV_CALL( components = cvCreateSeq( CV_SEQ_KIND_GENERIC, sizeof(CvSeq),
446 sizeof(CvConnectedComp), storage ));
448 v.f = (float)timestamp; ts = v.i;
449 v.f = FLT_MAX*0.1f; stub_val = v.i;
452 for( y = 0; y < mhi->rows; y++ )
454 int* mhi_row = (int*)(mhi->data.ptr + y*mhi->step);
455 for( x = 0; x < mhi->cols; x++ )
457 if( mhi_row[x] == 0 )
458 mhi_row[x] = stub_val;
462 for( y = 0; y < mhi->rows; y++ )
464 int* mhi_row = (int*)(mhi->data.ptr + y*mhi->step);
465 uchar* mask8u_row = mask8u->data.ptr + (y+1)*mask8u->step + 1;
467 for( x = 0; x < mhi->cols; x++ )
469 if( mhi_row[x] == ts && mask8u_row[x] == 0 )
471 CvConnectedComp comp;
473 CvScalar _seg_thresh = cvRealScalar(seg_thresh);
474 CvPoint seed = cvPoint(x,y);
476 CV_CALL( cvFloodFill( mhi, seed, cvRealScalar(0), _seg_thresh, _seg_thresh,
477 &comp, CV_FLOODFILL_MASK_ONLY + 2*256 + 4, mask8u ));
479 for( y1 = 0; y1 < comp.rect.height; y1++ )
481 int* mask_row1 = (int*)(mask->data.ptr +
482 (comp.rect.y + y1)*mask->step) + comp.rect.x;
483 uchar* mask8u_row1 = mask8u->data.ptr +
484 (comp.rect.y + y1+1)*mask8u->step + comp.rect.x+1;
486 for( x1 = 0; x1 < comp.rect.width; x1++ )
488 if( mask8u_row1[x1] > 1 )
491 mask_row1[x1] = comp_idx.i;
496 cvSeqPush( components, &comp );
501 for( y = 0; y < mhi->rows; y++ )
503 int* mhi_row = (int*)(mhi->data.ptr + y*mhi->step);
504 for( x = 0; x < mhi->cols; x++ )
506 if( mhi_row[x] == stub_val )
513 cvReleaseMat( &mask8u );