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.
46 intersect( CvPoint2D32f pt, CvSize win_size, CvSize img_size,
47 CvPoint * min_pt, CvPoint * max_pt )
51 ipt.x = cvFloor( pt.x );
52 ipt.y = cvFloor( pt.y );
54 ipt.x -= win_size.width;
55 ipt.y -= win_size.height;
57 win_size.width = win_size.width * 2 + 1;
58 win_size.height = win_size.height * 2 + 1;
60 min_pt->x = MAX( 0, -ipt.x );
61 min_pt->y = MAX( 0, -ipt.y );
62 max_pt->x = MIN( win_size.width, img_size.width - ipt.x );
63 max_pt->y = MIN( win_size.height, img_size.height - ipt.y );
68 icvInitPyramidalAlgorithm( uchar * imgA, uchar * imgB,
69 int imgStep, CvSize imgSize,
70 uchar * pyrA, uchar * pyrB,
72 CvTermCriteria * criteria,
73 int max_iters, int flags,
74 uchar *** imgI, uchar *** imgJ,
75 int **step, CvSize** size,
76 double **scale, uchar ** buffer )
78 uchar *pyr_down_temp_buffer = 0;
79 CvStatus result = CV_OK;
80 int pyrBytes, bufferBytes = 0;
81 int level1 = level + 1;
92 /* check input arguments */
94 return CV_NULLPTR_ERR;
96 if( (flags & CV_LKFLOW_PYR_A_READY) != 0 && !pyrA ||
97 (flags & CV_LKFLOW_PYR_B_READY) != 0 && !pyrB )
98 return CV_BADFLAG_ERR;
101 return CV_BADRANGE_ERR;
103 /*if( imgSize.width % (1 << level) != 0 ||
104 imgSize.height % (1 << level) != 0 ||
105 (imgSize.width >> level) == 0 || (imgSize.height >> level) == 0 )
106 return CV_BADSIZE_ERR;*/
108 switch (criteria->type)
110 case CV_TERMCRIT_ITER:
111 criteria->epsilon = 0.f;
113 case CV_TERMCRIT_EPS:
114 criteria->maxIter = max_iters;
116 case CV_TERMCRIT_ITER | CV_TERMCRIT_EPS:
120 return CV_BADFLAG_ERR;
123 /* compare squared values */
124 criteria->epsilon *= criteria->epsilon;
126 /* set pointers and step for every level */
133 for( i = 1; i < level1; i++ )
135 levelSize.width = (levelSize.width + 1) >> 1;
136 levelSize.height = (levelSize.height + 1) >> 1;
138 int tstep = icvAlign(levelSize.width,ALIGN) * sizeof( imgA[0] );
139 pyrBytes += tstep * levelSize.height;
142 assert( pyrBytes <= imgSize.width * imgSize.height * (int) sizeof( imgA[0] ) * 4 / 3 );
144 /* buffer_size = <size for patches> + <size for pyramids> */
145 bufferBytes = (level1 >= 0) * ((pyrA == 0) + (pyrB == 0)) * pyrBytes +
146 (sizeof( imgI[0][0] ) * 2 + sizeof( step[0][0] ) +
147 sizeof(size[0][0]) + sizeof( scale[0][0] )) * level1;
149 *buffer = (uchar *) icvAlloc( bufferBytes );
151 return CV_OUTOFMEM_ERR;
153 *imgI = (uchar **) buffer[0];
154 *imgJ = *imgI + level1;
155 *step = (int *) (*imgJ + level1);
156 *scale = (double *) (*step + level1);
157 *size = (CvSize *)(*scale + level1);
161 step[0][0] = imgStep;
163 size[0][0] = imgSize;
167 uchar *bufPtr = (uchar *) (*size + level1);
170 int pyr_down_buffer_size = 0;
181 icvPyrDownGetBufSize_Gauss5x5( imgSize.width, cv8u, 1, &pyr_down_buffer_size );
182 pyr_down_temp_buffer = (uchar *) icvAlloc( pyr_down_buffer_size );
186 /* build pyramids for both frames */
187 for( i = 1; i <= level; i++ )
190 CvSize srcSize = levelSize;
192 levelSize.width = (levelSize.width + 1) >> 1;
193 levelSize.height = (levelSize.height + 1) >> 1;
195 size[0][i] = levelSize;
196 step[0][i] = icvAlign( levelSize.width, ALIGN ) * sizeof( imgA[0] );
197 scale[0][i] = scale[0][i - 1] * 0.5;
199 levelBytes = step[0][i] * levelSize.height;
200 imgI[0][i] = (uchar *) ptrA;
204 srcSize.height &= -2;
206 if( !(flags & CV_LKFLOW_PYR_A_READY) )
208 result = icvPyrDown_Gauss5x5_8u_C1R( imgI[0][i - 1], step[0][i - 1],
209 imgI[0][i], step[0][i],
210 srcSize, pyr_down_temp_buffer );
213 icvPyrDownBorder_8u_CnR( imgI[0][i - 1], step[0][i - 1], size[0][i-1],
214 imgI[0][i], step[0][i], size[0][i], 1 );
217 imgJ[0][i] = (uchar *) ptrB;
220 if( !(flags & CV_LKFLOW_PYR_B_READY) )
222 result = icvPyrDown_Gauss5x5_8u_C1R( imgJ[0][i - 1], step[0][i - 1],
223 imgJ[0][i], step[0][i],
224 srcSize, pyr_down_temp_buffer );
227 icvPyrDownBorder_8u_CnR( imgJ[0][i - 1], step[0][i - 1], size[0][i-1],
228 imgJ[0][i], step[0][i], size[0][i], 1 );
234 icvFree( &pyr_down_temp_buffer );
240 /*F///////////////////////////////////////////////////////////////////////////////////////
241 // Name: icvCalcOpticalFlowPyrLK_8uC1R ( Lucas & Kanade method,
242 // modification that uses pyramids )
244 // Calculates optical flow between two images for certain set of points.
247 // imgA - pointer to first frame (time t)
248 // imgB - pointer to second frame (time t+1)
249 // imgStep - full width of the source images in bytes
250 // imgSize - size of the source images
251 // pyrA - buffer for pyramid for the first frame.
252 // if the pointer is not NULL, the buffer must have size enough to
253 // store pyramid (from level 1 to level #<level> (see below))
254 // (imgSize.width*imgSize.height/3 will be enough)).
255 // pyrB - similar to pyrA, but for the second frame.
257 // for both parameters above the following rules work:
258 // If pointer is 0, the function allocates the buffer internally,
259 // calculates pyramid and releases the buffer after processing.
260 // Else (it should be large enough then) the function calculates
261 // pyramid and stores it in the buffer unless the
262 // CV_LKFLOW_PYR_A[B]_READY flag is set. In both cases
263 // (flag is set or not) the subsequent calls may reuse the calculated
264 // pyramid by setting CV_LKFLOW_PYR_A[B]_READY.
266 // featuresA - array of points, for which the flow needs to be found
267 // count - number of feature points
268 // winSize - size of search window on each pyramid level
269 // level - maximal pyramid level number
270 // (if 0, pyramids are not used (single level),
271 // if 1, two levels are used etc.)
273 // next parameters are arrays of <count> elements.
274 // ------------------------------------------------------
275 // featuresB - array of 2D points, containing calculated
276 // new positions of input features (in the second image).
277 // status - array, every element of which will be set to 1 if the flow for the
278 // corresponding feature has been found, 0 else.
279 // error - array of double numbers, containing difference between
280 // patches around the original and moved points
281 // (it is optional parameter, can be NULL).
282 // ------------------------------------------------------
283 // criteria - specifies when to stop the iteration process of finding flow
284 // for each point on each pyramid level
286 // flags - miscellaneous flags:
287 // CV_LKFLOW_PYR_A_READY - pyramid for the first frame
288 // is precalculated before call
289 // CV_LKFLOW_PYR_B_READY - pyramid for the second frame
290 // is precalculated before call
291 // CV_LKFLOW_INITIAL_GUESSES - featuresB array holds initial
292 // guesses about new features'
293 // locations before function call.
294 // Returns: CV_OK - all ok
295 // CV_OUTOFMEM_ERR - insufficient memory for function work
296 // CV_NULLPTR_ERR - if one of input pointers is NULL
297 // CV_BADSIZE_ERR - wrong input sizes interrelation
299 // Notes: For calculating spatial derivatives 3x3 Sobel operator is used.
300 // The values of pixels beyond the image are determined using replication mode.
302 static CvStatus icvCalcOpticalFlowPyrLK_8uC1R( uchar * imgA,
308 CvPoint2D32f * featuresA,
309 CvPoint2D32f * featuresB,
315 CvTermCriteria criteria, int flags )
319 #define MAX_ITERS 100
321 static const float kerX[] = { -1, 0, 1 }, kerY[] =
323 0.09375, 0.3125, 0.09375}; /* 3/32, 10/32, 3/32 */
325 uchar *pyr_buffer = 0;
343 CvSize patchSize = cvSize( winSize.width * 2 + 1, winSize.height * 2 + 1 );
344 int patchLen = patchSize.width * patchSize.height;
345 int patchStep = patchSize.width * sizeof( patchI[0] );
347 CvSize srcPatchSize = cvSize( patchSize.width + 2, patchSize.height + 2 );
348 int srcPatchLen = srcPatchSize.width * srcPatchSize.height;
349 int srcPatchStep = srcPatchSize.width * sizeof( patchI[0] );
351 CvStatus result = CV_OK;
353 /* check input arguments */
354 if( !featuresA || !featuresB )
355 return CV_NULLPTR_ERR;
356 if( winSize.width <= 1 || winSize.height <= 1 )
357 return CV_BADSIZE_ERR;
359 if( (flags & ~7) != 0 )
360 return CV_BADFLAG_ERR;
362 return CV_BADRANGE_ERR;
364 result = icvInitPyramidalAlgorithm( imgA, imgB, imgStep, imgSize,
365 pyrA, pyrB, level, &criteria, MAX_ITERS, flags,
366 &imgI, &imgJ, &step, &size, &scale, &pyr_buffer );
371 /* buffer_size = <size for patches> + <size for pyramids> */
372 bufferBytes = (srcPatchLen + patchLen * 3) * sizeof( patchI[0] );
374 buffer = (uchar *) icvAlloc( bufferBytes );
377 result = CV_OUTOFMEM_ERR;
381 patchI = (float *) buffer;
382 patchJ = patchI + srcPatchLen;
383 Ix = patchJ + patchLen;
386 memset( status, 1, count );
388 if( !(flags & CV_LKFLOW_INITIAL_GUESSES) )
390 memcpy( featuresB, featuresA, count * sizeof( featuresA[0] ));
393 /* find flow for each given point */
394 for( i = 0; i < count; i++ )
397 CvPoint minI, maxI, minJ, maxJ;
401 minI = maxI = minJ = maxJ = cvPoint( 0, 0 );
403 v.x = (float) (featuresB[i].x * scale[level] * 0.5);
404 v.y = (float) (featuresB[i].y * scale[level] * 0.5);
406 /* do processing from top pyramid level (smallest image)
407 to the bottom (original image) */
408 for( l = level; l >= 0; l-- )
411 CvSize levelSize = size[l];
416 u.x = (float) (featuresA[i].x * scale[l]);
417 u.y = (float) (featuresA[i].y * scale[l]);
419 if( icvGetRectSubPix_8u32f_C1R( imgI[l], step[l], levelSize,
420 patchI, srcPatchStep, srcPatchSize, u ) < 0 )
422 /* point is outside the image. take the next */
428 icvSepConvSmall3_32f( patchI, srcPatchStep, Ix, patchStep,
429 srcPatchSize, kerX, kerY, patchJ );
432 icvSepConvSmall3_32f( patchI, srcPatchStep, Iy, patchStep,
433 srcPatchSize, kerY, kerX, patchJ );
435 /* repack patchI (remove borders) */
436 for( k = 0; k < patchSize.height; k++ )
437 memcpy( patchI + k * patchSize.width,
438 patchI + (k + 1) * srcPatchSize.width + 1, patchStep );
440 intersect( u, winSize, levelSize, &minI, &maxI );
442 for( j = 0; j < criteria.maxIter; j++ )
444 double bx = 0, by = 0;
446 double Gxx = 0, Gxy = 0, Gyy = 0;
449 if( icvGetRectSubPix_8u32f_C1R( imgJ[l], step[l], levelSize,
450 patchJ, patchStep, patchSize, v ) < 0 )
452 /* point is outside image. take the next */
457 intersect( v, winSize, levelSize, &minJ, &maxJ );
459 minJ.x = MAX( minJ.x, minI.x );
460 minJ.y = MAX( minJ.y, minI.y );
462 maxJ.x = MIN( maxJ.x, maxI.x );
463 maxJ.y = MIN( maxJ.y, maxI.y );
465 for( y = minJ.y; y < maxJ.y; y++ )
467 for( x = minJ.x; x < maxJ.x; x++ )
469 int idx = y * (winSize.width * 2 + 1) + x;
470 double t = patchI[idx] - patchJ[idx];
472 bx += (double) (t * Ix[idx]);
473 by += (double) (t * Iy[idx]);
474 Gxx += Ix[idx] * Ix[idx];
475 Gxy += Ix[idx] * Iy[idx];
476 Gyy += Iy[idx] * Iy[idx];
480 D = Gxx * Gyy - Gxy * Gxy;
481 if( D < DBL_EPSILON )
489 mx = (float) ((Gyy * bx - Gxy * by) * D);
490 my = (float) ((Gxx * by - Gxy * bx) * D);
495 if( mx * mx + my * my < criteria.epsilon )
512 for( y = minJ.y; y < maxJ.y; y++ )
514 for( x = minJ.x; x < maxJ.x; x++ )
516 int idx = y * (winSize.width * 2 + 1) + x;
517 double t = patchI[idx] - patchJ[idx];
522 error[i] = (float) sqrt( err );
527 status[i] = (char) pt_status;
532 icvFree( &pyr_buffer );
541 #define MAX_ITERS 100
543 static const float kerX[] = { -1, 0, 1 }, kerY[] =
545 0.09375, 0.3125, 0.09375}; /* 3/32, 10/32, 3/32 */
547 uchar *pyr_buffer = 0;
565 CvSize patchSize = cvSize( winSize.width * 2 + 1, winSize.height * 2 + 1 );
566 int patchLen = patchSize.width * patchSize.height;
567 int patchStep = patchSize.width * sizeof( patchI[0] );
569 CvSize srcPatchSize = cvSize( patchSize.width + 2, patchSize.height + 2 );
570 int srcPatchLen = srcPatchSize.width * srcPatchSize.height;
571 int srcPatchStep = srcPatchSize.width * sizeof( patchI[0] );
573 CvStatus result = CV_OK;
575 /* check input arguments */
576 if( !featuresA || !featuresB )
577 return CV_NULLPTR_ERR;
578 if( winSize.width <= 1 || winSize.height <= 1 )
579 return CV_BADSIZE_ERR;
581 if( (flags & ~7) != 0 )
582 return CV_BADFLAG_ERR;
584 return CV_BADRANGE_ERR;
586 result = icvInitPyramidalAlgorithm( imgA, imgB, imgStep, imgSize,
587 pyrA, pyrB, level, &criteria, MAX_ITERS, flags,
588 &imgI, &imgJ, &step, &size, &scale, &pyr_buffer );
593 /* buffer_size = <size for patches> + <size for pyramids> */
594 bufferBytes = (srcPatchLen + patchLen * 3) * sizeof( patchI[0] );
596 buffer = (uchar *) icvAlloc( bufferBytes );
599 result = CV_OUTOFMEM_ERR;
603 patchI = (float *) buffer;
604 patchJ = patchI + srcPatchLen;
605 Ix = patchJ + patchLen;
609 memset( status, 1, count );
611 if( !(flags & CV_LKFLOW_INITIAL_GUESSES) )
613 memcpy( featuresB, featuresA, count * sizeof( featuresA[0] ));
616 /* find flow for each given point */
617 for( i = 0; i < count; i++ )
622 //CvPoint minI, maxI, minJ, maxJ;
626 //minI = maxI = minJ = maxJ = cvPoint(0,0);
628 v.x = (float) (featuresB[i].x * scale[level] * 0.5);
629 v.y = (float) (featuresB[i].y * scale[level] * 0.5);
631 /* do processing from top pyramid level (smallest image)
632 to the bottom (original image) */
633 for( l = level; l >= 0; l-- )
636 CvSize levelSize = size[l];
637 double Gxx, Gxy, Gyy, D;
644 u.x = (float) (featuresA[i].x * scale[l]);
645 u.y = (float) (featuresA[i].y * scale[l]);
647 if( icvGetRectSubPix_8u32f_C1R( imgI[l], step[l], levelSize,
648 patchI, srcPatchStep, srcPatchSize, u ) < 0 )
650 /* point is outside the image. take the next */
656 icvSepConvSmall3_32f( patchI, srcPatchStep, Ix, patchStep,
657 srcPatchSize, kerX, kerY, patchJ );
660 icvSepConvSmall3_32f( patchI, srcPatchStep, Iy, patchStep,
661 srcPatchSize, kerY, kerX, patchJ );
663 /* repack patchI (remove borders) */
664 for( k = 0; k < patchSize.height; k++ )
665 memcpy( patchI + k * patchSize.width,
666 patchI + (k + 1) * srcPatchSize.width + 1, patchStep );
668 for( y = 0, k = 0; y < patchSize.height; y++ )
670 for( x = 0; x < patchSize.width; x++, k++ )
672 Gxx += Ix[k] * Ix[k];
673 Gxy += Ix[k] * Iy[k];
674 Gyy += Iy[k] * Iy[k];
678 D = Gxx * Gyy - Gxy * Gxy;
679 if( D < DBL_EPSILON )
692 //intersect( u, winSize, levelSize, &minI, &maxI );
694 for( j = 0; j < criteria.maxIter; j++ )
696 double bx = 0, by = 0;
699 if( icvGetRectSubPix_8u32f_C1R( imgJ[l], step[l], levelSize,
700 patchJ, patchStep, patchSize, v ) < 0 )
702 /* point is outside image. take the next */
707 for( k = 0; k < patchLen; k++ )
709 double t = patchI[k] - patchJ[k];
711 bx += (double) (t * Ix[k]);
712 by += (double) (t * Iy[k]);
715 mx = (float) (Gxx * bx + Gxy * by);
716 my = (float) (Gxy * bx + Gyy * by);
721 if( mx * mx + my * my < criteria.epsilon )
738 for( k = 0; k < patchLen; k++ )
740 double t = patchI[k] - patchJ[k];
744 error[i] = (float) sqrt( err );
749 status[i] = (char) pt_status;
754 icvFree( &pyr_buffer );
763 /* Affine tracking algorithm */
764 static CvStatus icvCalcAffineFlowPyrLK_8uC1R( uchar * imgA, uchar * imgB,
765 int imgStep, CvSize imgSize,
766 uchar * pyrA, uchar * pyrB,
767 CvPoint2D32f * featuresA,
768 CvPoint2D32f * featuresB,
769 float *matrices, int count,
770 CvSize winSize, int level,
771 char *status, float *error,
772 CvTermCriteria criteria, int flags )
775 #define MAX_ITERS 100
777 static const float kerX[] = { -1, 0, 1 }, kerY[] =
779 0.09375, 0.3125, 0.09375}; /* 3/32, 10/32, 3/32 */
782 uchar *pyr_buffer = 0;
799 CvSize patchSize = cvSize( winSize.width * 2 + 1, winSize.height * 2 + 1 );
800 int patchLen = patchSize.width * patchSize.height;
801 int patchStep = patchSize.width * sizeof( patchI[0] );
803 CvSize srcPatchSize = cvSize( patchSize.width + 2, patchSize.height + 2 );
804 int srcPatchLen = srcPatchSize.width * srcPatchSize.height;
805 int srcPatchStep = srcPatchSize.width * sizeof( patchI[0] );
807 CvStatus result = CV_OK;
809 /* check input arguments */
810 if( !featuresA || !featuresB || !matrices )
811 return CV_NULLPTR_ERR;
812 if( winSize.width <= 1 || winSize.height <= 1 )
813 return CV_BADSIZE_ERR;
815 if( (flags & ~7) != 0 )
816 return CV_BADFLAG_ERR;
818 return CV_BADRANGE_ERR;
820 result = icvInitPyramidalAlgorithm( imgA, imgB, imgStep, imgSize,
821 pyrA, pyrB, level, &criteria, MAX_ITERS, flags,
822 &imgI, &imgJ, &step, &size, &scale, &pyr_buffer );
827 /* buffer_size = <size for patches> + <size for pyramids> */
828 bufferBytes = (srcPatchLen + patchLen * 3) * sizeof( patchI[0] ) +
830 (36 * 2 + 6) * sizeof( double );
832 buffer = (uchar *) icvAlloc( bufferBytes );
835 result = CV_OUTOFMEM_ERR;
839 patchI = (float *) buffer;
840 patchJ = patchI + srcPatchLen;
841 Ix = patchJ + patchLen;
845 memset( status, 1, count );
847 if( !(flags & CV_LKFLOW_INITIAL_GUESSES) )
849 memcpy( featuresB, featuresA, count * sizeof( featuresA[0] ));
850 for( i = 0; i < count * 4; i += 4 )
852 matrices[i] = matrices[i + 2] = 1.f;
853 matrices[i + 1] = matrices[i + 3] = 0.f;
857 /* find flow for each given point */
858 for( i = 0; i < count; i++ )
866 memcpy( A, matrices + i * 4, sizeof( A ));
868 v.x = (float) (featuresB[i].x * scale[level] * 0.5);
869 v.y = (float) (featuresB[i].y * scale[level] * 0.5);
871 /* do processing from top pyramid level (smallest image)
872 to the bottom (original image) */
873 for( l = level; l >= 0; l-- )
876 CvSize levelSize = size[l];
882 u.x = (float) (featuresA[i].x * scale[l]);
883 u.y = (float) (featuresA[i].y * scale[l]);
885 if( icvGetRectSubPix_8u32f_C1R( imgI[l], step[l], levelSize,
886 patchI, srcPatchStep, srcPatchSize, u ) < 0 )
888 /* point is outside the image. take the next */
894 icvSepConvSmall3_32f( patchI, srcPatchStep, Ix, patchStep,
895 srcPatchSize, kerX, kerY, patchJ );
898 icvSepConvSmall3_32f( patchI, srcPatchStep, Iy, patchStep,
899 srcPatchSize, kerY, kerX, patchJ );
901 /* repack patchI (remove borders) */
902 for( k = 0; k < patchSize.height; k++ )
903 memcpy( patchI + k * patchSize.width,
904 patchI + (k + 1) * srcPatchSize.width + 1, patchStep );
906 memset( G, 0, sizeof( G ));
908 /* calculate G matrix */
909 for( y = -winSize.height, k = 0; y <= winSize.height; y++ )
911 for( x = -winSize.width; x <= winSize.width; x++, k++ )
913 double ixix = ((double) Ix[k]) * Ix[k];
914 double ixiy = ((double) Ix[k]) * Iy[k];
915 double iyiy = ((double) Iy[k]) * Iy[k];
938 // G[13] == G[8] == G[4]
971 // fill part of G below its diagonal
972 for( y = 1; y < 6; y++ )
973 for( x = 0; x < y; x++ )
974 G[y * 6 + x] = G[x * 6 + y];
977 cvInitMatHeader( &mat, 6, 6, CV_64FC1, G );
979 if( cvInvert( &mat, &mat, CV_SVD ) < 1e-3 )
981 /* bad matrix. take the next point */
986 for( j = 0; j < criteria.maxIter; j++ )
989 double t0, t1, s = 0;
991 if( icvGetQuadrangleSubPix_8u32f_C1R( imgJ[l], step[l], levelSize,
992 patchJ, patchStep, patchSize, A,
999 memset( b, 0, sizeof( b ));
1001 for( y = -winSize.height, k = 0; y <= winSize.height; y++ )
1003 for( x = -winSize.width; x <= winSize.width; x++, k++ )
1005 double t = patchI[k] - patchJ[k];
1006 double ixt = Ix[k] * t;
1007 double iyt = Iy[k] * t;
1020 icvTransformVector_64d( G, b, eta, 6, 6 );
1022 t0 = v.x + A[0] * eta[0] + A[1] * eta[1];
1023 t1 = v.y + A[2] * eta[0] + A[3] * eta[1];
1025 assert( fabs( t0 ) < levelSize.width * 2 );
1026 assert( fabs( t1 ) < levelSize.height * 2 );
1031 t0 = A[0] * (1 + eta[2]) + A[1] * eta[4];
1032 t1 = A[0] * eta[3] + A[1] * (1 + eta[5]);
1036 t0 = A[2] * (1 + eta[2]) + A[3] * eta[4];
1037 t1 = A[2] * eta[3] + A[3] * (1 + eta[5]);
1041 /*t0 = 4./(fabs(A[0]) + fabs(A[1]) + fabs(A[2]) + fabs(A[3]) + DBL_EPSILON);
1042 A[0] = (float)(A[0]*t0);
1043 A[1] = (float)(A[1]*t0);
1044 A[2] = (float)(A[2]*t0);
1045 A[3] = (float)(A[3]*t0);
1047 t0 = fabs(A[0]*A[2] - A[1]*A[3]);
1049 A[0] = (float)(A[0]*t0);
1050 A[1] = (float)(A[1]*t0);
1051 A[2] = (float)(A[2]*t0);
1052 A[3] = (float)(A[3]*t0); */
1054 if( eta[0] * eta[0] + eta[1] * eta[1] < criteria.epsilon )
1059 if( pt_status == 0 )
1066 memcpy( matrices + i * 4, A, sizeof( A ));
1073 for( y = 0, k = 0; y < patchSize.height; y++ )
1075 for( x = 0; x < patchSize.width; x++, k++ )
1077 double t = patchI[k] - patchJ[k];
1081 error[i] = (float) sqrt( err );
1086 status[i] = (char) pt_status;
1091 icvFree( &pyr_buffer );
1099 static int icvMinimalPyramidSize( CvSize img_size )
1101 return icvAlign(img_size.width,8) * img_size.height / 3;
1106 cvCalcOpticalFlowPyrLK( const void* arrA, const void* arrB,
1107 void* pyrarrA, void* pyrarrB,
1108 CvPoint2D32f * featuresA,
1109 CvPoint2D32f * featuresB,
1110 int count, CvSize winSize, int level,
1111 char *status, float *error,
1112 CvTermCriteria criteria, int flags )
1114 CV_FUNCNAME( "cvCalcOpticalFlowPyrLK" );
1118 CvMat stubA, *imgA = (CvMat*)arrA;
1119 CvMat stubB, *imgB = (CvMat*)arrB;
1120 CvMat pstubA, *pyrA = (CvMat*)pyrarrA;
1121 CvMat pstubB, *pyrB = (CvMat*)pyrarrB;
1124 CV_CALL( imgA = cvGetMat( imgA, &stubA ));
1125 CV_CALL( imgB = cvGetMat( imgB, &stubB ));
1127 if( CV_MAT_TYPE( imgA->type ) != CV_8UC1 )
1128 CV_ERROR( CV_StsUnsupportedFormat, "" );
1130 if( !CV_ARE_TYPES_EQ( imgA, imgB ))
1131 CV_ERROR( CV_StsUnmatchedFormats, "" );
1133 if( !CV_ARE_SIZES_EQ( imgA, imgB ))
1134 CV_ERROR( CV_StsUnmatchedSizes, "" );
1136 if( imgA->step != imgB->step )
1137 CV_ERROR( CV_StsUnmatchedSizes, "imgA and imgB must have equal steps" );
1139 img_size = icvGetMatSize( imgA );
1143 CV_CALL( pyrA = cvGetMat( pyrA, &pstubA ));
1145 if( pyrA->step*pyrA->height < icvMinimalPyramidSize( img_size ) )
1146 CV_ERROR( CV_StsBadArg, "pyramid A has insufficient size" );
1157 CV_CALL( pyrB = cvGetMat( pyrB, &pstubB ));
1159 if( pyrB->step*pyrB->height < icvMinimalPyramidSize( img_size ) )
1160 CV_ERROR( CV_StsBadArg, "pyramid B has insufficient size" );
1168 IPPI_CALL( icvCalcOpticalFlowPyrLK_8uC1R( imgA->data.ptr, imgB->data.ptr, imgA->step,
1169 img_size, pyrA->data.ptr, pyrB->data.ptr,
1170 featuresA, featuresB,
1171 count, winSize, level, status,
1172 error, criteria, flags ));
1179 cvCalcAffineFlowPyrLK( const void* arrA, const void* arrB,
1180 void* pyrarrA, void* pyrarrB,
1181 CvPoint2D32f * featuresA,
1182 CvPoint2D32f * featuresB,
1183 float *matrices, int count,
1184 CvSize winSize, int level,
1185 char *status, float *error,
1186 CvTermCriteria criteria, int flags )
1188 CV_FUNCNAME( "cvCalcAffineFlowPyrLK" );
1192 CvMat stubA, *imgA = (CvMat*)arrA;
1193 CvMat stubB, *imgB = (CvMat*)arrB;
1194 CvMat pstubA, *pyrA = (CvMat*)pyrarrA;
1195 CvMat pstubB, *pyrB = (CvMat*)pyrarrB;
1198 CV_CALL( imgA = cvGetMat( imgA, &stubA ));
1199 CV_CALL( imgB = cvGetMat( imgB, &stubB ));
1201 if( CV_MAT_TYPE( imgA->type ) != CV_8UC1 )
1202 CV_ERROR( CV_StsUnsupportedFormat, "" );
1204 if( !CV_ARE_TYPES_EQ( imgA, imgB ))
1205 CV_ERROR( CV_StsUnmatchedFormats, "" );
1207 if( !CV_ARE_SIZES_EQ( imgA, imgB ))
1208 CV_ERROR( CV_StsUnmatchedSizes, "" );
1210 if( imgA->step != imgB->step )
1211 CV_ERROR( CV_StsUnmatchedSizes, "imgA and imgB must have equal steps" );
1214 CV_ERROR( CV_StsNullPtr, "" );
1216 img_size = icvGetMatSize( imgA );
1220 CV_CALL( pyrA = cvGetMat( pyrA, &pstubA ));
1222 if( pyrA->step*pyrA->height < icvMinimalPyramidSize( img_size ) )
1223 CV_ERROR( CV_StsBadArg, "pyramid A has insufficient size" );
1234 CV_CALL( pyrB = cvGetMat( pyrB, &pstubB ));
1236 if( pyrB->step*pyrB->height < icvMinimalPyramidSize( img_size ) )
1237 CV_ERROR( CV_StsBadArg, "pyramid B has insufficient size" );
1245 IPPI_CALL( icvCalcAffineFlowPyrLK_8uC1R( imgA->data.ptr, imgB->data.ptr, imgA->step,
1246 img_size, pyrA->data.ptr, pyrB->data.ptr,
1247 featuresA, featuresB, matrices,
1248 count, winSize, level, status,
1249 error, criteria, flags ));