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 imgSize,
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, imgSize.width - ipt.x );
63 max_pt->y = MIN( win_size.height, imgSize.height - ipt.y );
67 static int icvMinimalPyramidSize( CvSize imgSize )
69 return cvAlign(imgSize.width,8) * imgSize.height / 3;
74 icvInitPyramidalAlgorithm( const CvMat* imgA, const CvMat* imgB,
75 CvMat* pyrA, CvMat* pyrB,
76 int level, CvTermCriteria * criteria,
77 int max_iters, int flags,
78 uchar *** imgI, uchar *** imgJ,
79 int **step, CvSize** size,
80 double **scale, uchar ** buffer )
82 CV_FUNCNAME( "icvInitPyramidalAlgorithm" );
87 int pyrBytes, bufferBytes = 0, elem_size;
88 int level1 = level + 1;
91 CvSize imgSize, levelSize;
99 /* check input arguments */
100 if( ((flags & CV_LKFLOW_PYR_A_READY) != 0 && !pyrA) ||
101 ((flags & CV_LKFLOW_PYR_B_READY) != 0 && !pyrB) )
102 CV_ERROR( CV_StsNullPtr, "Some of the precomputed pyramids are missing" );
105 CV_ERROR( CV_StsOutOfRange, "The number of pyramid layers is negative" );
107 switch( criteria->type )
109 case CV_TERMCRIT_ITER:
110 criteria->epsilon = 0.f;
112 case CV_TERMCRIT_EPS:
113 criteria->max_iter = max_iters;
115 case CV_TERMCRIT_ITER | CV_TERMCRIT_EPS:
119 CV_ERROR( CV_StsBadArg, "Invalid termination criteria" );
122 /* compare squared values */
123 criteria->epsilon *= criteria->epsilon;
125 /* set pointers and step for every level */
128 imgSize = cvGetSize(imgA);
129 elem_size = CV_ELEM_SIZE(imgA->type);
132 for( i = 1; i < level1; i++ )
134 levelSize.width = (levelSize.width + 1) >> 1;
135 levelSize.height = (levelSize.height + 1) >> 1;
137 int tstep = cvAlign(levelSize.width,ALIGN) * elem_size;
138 pyrBytes += tstep * levelSize.height;
141 assert( pyrBytes <= imgSize.width * imgSize.height * elem_size * 4 / 3 );
143 /* buffer_size = <size for patches> + <size for pyramids> */
144 bufferBytes = (int)((level1 >= 0) * ((pyrA->data.ptr == 0) +
145 (pyrB->data.ptr == 0)) * pyrBytes +
146 (sizeof(imgI[0][0]) * 2 + sizeof(step[0][0]) +
147 sizeof(size[0][0]) + sizeof(scale[0][0])) * level1);
149 CV_CALL( *buffer = (uchar *)cvAlloc( bufferBytes ));
151 *imgI = (uchar **) buffer[0];
152 *imgJ = *imgI + level1;
153 *step = (int *) (*imgJ + level1);
154 *scale = (double *) (*step + level1);
155 *size = (CvSize *)(*scale + level1);
157 imgI[0][0] = imgA->data.ptr;
158 imgJ[0][0] = imgB->data.ptr;
159 step[0][0] = imgA->step;
161 size[0][0] = imgSize;
165 uchar *bufPtr = (uchar *) (*size + level1);
166 uchar *ptrA = pyrA->data.ptr;
167 uchar *ptrB = pyrB->data.ptr;
180 /* build pyramids for both frames */
181 for( i = 1; i <= level; i++ )
184 CvMat prev_level, next_level;
186 levelSize.width = (levelSize.width + 1) >> 1;
187 levelSize.height = (levelSize.height + 1) >> 1;
189 size[0][i] = levelSize;
190 step[0][i] = cvAlign( levelSize.width, ALIGN ) * elem_size;
191 scale[0][i] = scale[0][i - 1] * 0.5;
193 levelBytes = step[0][i] * levelSize.height;
194 imgI[0][i] = (uchar *) ptrA;
197 if( !(flags & CV_LKFLOW_PYR_A_READY) )
199 prev_level = cvMat( size[0][i-1].height, size[0][i-1].width, CV_8UC1 );
200 next_level = cvMat( size[0][i].height, size[0][i].width, CV_8UC1 );
201 cvSetData( &prev_level, imgI[0][i-1], step[0][i-1] );
202 cvSetData( &next_level, imgI[0][i], step[0][i] );
203 cvPyrDown( &prev_level, &next_level );
206 imgJ[0][i] = (uchar *) ptrB;
209 if( !(flags & CV_LKFLOW_PYR_B_READY) )
211 prev_level = cvMat( size[0][i-1].height, size[0][i-1].width, CV_8UC1 );
212 next_level = cvMat( size[0][i].height, size[0][i].width, CV_8UC1 );
213 cvSetData( &prev_level, imgJ[0][i-1], step[0][i-1] );
214 cvSetData( &next_level, imgJ[0][i], step[0][i] );
215 cvPyrDown( &prev_level, &next_level );
224 /* compute dI/dx and dI/dy */
226 icvCalcIxIy_32f( const float* src, int src_step, float* dstX, float* dstY, int dst_step,
227 CvSize src_size, const float* smooth_k, float* buffer0 )
229 int src_width = src_size.width, dst_width = src_size.width-2;
230 int x, height = src_size.height - 2;
231 float* buffer1 = buffer0 + src_width;
233 src_step /= sizeof(src[0]);
234 dst_step /= sizeof(dstX[0]);
236 for( ; height--; src += src_step, dstX += dst_step, dstY += dst_step )
238 const float* src2 = src + src_step;
239 const float* src3 = src + src_step*2;
241 for( x = 0; x < src_width; x++ )
243 float t0 = (src3[x] + src[x])*smooth_k[0] + src2[x]*smooth_k[1];
244 float t1 = src3[x] - src[x];
245 buffer0[x] = t0; buffer1[x] = t1;
248 for( x = 0; x < dst_width; x++ )
250 float t0 = buffer0[x+2] - buffer0[x];
251 float t1 = (buffer1[x] + buffer1[x+2])*smooth_k[0] + buffer1[x+1]*smooth_k[1];
252 dstX[x] = t0; dstY[x] = t1;
259 cvCalcOpticalFlowPyrLK( const void* arrA, const void* arrB,
260 void* pyrarrA, void* pyrarrB,
261 const CvPoint2D32f * featuresA,
262 CvPoint2D32f * featuresB,
263 int count, CvSize winSize, int level,
264 char *status, float *error,
265 CvTermCriteria criteria, int flags )
267 uchar *pyrBuffer = 0;
272 CV_FUNCNAME( "cvCalcOpticalFlowPyrLK" );
276 const int MAX_ITERS = 100;
278 CvMat stubA, *imgA = (CvMat*)arrA;
279 CvMat stubB, *imgB = (CvMat*)arrB;
280 CvMat pstubA, *pyrA = (CvMat*)pyrarrA;
281 CvMat pstubB, *pyrB = (CvMat*)pyrarrB;
283 static const float smoothKernel[] = { 0.09375, 0.3125, 0.09375 }; /* 3/32, 10/32, 3/32 */
292 int threadCount = cvGetNumThreads();
293 float* _patchI[CV_MAX_THREADS];
294 float* _patchJ[CV_MAX_THREADS];
295 float* _Ix[CV_MAX_THREADS];
296 float* _Iy[CV_MAX_THREADS];
300 CvSize patchSize = cvSize( winSize.width * 2 + 1, winSize.height * 2 + 1 );
301 int patchLen = patchSize.width * patchSize.height;
302 int srcPatchLen = (patchSize.width + 2)*(patchSize.height + 2);
304 CV_CALL( imgA = cvGetMat( imgA, &stubA ));
305 CV_CALL( imgB = cvGetMat( imgB, &stubB ));
307 if( CV_MAT_TYPE( imgA->type ) != CV_8UC1 )
308 CV_ERROR( CV_StsUnsupportedFormat, "" );
310 if( !CV_ARE_TYPES_EQ( imgA, imgB ))
311 CV_ERROR( CV_StsUnmatchedFormats, "" );
313 if( !CV_ARE_SIZES_EQ( imgA, imgB ))
314 CV_ERROR( CV_StsUnmatchedSizes, "" );
316 if( imgA->step != imgB->step )
317 CV_ERROR( CV_StsUnmatchedSizes, "imgA and imgB must have equal steps" );
319 imgSize = cvGetMatSize( imgA );
323 CV_CALL( pyrA = cvGetMat( pyrA, &pstubA ));
325 if( pyrA->step*pyrA->height < icvMinimalPyramidSize( imgSize ) )
326 CV_ERROR( CV_StsBadArg, "pyramid A has insufficient size" );
336 CV_CALL( pyrB = cvGetMat( pyrB, &pstubB ));
338 if( pyrB->step*pyrB->height < icvMinimalPyramidSize( imgSize ) )
339 CV_ERROR( CV_StsBadArg, "pyramid B has insufficient size" );
350 if( !featuresA || !featuresB )
351 CV_ERROR( CV_StsNullPtr, "Some of arrays of point coordinates are missing" );
354 CV_ERROR( CV_StsOutOfRange, "The number of tracked points is negative or zero" );
356 if( winSize.width <= 1 || winSize.height <= 1 )
357 CV_ERROR( CV_StsBadSize, "Invalid search window size" );
359 for( i = 0; i < threadCount; i++ )
360 _patchI[i] = _patchJ[i] = _Ix[i] = _Iy[i] = 0;
362 CV_CALL( icvInitPyramidalAlgorithm( imgA, imgB, pyrA, pyrB,
363 level, &criteria, MAX_ITERS, flags,
364 &imgI, &imgJ, &step, &size, &scale, &pyrBuffer ));
367 CV_CALL( status = _status = (char*)cvAlloc( count*sizeof(_status[0]) ));
369 /* buffer_size = <size for patches> + <size for pyramids> */
370 bufferBytes = (srcPatchLen + patchLen * 3) * sizeof( _patchI[0][0] ) * threadCount;
371 CV_CALL( buffer = (uchar*)cvAlloc( bufferBytes ));
373 for( i = 0; i < threadCount; i++ )
375 _patchI[i] = i == 0 ? (float*)buffer : _Iy[i-1] + patchLen;
376 _patchJ[i] = _patchI[i] + srcPatchLen;
377 _Ix[i] = _patchJ[i] + patchLen;
378 _Iy[i] = _Ix[i] + patchLen;
381 memset( status, 1, count );
383 memset( error, 0, count*sizeof(error[0]) );
385 if( !(flags & CV_LKFLOW_INITIAL_GUESSES) )
386 memcpy( featuresB, featuresA, count*sizeof(featuresA[0]));
388 /* do processing from top pyramid level (smallest image)
389 to the bottom (original image) */
390 for( l = level; l >= 0; l-- )
392 CvSize levelSize = size[l];
393 int levelStep = step[l];
397 #pragma omp parallel for num_threads(threadCount) schedule(dynamic)
399 /* find flow for each given point */
400 for( i = 0; i < count; i++ )
403 CvPoint minI, maxI, minJ, maxJ;
407 CvPoint prev_minJ = { -1, -1 }, prev_maxJ = { -1, -1 };
408 double Gxx = 0, Gxy = 0, Gyy = 0, D = 0, minEig = 0;
409 float prev_mx = 0, prev_my = 0;
411 int threadIdx = cvGetThreadNum();
412 float* patchI = _patchI[threadIdx];
413 float* patchJ = _patchJ[threadIdx];
414 float* Ix = _Ix[threadIdx];
415 float* Iy = _Iy[threadIdx];
417 v.x = featuresB[i].x;
418 v.y = featuresB[i].y;
426 v.x = (float)(v.x * scale[l]);
427 v.y = (float)(v.y * scale[l]);
430 pt_status = status[i];
434 minI = maxI = minJ = maxJ = cvPoint( 0, 0 );
436 u.x = (float) (featuresA[i].x * scale[l]);
437 u.y = (float) (featuresA[i].y * scale[l]);
439 intersect( u, winSize, levelSize, &minI, &maxI );
440 isz = jsz = cvSize(maxI.x - minI.x + 2, maxI.y - minI.y + 2);
441 u.x += (minI.x - (patchSize.width - maxI.x + 1))*0.5f;
442 u.y += (minI.y - (patchSize.height - maxI.y + 1))*0.5f;
444 if( isz.width < 3 || isz.height < 3 ||
445 icvGetRectSubPix_8u32f_C1R( imgI[l], levelStep, levelSize,
446 patchI, isz.width*sizeof(patchI[0]), isz, u ) < 0 )
448 /* point is outside the image. take the next */
453 icvCalcIxIy_32f( patchI, isz.width*sizeof(patchI[0]), Ix, Iy,
454 (isz.width-2)*sizeof(patchI[0]), isz, smoothKernel, patchJ );
456 for( j = 0; j < criteria.max_iter; j++ )
458 double bx = 0, by = 0;
462 intersect( v, winSize, levelSize, &minJ, &maxJ );
464 minJ.x = MAX( minJ.x, minI.x );
465 minJ.y = MAX( minJ.y, minI.y );
467 maxJ.x = MIN( maxJ.x, maxI.x );
468 maxJ.y = MIN( maxJ.y, maxI.y );
470 jsz = cvSize(maxJ.x - minJ.x, maxJ.y - minJ.y);
472 _v.x = v.x + (minJ.x - (patchSize.width - maxJ.x + 1))*0.5f;
473 _v.y = v.y + (minJ.y - (patchSize.height - maxJ.y + 1))*0.5f;
475 if( jsz.width < 1 || jsz.height < 1 ||
476 icvGetRectSubPix_8u32f_C1R( imgJ[l], levelStep, levelSize, patchJ,
477 jsz.width*sizeof(patchJ[0]), jsz, _v ) < 0 )
479 /* point is outside image. take the next */
484 if( maxJ.x == prev_maxJ.x && maxJ.y == prev_maxJ.y &&
485 minJ.x == prev_minJ.x && minJ.y == prev_minJ.y )
487 for( y = 0; y < jsz.height; y++ )
489 const float* pi = patchI +
490 (y + minJ.y - minI.y + 1)*isz.width + minJ.x - minI.x + 1;
491 const float* pj = patchJ + y*jsz.width;
492 const float* ix = Ix +
493 (y + minJ.y - minI.y)*(isz.width-2) + minJ.x - minI.x;
494 const float* iy = Iy + (ix - Ix);
496 for( x = 0; x < jsz.width; x++ )
498 double t0 = pi[x] - pj[x];
507 for( y = 0; y < jsz.height; y++ )
509 const float* pi = patchI +
510 (y + minJ.y - minI.y + 1)*isz.width + minJ.x - minI.x + 1;
511 const float* pj = patchJ + y*jsz.width;
512 const float* ix = Ix +
513 (y + minJ.y - minI.y)*(isz.width-2) + minJ.x - minI.x;
514 const float* iy = Iy + (ix - Ix);
516 for( x = 0; x < jsz.width; x++ )
518 double t = pi[x] - pj[x];
519 bx += (double) (t * ix[x]);
520 by += (double) (t * iy[x]);
521 Gxx += ix[x] * ix[x];
522 Gxy += ix[x] * iy[x];
523 Gyy += iy[x] * iy[x];
527 D = Gxx * Gyy - Gxy * Gxy;
528 if( D < DBL_EPSILON )
534 // Adi Shavit - 2008.05
535 if( flags & CV_LKFLOW_GET_MIN_EIGENVALS )
536 minEig = (Gyy + Gxx - sqrt((Gxx-Gyy)*(Gxx-Gyy) + 4.*Gxy*Gxy))/(2*jsz.height*jsz.width);
544 mx = (float) ((Gyy * bx - Gxy * by) * D);
545 my = (float) ((Gxx * by - Gxy * bx) * D);
550 if( mx * mx + my * my < criteria.epsilon )
553 if( j > 0 && fabs(mx + prev_mx) < 0.01 && fabs(my + prev_my) < 0.01 )
564 status[i] = (char)pt_status;
565 if( l == 0 && error && pt_status )
569 if( flags & CV_LKFLOW_GET_MIN_EIGENVALS )
573 for( y = 0; y < jsz.height; y++ )
575 const float* pi = patchI +
576 (y + minJ.y - minI.y + 1)*isz.width + minJ.x - minI.x + 1;
577 const float* pj = patchJ + y*jsz.width;
579 for( x = 0; x < jsz.width; x++ )
581 double t = pi[x] - pj[x];
587 error[i] = (float)err;
589 } // end of point processing loop (i)
591 } // end of pyramid levels loop (l)
595 cvFree( &pyrBuffer );
602 /* Affine tracking algorithm */
605 cvCalcAffineFlowPyrLK( const void* arrA, const void* arrB,
606 void* pyrarrA, void* pyrarrB,
607 const CvPoint2D32f * featuresA,
608 CvPoint2D32f * featuresB,
609 float *matrices, int count,
610 CvSize winSize, int level,
611 char *status, float *error,
612 CvTermCriteria criteria, int flags )
614 const int MAX_ITERS = 100;
618 uchar *pyr_buffer = 0;
620 CV_FUNCNAME( "cvCalcAffineFlowPyrLK" );
624 CvMat stubA, *imgA = (CvMat*)arrA;
625 CvMat stubB, *imgB = (CvMat*)arrB;
626 CvMat pstubA, *pyrA = (CvMat*)pyrarrA;
627 CvMat pstubB, *pyrB = (CvMat*)pyrarrB;
629 static const float smoothKernel[] = { 0.09375, 0.3125, 0.09375 }; /* 3/32, 10/32, 3/32 */
646 CvSize patchSize = cvSize( winSize.width * 2 + 1, winSize.height * 2 + 1 );
647 int patchLen = patchSize.width * patchSize.height;
648 int patchStep = patchSize.width * sizeof( patchI[0] );
650 CvSize srcPatchSize = cvSize( patchSize.width + 2, patchSize.height + 2 );
651 int srcPatchLen = srcPatchSize.width * srcPatchSize.height;
652 int srcPatchStep = srcPatchSize.width * sizeof( patchI[0] );
654 float eps = (float)MIN(winSize.width, winSize.height);
656 CV_CALL( imgA = cvGetMat( imgA, &stubA ));
657 CV_CALL( imgB = cvGetMat( imgB, &stubB ));
659 if( CV_MAT_TYPE( imgA->type ) != CV_8UC1 )
660 CV_ERROR( CV_StsUnsupportedFormat, "" );
662 if( !CV_ARE_TYPES_EQ( imgA, imgB ))
663 CV_ERROR( CV_StsUnmatchedFormats, "" );
665 if( !CV_ARE_SIZES_EQ( imgA, imgB ))
666 CV_ERROR( CV_StsUnmatchedSizes, "" );
668 if( imgA->step != imgB->step )
669 CV_ERROR( CV_StsUnmatchedSizes, "imgA and imgB must have equal steps" );
672 CV_ERROR( CV_StsNullPtr, "" );
674 imgSize = cvGetMatSize( imgA );
678 CV_CALL( pyrA = cvGetMat( pyrA, &pstubA ));
680 if( pyrA->step*pyrA->height < icvMinimalPyramidSize( imgSize ) )
681 CV_ERROR( CV_StsBadArg, "pyramid A has insufficient size" );
691 CV_CALL( pyrB = cvGetMat( pyrB, &pstubB ));
693 if( pyrB->step*pyrB->height < icvMinimalPyramidSize( imgSize ) )
694 CV_ERROR( CV_StsBadArg, "pyramid B has insufficient size" );
705 /* check input arguments */
706 if( !featuresA || !featuresB || !matrices )
707 CV_ERROR( CV_StsNullPtr, "" );
709 if( winSize.width <= 1 || winSize.height <= 1 )
710 CV_ERROR( CV_StsOutOfRange, "the search window is too small" );
713 CV_ERROR( CV_StsOutOfRange, "" );
715 CV_CALL( icvInitPyramidalAlgorithm( imgA, imgB,
716 pyrA, pyrB, level, &criteria, MAX_ITERS, flags,
717 &imgI, &imgJ, &step, &size, &scale, &pyr_buffer ));
719 /* buffer_size = <size for patches> + <size for pyramids> */
720 bufferBytes = (srcPatchLen + patchLen*3)*sizeof(patchI[0]) + (36*2 + 6)*sizeof(double);
722 CV_CALL( buffer = (uchar*)cvAlloc(bufferBytes));
725 CV_CALL( status = _status = (char*)cvAlloc(count) );
727 patchI = (float *) buffer;
728 patchJ = patchI + srcPatchLen;
729 Ix = patchJ + patchLen;
733 memset( status, 1, count );
735 if( !(flags & CV_LKFLOW_INITIAL_GUESSES) )
737 memcpy( featuresB, featuresA, count * sizeof( featuresA[0] ));
738 for( i = 0; i < count * 4; i += 4 )
740 matrices[i] = matrices[i + 3] = 1.f;
741 matrices[i + 1] = matrices[i + 2] = 0.f;
745 for( i = 0; i < count; i++ )
747 featuresB[i].x = (float)(featuresB[i].x * scale[level] * 0.5);
748 featuresB[i].y = (float)(featuresB[i].y * scale[level] * 0.5);
751 /* do processing from top pyramid level (smallest image)
752 to the bottom (original image) */
753 for( l = level; l >= 0; l-- )
755 CvSize levelSize = size[l];
756 int levelStep = step[l];
758 /* find flow for each given point at the particular level */
759 for( i = 0; i < count; i++ )
764 double meanI = 0, meanJ = 0;
766 int pt_status = status[i];
772 Av[0] = matrices[i*4];
773 Av[1] = matrices[i*4+1];
774 Av[3] = matrices[i*4+2];
775 Av[4] = matrices[i*4+3];
777 Av[2] = featuresB[i].x += featuresB[i].x;
778 Av[5] = featuresB[i].y += featuresB[i].y;
780 u.x = (float) (featuresA[i].x * scale[l]);
781 u.y = (float) (featuresA[i].y * scale[l]);
783 if( u.x < -eps || u.x >= levelSize.width+eps ||
784 u.y < -eps || u.y >= levelSize.height+eps ||
785 icvGetRectSubPix_8u32f_C1R( imgI[l], levelStep,
786 levelSize, patchI, srcPatchStep, srcPatchSize, u ) < 0 )
788 /* point is outside the image. take the next */
794 icvCalcIxIy_32f( patchI, srcPatchStep, Ix, Iy,
795 (srcPatchSize.width-2)*sizeof(patchI[0]), srcPatchSize,
796 smoothKernel, patchJ );
798 /* repack patchI (remove borders) */
799 for( k = 0; k < patchSize.height; k++ )
800 memcpy( patchI + k * patchSize.width,
801 patchI + (k + 1) * srcPatchSize.width + 1, patchStep );
803 memset( G, 0, sizeof( G ));
805 /* calculate G matrix */
806 for( y = -winSize.height, k = 0; y <= winSize.height; y++ )
808 for( x = -winSize.width; x <= winSize.width; x++, k++ )
810 double ixix = ((double) Ix[k]) * Ix[k];
811 double ixiy = ((double) Ix[k]) * Iy[k];
812 double iyiy = ((double) Iy[k]) * Iy[k];
835 // G[13] == G[8] == G[4]
866 meanI /= patchSize.width*patchSize.height;
872 // fill part of G below its diagonal
873 for( y = 1; y < 6; y++ )
874 for( x = 0; x < y; x++ )
875 G[y * 6 + x] = G[x * 6 + y];
877 cvInitMatHeader( &mat, 6, 6, CV_64FC1, G );
879 if( cvInvert( &mat, &mat, CV_SVD ) < 1e-4 )
881 /* bad matrix. take the next point */
887 for( j = 0; j < criteria.max_iter; j++ )
889 double b[6] = {0,0,0,0,0,0}, eta[6];
890 double t0, t1, s = 0;
892 if( Av[2] < -eps || Av[2] >= levelSize.width+eps ||
893 Av[5] < -eps || Av[5] >= levelSize.height+eps ||
894 icvGetQuadrangleSubPix_8u32f_C1R( imgJ[l], levelStep,
895 levelSize, patchJ, patchStep, patchSize, Av ) < 0 )
901 for( y = -winSize.height, k = 0, meanJ = 0; y <= winSize.height; y++ )
902 for( x = -winSize.width; x <= winSize.width; x++, k++ )
905 meanJ = meanJ / (patchSize.width * patchSize.height) - meanI;
907 for( y = -winSize.height, k = 0; y <= winSize.height; y++ )
909 for( x = -winSize.width; x <= winSize.width; x++, k++ )
911 double t = patchI[k] - patchJ[k] + meanJ;
912 double ixt = Ix[k] * t;
913 double iyt = Iy[k] * t;
926 icvTransformVector_64d( G, b, eta, 6, 6 );
928 Av[2] = (float)(Av[2] + Av[0] * eta[0] + Av[1] * eta[1]);
929 Av[5] = (float)(Av[5] + Av[3] * eta[0] + Av[4] * eta[1]);
931 t0 = Av[0] * (1 + eta[2]) + Av[1] * eta[4];
932 t1 = Av[0] * eta[3] + Av[1] * (1 + eta[5]);
936 t0 = Av[3] * (1 + eta[2]) + Av[4] * eta[4];
937 t1 = Av[3] * eta[3] + Av[4] * (1 + eta[5]);
941 if( eta[0] * eta[0] + eta[1] * eta[1] < criteria.epsilon )
945 if( pt_status != 0 || l == 0 )
947 status[i] = (char)pt_status;
948 featuresB[i].x = Av[2];
949 featuresB[i].y = Av[5];
951 matrices[i*4] = Av[0];
952 matrices[i*4+1] = Av[1];
953 matrices[i*4+2] = Av[3];
954 matrices[i*4+3] = Av[4];
957 if( pt_status && l == 0 && error )
962 for( y = 0, k = 0; y < patchSize.height; y++ )
964 for( x = 0; x < patchSize.width; x++, k++ )
966 double t = patchI[k] - patchJ[k] + meanJ;
970 error[i] = (float)sqrt(err);
977 cvFree( &pyr_buffer );
985 icvGetRTMatrix( const CvPoint2D32f* a, const CvPoint2D32f* b,
986 int count, CvMat* M, int full_affine )
990 double sa[36], sb[6];
991 CvMat A = cvMat( 6, 6, CV_64F, sa ), B = cvMat( 6, 1, CV_64F, sb );
992 CvMat MM = cvMat( 6, 1, CV_64F, M->data.db );
996 memset( sa, 0, sizeof(sa) );
997 memset( sb, 0, sizeof(sb) );
999 for( i = 0; i < count; i++ )
1001 sa[0] += a[i].x*a[i].x;
1002 sa[1] += a[i].y*a[i].x;
1005 sa[6] += a[i].x*a[i].y;
1006 sa[7] += a[i].y*a[i].y;
1013 sb[0] += a[i].x*b[i].x;
1014 sb[1] += a[i].y*b[i].x;
1016 sb[3] += a[i].x*b[i].y;
1017 sb[4] += a[i].y*b[i].y;
1031 cvSolve( &A, &B, &MM, CV_SVD );
1035 double sa[16], sb[4], m[4], *om = M->data.db;
1036 CvMat A = cvMat( 4, 4, CV_64F, sa ), B = cvMat( 4, 1, CV_64F, sb );
1037 CvMat MM = cvMat( 4, 1, CV_64F, m );
1041 memset( sa, 0, sizeof(sa) );
1042 memset( sb, 0, sizeof(sb) );
1044 for( i = 0; i < count; i++ )
1046 sa[0] += a[i].x*a[i].x + a[i].y*a[i].y;
1052 sa[5] += a[i].x*a[i].x + a[i].y*a[i].y;
1066 sb[0] += a[i].x*b[i].x + a[i].y*b[i].y;
1067 sb[1] += a[i].x*b[i].y - a[i].y*b[i].x;
1072 cvSolve( &A, &B, &MM, CV_SVD );
1074 om[0] = om[4] = m[0];
1084 cvEstimateRigidTransform( const CvArr* _A, const CvArr* _B, CvMat* _M, int full_affine )
1088 const int COUNT = 15;
1089 const int WIDTH = 160, HEIGHT = 120;
1090 const int RANSAC_MAX_ITERS = 100;
1091 const int RANSAC_SIZE0 = 3;
1092 const double MIN_TRIANGLE_SIDE = 20;
1093 const double RANSAC_GOOD_RATIO = 0.5;
1096 CvMat *sA = 0, *sB = 0;
1097 CvPoint2D32f *pA = 0, *pB = 0;
1102 CV_FUNCNAME( "cvEstimateRigidTransform" );
1109 int cn, equal_sizes;
1111 int count_x, count_y, count;
1113 CvRNG rng = cvRNG(-1);
1115 CvMat M = cvMat( 2, 3, CV_64F, m );
1118 CV_CALL( A = cvGetMat( _A, &stubA ));
1119 CV_CALL( B = cvGetMat( _B, &stubB ));
1121 if( !CV_IS_MAT(_M) )
1122 CV_ERROR( _M ? CV_StsBadArg : CV_StsNullPtr, "Output parameter M is not a valid matrix" );
1124 if( !CV_ARE_SIZES_EQ( A, B ) )
1125 CV_ERROR( CV_StsUnmatchedSizes, "Both input images must have the same size" );
1127 if( !CV_ARE_TYPES_EQ( A, B ) )
1128 CV_ERROR( CV_StsUnmatchedFormats, "Both input images must have the same data type" );
1130 if( CV_MAT_TYPE(A->type) == CV_8UC1 || CV_MAT_TYPE(A->type) == CV_8UC3 )
1132 cn = CV_MAT_CN(A->type);
1134 sz1 = cvSize(WIDTH, HEIGHT);
1136 scale = MAX( (double)sz1.width/sz0.width, (double)sz1.height/sz0.height );
1137 scale = MIN( scale, 1. );
1138 sz1.width = cvRound( sz0.width * scale );
1139 sz1.height = cvRound( sz0.height * scale );
1141 equal_sizes = sz1.width == sz0.width && sz1.height == sz0.height;
1143 if( !equal_sizes || cn != 1 )
1145 CV_CALL( sA = cvCreateMat( sz1.height, sz1.width, CV_8UC1 ));
1146 CV_CALL( sB = cvCreateMat( sz1.height, sz1.width, CV_8UC1 ));
1148 if( !equal_sizes && cn != 1 )
1149 CV_CALL( gray = cvCreateMat( sz0.height, sz0.width, CV_8UC1 ));
1153 cvCvtColor( A, gray, CV_BGR2GRAY );
1154 cvResize( gray, sA, CV_INTER_AREA );
1155 cvCvtColor( B, gray, CV_BGR2GRAY );
1156 cvResize( gray, sB, CV_INTER_AREA );
1160 cvResize( gray, sA, CV_INTER_AREA );
1161 cvResize( gray, sB, CV_INTER_AREA );
1165 cvCvtColor( A, gray, CV_BGR2GRAY );
1166 cvResize( gray, sA, CV_INTER_AREA );
1167 cvCvtColor( B, gray, CV_BGR2GRAY );
1170 cvReleaseMat( &gray );
1176 count_x = cvRound((double)COUNT*sz1.width/sz1.height);
1177 count = count_x * count_y;
1179 CV_CALL( pA = (CvPoint2D32f*)cvAlloc( count*sizeof(pA[0]) ));
1180 CV_CALL( pB = (CvPoint2D32f*)cvAlloc( count*sizeof(pB[0]) ));
1181 CV_CALL( status = (char*)cvAlloc( count*sizeof(status[0]) ));
1183 for( i = 0, k = 0; i < count_y; i++ )
1184 for( j = 0; j < count_x; j++, k++ )
1186 pA[k].x = (j+0.5f)*sz1.width/count_x;
1187 pA[k].y = (i+0.5f)*sz1.height/count_y;
1190 // find the corresponding points in B
1191 cvCalcOpticalFlowPyrLK( A, B, 0, 0, pA, pB, count, cvSize(10,10), 3,
1192 status, 0, cvTermCriteria(CV_TERMCRIT_ITER,40,0.1), 0 );
1194 // repack the remained points
1195 for( i = 0, k = 0; i < count; i++ )
1208 else if( CV_MAT_TYPE(A->type) == CV_32FC2 || CV_MAT_TYPE(A->type) == CV_32SC2 )
1210 count = A->cols*A->rows;
1212 if( CV_IS_MAT_CONT(A->type & B->type) && CV_MAT_TYPE(A->type) == CV_32FC2 )
1214 pA = (CvPoint2D32f*)A->data.ptr;
1215 pB = (CvPoint2D32f*)B->data.ptr;
1222 CV_CALL( pA = (CvPoint2D32f*)cvAlloc( count*sizeof(pA[0]) ));
1223 CV_CALL( pB = (CvPoint2D32f*)cvAlloc( count*sizeof(pB[0]) ));
1224 _pA = cvMat( A->rows, A->cols, CV_32FC2, pA );
1225 _pB = cvMat( B->rows, B->cols, CV_32FC2, pB );
1226 cvConvert( A, &_pA );
1227 cvConvert( B, &_pB );
1231 CV_ERROR( CV_StsUnsupportedFormat, "Both input images must have either 8uC1 or 8uC3 type" );
1233 CV_CALL( good_idx = (int*)cvAlloc( count*sizeof(good_idx[0]) ));
1235 if( count < RANSAC_SIZE0 )
1239 // 1. find the consensus
1240 for( k = 0; k < RANSAC_MAX_ITERS; k++ )
1242 int idx[RANSAC_SIZE0];
1246 memset( a, 0, sizeof(a) );
1247 memset( b, 0, sizeof(b) );
1249 // choose random 3 non-complanar points from A & B
1250 for( i = 0; i < RANSAC_SIZE0; i++ )
1252 for( k1 = 0; k1 < RANSAC_MAX_ITERS; k1++ )
1254 idx[i] = cvRandInt(&rng) % count;
1256 for( j = 0; j < i; j++ )
1258 if( idx[j] == idx[i] )
1260 // check that the points are not very close one each other
1261 if( fabs(pA[idx[i]].x - pA[idx[j]].x) +
1262 fabs(pA[idx[i]].y - pA[idx[j]].y) < MIN_TRIANGLE_SIDE )
1264 if( fabs(pB[idx[i]].x - pB[idx[j]].x) +
1265 fabs(pB[idx[i]].y - pB[idx[j]].y) < MIN_TRIANGLE_SIDE )
1272 if( i+1 == RANSAC_SIZE0 )
1274 // additional check for non-complanar vectors
1283 if( fabs((a[1].x - a[0].x)*(a[2].y - a[0].y) - (a[1].y - a[0].y)*(a[2].x - a[0].x)) < 1 ||
1284 fabs((b[1].x - b[0].x)*(b[2].y - b[0].y) - (b[1].y - b[0].y)*(b[2].x - b[0].x)) < 1 )
1290 if( k1 >= RANSAC_MAX_ITERS )
1294 if( i < RANSAC_SIZE0 )
1297 // estimate the transformation using 3 points
1298 icvGetRTMatrix( a, b, 3, &M, full_affine );
1300 for( i = 0, good_count = 0; i < count; i++ )
1302 if( fabs( m[0]*pA[i].x + m[1]*pA[i].y + m[2] - pB[i].x ) +
1303 fabs( m[3]*pA[i].x + m[4]*pA[i].y + m[5] - pB[i].y ) < 8 )
1304 good_idx[good_count++] = i;
1307 if( good_count >= count*RANSAC_GOOD_RATIO )
1311 if( k >= RANSAC_MAX_ITERS )
1314 if( good_count < count )
1316 for( i = 0; i < good_count; i++ )
1324 icvGetRTMatrix( pA, pB, good_count, &M, full_affine );
1327 CV_CALL( cvConvert( &M, _M ));
1332 cvReleaseMat( &sA );
1333 cvReleaseMat( &sB );
1337 cvFree( &good_idx );
1338 cvReleaseMat( &gray );