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.
41 #include "precomp.hpp"
43 #define CONV( A, B, C) ( (float)( A + (B<<1) + C ) )
52 float alpha; /* alpha = 1 / ( 1/lambda + xx + yy ) */
56 /*F///////////////////////////////////////////////////////////////////////////////////////
57 // Name: icvCalcOpticalFlowHS_8u32fR (Horn & Schunck method )
58 // Purpose: calculate Optical flow for 2 images using Horn & Schunck algorithm
61 // imgA - pointer to first frame ROI
62 // imgB - pointer to second frame ROI
63 // imgStep - width of single row of source images in bytes
64 // imgSize - size of the source image ROI
65 // usePrevious - use previous (input) velocity field.
66 // velocityX - pointer to horizontal and
67 // velocityY - vertical components of optical flow ROI
68 // velStep - width of single row of velocity frames in bytes
69 // lambda - Lagrangian multiplier
70 // criteria - criteria of termination processmaximum number of iterations
72 // Returns: CV_OK - all ok
73 // CV_OUTOFMEM_ERR - insufficient memory for function work
74 // CV_NULLPTR_ERR - if one of input pointers is NULL
75 // CV_BADSIZE_ERR - wrong input sizes interrelation
77 // Notes: 1.Optical flow to be computed for every pixel in ROI
78 // 2.For calculating spatial derivatives we use 3x3 Sobel operator.
79 // 3.We use the following border mode.
80 // The last row or column is replicated for the border
81 // ( IPL_BORDER_REPLICATE in IPL ).
85 static CvStatus CV_STDCALL
86 icvCalcOpticalFlowHS_8u32fR( uchar* imgA,
95 CvTermCriteria criteria )
100 /* Buffers for Sobel calculations */
105 float GradX, GradY, GradT;
107 int imageWidth = imgSize.width;
108 int imageHeight = imgSize.height;
115 float Ilambda = 1 / lambda;
119 /* buffers derivatives product */
125 /* variables for storing number of first pixel of image line */
135 /* Checking bad arguments */
137 return CV_NULLPTR_ERR;
139 return CV_NULLPTR_ERR;
141 if( imgSize.width <= 0 )
142 return CV_BADSIZE_ERR;
143 if( imgSize.height <= 0 )
144 return CV_BADSIZE_ERR;
145 if( imgSize.width > imgStep )
146 return CV_BADSIZE_ERR;
148 if( (velStep & 3) != 0 )
149 return CV_BADSIZE_ERR;
153 /****************************************************************************************/
154 /* Allocating memory for all buffers */
155 /****************************************************************************************/
156 for( k = 0; k < 2; k++ )
158 MemX[k] = (float *) cvAlloc( (imgSize.height) * sizeof( float ));
160 if( MemX[k] == NULL )
162 MemY[k] = (float *) cvAlloc( (imgSize.width) * sizeof( float ));
164 if( MemY[k] == NULL )
167 VelBufX[k] = (float *) cvAlloc( imageWidth * sizeof( float ));
169 if( VelBufX[k] == NULL )
171 VelBufY[k] = (float *) cvAlloc( imageWidth * sizeof( float ));
173 if( VelBufY[k] == NULL )
177 BufferSize = imageHeight * imageWidth;
179 II = (icvDerProductEx *) cvAlloc( BufferSize * sizeof( icvDerProductEx ));
185 for( k = 0; k < 2; k++ )
194 cvFree( &VelBufX[k] );
197 cvFree( &VelBufY[k] );
201 return CV_OUTOFMEM_ERR;
203 /****************************************************************************************\
204 * Calculate first line of memX and memY *
205 \****************************************************************************************/
206 MemY[0][0] = MemY[1][0] = CONV( imgA[0], imgA[0], imgA[1] );
207 MemX[0][0] = MemX[1][0] = CONV( imgA[0], imgA[0], imgA[imgStep] );
209 for( j = 1; j < imageWidth - 1; j++ )
211 MemY[0][j] = MemY[1][j] = CONV( imgA[j - 1], imgA[j], imgA[j + 1] );
215 for( i = 1; i < imageHeight - 1; i++ )
217 MemX[0][i] = MemX[1][i] = CONV( imgA[pixNumber - imgStep],
218 imgA[pixNumber], imgA[pixNumber + imgStep] );
219 pixNumber += imgStep;
222 MemY[0][imageWidth - 1] =
223 MemY[1][imageWidth - 1] = CONV( imgA[imageWidth - 2],
224 imgA[imageWidth - 1], imgA[imageWidth - 1] );
226 MemX[0][imageHeight - 1] =
227 MemX[1][imageHeight - 1] = CONV( imgA[pixNumber - imgStep],
228 imgA[pixNumber], imgA[pixNumber] );
231 /****************************************************************************************\
232 * begin scan image, calc derivatives *
233 \****************************************************************************************/
238 LastLine = imgStep * (imageHeight - 1);
239 while( ConvLine < imageHeight )
241 /*Here we calculate derivatives for line of image */
242 int memYline = (ConvLine + 1) & 1;
245 Line1 = Line2 - ((Line2 == 0) ? 0 : imgStep);
246 Line3 = Line2 + ((Line2 == LastLine) ? 0 : imgStep);
248 /* Process first pixel */
249 ConvX = CONV( imgA[Line1 + 1], imgA[Line2 + 1], imgA[Line3 + 1] );
250 ConvY = CONV( imgA[Line3], imgA[Line3], imgA[Line3 + 1] );
252 GradY = (ConvY - MemY[memYline][0]) * 0.125f;
253 GradX = (ConvX - MemX[1][ConvLine]) * 0.125f;
255 MemY[memYline][0] = ConvY;
256 MemX[1][ConvLine] = ConvX;
258 GradT = (float) (imgB[Line2] - imgA[Line2]);
260 II[address].xx = GradX * GradX;
261 II[address].xy = GradX * GradY;
262 II[address].yy = GradY * GradY;
263 II[address].xt = GradX * GradT;
264 II[address].yt = GradY * GradT;
266 II[address].alpha = 1 / (Ilambda + II[address].xx + II[address].yy);
269 /* Process middle of line */
270 for( j = 1; j < imageWidth - 1; j++ )
272 ConvX = CONV( imgA[Line1 + j + 1], imgA[Line2 + j + 1], imgA[Line3 + j + 1] );
273 ConvY = CONV( imgA[Line3 + j - 1], imgA[Line3 + j], imgA[Line3 + j + 1] );
275 GradY = (ConvY - MemY[memYline][j]) * 0.125f;
276 GradX = (ConvX - MemX[(j - 1) & 1][ConvLine]) * 0.125f;
278 MemY[memYline][j] = ConvY;
279 MemX[(j - 1) & 1][ConvLine] = ConvX;
281 GradT = (float) (imgB[Line2 + j] - imgA[Line2 + j]);
283 II[address].xx = GradX * GradX;
284 II[address].xy = GradX * GradY;
285 II[address].yy = GradY * GradY;
286 II[address].xt = GradX * GradT;
287 II[address].yt = GradY * GradT;
289 II[address].alpha = 1 / (Ilambda + II[address].xx + II[address].yy);
292 /* Process last pixel of line */
293 ConvX = CONV( imgA[Line1 + imageWidth - 1], imgA[Line2 + imageWidth - 1],
294 imgA[Line3 + imageWidth - 1] );
296 ConvY = CONV( imgA[Line3 + imageWidth - 2], imgA[Line3 + imageWidth - 1],
297 imgA[Line3 + imageWidth - 1] );
300 GradY = (ConvY - MemY[memYline][imageWidth - 1]) * 0.125f;
301 GradX = (ConvX - MemX[(imageWidth - 2) & 1][ConvLine]) * 0.125f;
303 MemY[memYline][imageWidth - 1] = ConvY;
305 GradT = (float) (imgB[Line2 + imageWidth - 1] - imgA[Line2 + imageWidth - 1]);
307 II[address].xx = GradX * GradX;
308 II[address].xy = GradX * GradY;
309 II[address].yy = GradY * GradY;
310 II[address].xt = GradX * GradT;
311 II[address].yt = GradY * GradT;
313 II[address].alpha = 1 / (Ilambda + II[address].xx + II[address].yy);
318 /****************************************************************************************\
319 * Prepare initial approximation *
320 \****************************************************************************************/
323 float *vx = velocityX;
324 float *vy = velocityY;
326 for( i = 0; i < imageHeight; i++ )
328 memset( vx, 0, imageWidth * sizeof( float ));
329 memset( vy, 0, imageWidth * sizeof( float ));
335 /****************************************************************************************\
336 * Perform iterations *
337 \****************************************************************************************/
340 LastLine = velStep * (imageHeight - 1);
347 /****************************************************************************************\
348 * begin scan velocity and update it *
349 \****************************************************************************************/
351 for( i = 0; i < imageHeight; i++ )
353 /* Here average velocity */
360 Line1 = Line2 - ((Line2 == 0) ? 0 : velStep);
361 Line3 = Line2 + ((Line2 == LastLine) ? 0 : velStep);
362 /* Process first pixel */
363 averageX = (velocityX[Line2] +
364 velocityX[Line2 + 1] + velocityX[Line1] + velocityX[Line3]) / 4;
366 averageY = (velocityY[Line2] +
367 velocityY[Line2 + 1] + velocityY[Line1] + velocityY[Line3]) / 4;
369 VelBufX[i & 1][0] = averageX -
370 (II[address].xx * averageX +
371 II[address].xy * averageY + II[address].xt) * II[address].alpha;
373 VelBufY[i & 1][0] = averageY -
374 (II[address].xy * averageX +
375 II[address].yy * averageY + II[address].yt) * II[address].alpha;
378 if( criteria.type & CV_TERMCRIT_EPS )
380 tmp = (float)fabs(velocityX[Line2] - VelBufX[i & 1][0]);
381 Eps = MAX( tmp, Eps );
382 tmp = (float)fabs(velocityY[Line2] - VelBufY[i & 1][0]);
383 Eps = MAX( tmp, Eps );
386 /* Process middle of line */
387 for( j = 1; j < imageWidth - 1; j++ )
389 averageX = (velocityX[Line2 + j - 1] +
390 velocityX[Line2 + j + 1] +
391 velocityX[Line1 + j] + velocityX[Line3 + j]) / 4;
392 averageY = (velocityY[Line2 + j - 1] +
393 velocityY[Line2 + j + 1] +
394 velocityY[Line1 + j] + velocityY[Line3 + j]) / 4;
396 VelBufX[i & 1][j] = averageX -
397 (II[address].xx * averageX +
398 II[address].xy * averageY + II[address].xt) * II[address].alpha;
400 VelBufY[i & 1][j] = averageY -
401 (II[address].xy * averageX +
402 II[address].yy * averageY + II[address].yt) * II[address].alpha;
404 if( criteria.type & CV_TERMCRIT_EPS )
406 tmp = (float)fabs(velocityX[Line2 + j] - VelBufX[i & 1][j]);
407 Eps = MAX( tmp, Eps );
408 tmp = (float)fabs(velocityY[Line2 + j] - VelBufY[i & 1][j]);
409 Eps = MAX( tmp, Eps );
413 /* Process last pixel of line */
414 averageX = (velocityX[Line2 + imageWidth - 2] +
415 velocityX[Line2 + imageWidth - 1] +
416 velocityX[Line1 + imageWidth - 1] +
417 velocityX[Line3 + imageWidth - 1]) / 4;
419 averageY = (velocityY[Line2 + imageWidth - 2] +
420 velocityY[Line2 + imageWidth - 1] +
421 velocityY[Line1 + imageWidth - 1] +
422 velocityY[Line3 + imageWidth - 1]) / 4;
425 VelBufX[i & 1][imageWidth - 1] = averageX -
426 (II[address].xx * averageX +
427 II[address].xy * averageY + II[address].xt) * II[address].alpha;
429 VelBufY[i & 1][imageWidth - 1] = averageY -
430 (II[address].xy * averageX +
431 II[address].yy * averageY + II[address].yt) * II[address].alpha;
434 if( criteria.type & CV_TERMCRIT_EPS )
436 tmp = (float)fabs(velocityX[Line2 + imageWidth - 1] -
437 VelBufX[i & 1][imageWidth - 1]);
438 Eps = MAX( tmp, Eps );
439 tmp = (float)fabs(velocityY[Line2 + imageWidth - 1] -
440 VelBufY[i & 1][imageWidth - 1]);
441 Eps = MAX( tmp, Eps );
445 /* store new velocity from old buffer to velocity frame */
448 memcpy( &velocityX[Line1], VelBufX[(i - 1) & 1], imageWidth * sizeof( float ));
449 memcpy( &velocityY[Line1], VelBufY[(i - 1) & 1], imageWidth * sizeof( float ));
452 /* store new velocity from old buffer to velocity frame */
453 memcpy( &velocityX[imageWidth * (imageHeight - 1)],
454 VelBufX[(imageHeight - 1) & 1], imageWidth * sizeof( float ));
456 memcpy( &velocityY[imageWidth * (imageHeight - 1)],
457 VelBufY[(imageHeight - 1) & 1], imageWidth * sizeof( float ));
459 if( (criteria.type & CV_TERMCRIT_ITER) && (iter == criteria.max_iter) )
461 if( (criteria.type & CV_TERMCRIT_EPS) && (Eps < criteria.epsilon) )
465 for( k = 0; k < 2; k++ )
469 cvFree( &VelBufX[k] );
470 cvFree( &VelBufY[k] );
475 } /*icvCalcOpticalFlowHS_8u32fR*/
478 /*F///////////////////////////////////////////////////////////////////////////////////////
479 // Name: cvCalcOpticalFlowHS
480 // Purpose: Optical flow implementation
483 // srcA, srcB - source image
484 // velx, vely - destination image
490 cvCalcOpticalFlowHS( const void* srcarrA, const void* srcarrB, int usePrevious,
491 void* velarrx, void* velarry,
492 double lambda, CvTermCriteria criteria )
494 CvMat stubA, *srcA = cvGetMat( srcarrA, &stubA );
495 CvMat stubB, *srcB = cvGetMat( srcarrB, &stubB );
496 CvMat stubx, *velx = cvGetMat( velarrx, &stubx );
497 CvMat stuby, *vely = cvGetMat( velarry, &stuby );
499 if( !CV_ARE_TYPES_EQ( srcA, srcB ))
500 CV_Error( CV_StsUnmatchedFormats, "Source images have different formats" );
502 if( !CV_ARE_TYPES_EQ( velx, vely ))
503 CV_Error( CV_StsUnmatchedFormats, "Destination images have different formats" );
505 if( !CV_ARE_SIZES_EQ( srcA, srcB ) ||
506 !CV_ARE_SIZES_EQ( velx, vely ) ||
507 !CV_ARE_SIZES_EQ( srcA, velx ))
508 CV_Error( CV_StsUnmatchedSizes, "" );
510 if( CV_MAT_TYPE( srcA->type ) != CV_8UC1 ||
511 CV_MAT_TYPE( velx->type ) != CV_32FC1 )
512 CV_Error( CV_StsUnsupportedFormat, "Source images must have 8uC1 type and "
513 "destination images must have 32fC1 type" );
515 if( srcA->step != srcB->step || velx->step != vely->step )
516 CV_Error( CV_BadStep, "source and destination images have different step" );
518 IPPI_CALL( icvCalcOpticalFlowHS_8u32fR( (uchar*)srcA->data.ptr, (uchar*)srcB->data.ptr,
519 srcA->step, cvGetMatSize( srcA ), usePrevious,
520 velx->data.fl, vely->data.fl,
521 velx->step, (float)lambda, criteria ));