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.
48 void calcOpticalFlowPyrLK( const Mat& prevImg, const Mat& nextImg,
49 const vector<Point2f>& prevPts,
50 vector<Point2f>& nextPts,
51 vector<uchar>& status, vector<float>& err,
52 Size winSize, int maxLevel,
53 TermCriteria criteria,
57 derivLambda = std::min(std::max(derivLambda, 0.), 1.);
58 double lambda1 = 1. - derivLambda, lambda2 = derivLambda;
59 const int derivKernelSize = 3;
60 const float deriv1Scale = 0.5f/4.f;
61 const float deriv2Scale = 0.25f/4.f;
62 const int derivDepth = CV_32F;
63 Point2f halfWin((winSize.width-1)*0.5f, (winSize.height-1)*0.5f);
65 CV_Assert( maxLevel >= 0 && winSize.width > 2 && winSize.height > 2 );
66 CV_Assert( prevImg.size() == nextImg.size() &&
67 prevImg.type() == nextImg.type() );
69 size_t npoints = prevPts.size();
70 nextPts.resize(npoints);
71 status.resize(npoints);
72 for( size_t i = 0; i < npoints; i++ )
79 vector<Mat> prevPyr, nextPyr;
81 int cn = prevImg.channels();
82 buildPyramid( prevImg, prevPyr, maxLevel );
83 buildPyramid( nextImg, nextPyr, maxLevel );
84 // I, dI/dx ~ Ix, dI/dy ~ Iy, d2I/dx2 ~ Ixx, d2I/dxdy ~ Ixy, d2I/dy2 ~ Iyy
85 Mat derivIBuf((prevImg.rows + winSize.height*2),
86 (prevImg.cols + winSize.width*2),
87 CV_MAKETYPE(derivDepth, cn*6));
88 // J, dJ/dx ~ Jx, dJ/dy ~ Jy
89 Mat derivJBuf((prevImg.rows + winSize.height*2),
90 (prevImg.cols + winSize.width*2),
91 CV_MAKETYPE(derivDepth, cn*3));
92 Mat tempDerivBuf(prevImg.size(), CV_MAKETYPE(derivIBuf.type(), cn));
93 Mat derivIWinBuf(winSize, derivIBuf.type());
95 if( (criteria.type & TermCriteria::COUNT) == 0 )
96 criteria.maxCount = 30;
98 criteria.maxCount = std::min(std::max(criteria.maxCount, 0), 100);
99 if( (criteria.type & TermCriteria::EPS) == 0 )
100 criteria.epsilon = 0.01;
102 criteria.epsilon = std::min(std::max(criteria.epsilon, 0.), 10.);
103 criteria.epsilon *= criteria.epsilon;
105 for( int level = maxLevel; level >= 0; level-- )
108 Size imgSize = prevPyr[level].size();
109 Mat tempDeriv( imgSize, tempDerivBuf.type(), tempDerivBuf.data );
110 Mat _derivI( imgSize.height + winSize.height*2,
111 imgSize.width + winSize.width*2,
112 derivIBuf.type(), derivIBuf.data );
113 Mat _derivJ( imgSize.height + winSize.height*2,
114 imgSize.width + winSize.width*2,
115 derivJBuf.type(), derivJBuf.data );
116 Mat derivI(_derivI, Rect(winSize.width, winSize.height, imgSize.width, imgSize.height));
117 Mat derivJ(_derivJ, Rect(winSize.width, winSize.height, imgSize.width, imgSize.height));
118 CvMat cvderivI = _derivI;
120 CvMat cvderivJ = _derivJ;
123 vector<int> fromTo(cn*2);
124 for( k = 0; k < cn; k++ )
127 prevPyr[level].convertTo(tempDeriv, derivDepth);
128 for( k = 0; k < cn; k++ )
130 mixChannels(&tempDeriv, 1, &derivI, 1, &fromTo[0], cn);
132 // compute spatial derivatives and merge them together
133 Sobel(prevPyr[level], tempDeriv, derivDepth, 1, 0, derivKernelSize, deriv1Scale );
134 for( k = 0; k < cn; k++ )
135 fromTo[k*2+1] = k*6 + 1;
136 mixChannels(&tempDeriv, 1, &derivI, 1, &fromTo[0], cn);
138 Sobel(prevPyr[level], tempDeriv, derivDepth, 0, 1, derivKernelSize, deriv1Scale );
139 for( k = 0; k < cn; k++ )
140 fromTo[k*2+1] = k*6 + 2;
141 mixChannels(&tempDeriv, 1, &derivI, 1, &fromTo[0], cn);
143 Sobel(prevPyr[level], tempDeriv, derivDepth, 2, 0, derivKernelSize, deriv2Scale );
144 for( k = 0; k < cn; k++ )
145 fromTo[k*2+1] = k*6 + 3;
146 mixChannels(&tempDeriv, 1, &derivI, 1, &fromTo[0], cn);
148 Sobel(prevPyr[level], tempDeriv, derivDepth, 1, 1, derivKernelSize, deriv2Scale );
149 for( k = 0; k < cn; k++ )
150 fromTo[k*2+1] = k*6 + 4;
151 mixChannels(&tempDeriv, 1, &derivI, 1, &fromTo[0], cn);
153 Sobel(prevPyr[level], tempDeriv, derivDepth, 0, 2, derivKernelSize, deriv2Scale );
154 for( k = 0; k < cn; k++ )
155 fromTo[k*2+1] = k*6 + 5;
156 mixChannels(&tempDeriv, 1, &derivI, 1, &fromTo[0], cn);
158 nextPyr[level].convertTo(tempDeriv, derivDepth);
159 for( k = 0; k < cn; k++ )
161 mixChannels(&tempDeriv, 1, &derivJ, 1, &fromTo[0], cn);
163 Sobel(nextPyr[level], tempDeriv, derivDepth, 1, 0, derivKernelSize, deriv1Scale );
164 for( k = 0; k < cn; k++ )
165 fromTo[k*2+1] = k*3 + 1;
166 mixChannels(&tempDeriv, 1, &derivJ, 1, &fromTo[0], cn);
168 Sobel(nextPyr[level], tempDeriv, derivDepth, 0, 1, derivKernelSize, deriv1Scale );
169 for( k = 0; k < cn; k++ )
170 fromTo[k*2+1] = k*3 + 2;
171 mixChannels(&tempDeriv, 1, &derivJ, 1, &fromTo[0], cn);
173 /*copyMakeBorder( derivI, _derivI, winSize.height, winSize.height,
174 winSize.width, winSize.width, BORDER_CONSTANT );
175 copyMakeBorder( derivJ, _derivJ, winSize.height, winSize.height,
176 winSize.width, winSize.width, BORDER_CONSTANT );*/
178 for( size_t ptidx = 0; ptidx < npoints; ptidx++ )
180 Point2f prevPt = prevPts[ptidx]*(float)(1./(1 << level));
182 if( level == maxLevel )
184 if( flags & OPTFLOW_USE_INITIAL_FLOW )
185 nextPt = nextPts[ptidx]*(float)(1./(1 << level));
190 nextPt = nextPts[ptidx]*2.f;
191 nextPts[ptidx] = nextPt;
193 Point2i iprevPt, inextPt;
195 iprevPt.x = cvFloor(prevPt.x);
196 iprevPt.y = cvFloor(prevPt.y);
198 if( iprevPt.x < -winSize.width || iprevPt.x >= derivI.cols ||
199 iprevPt.y < -winSize.height || iprevPt.y >= derivI.rows )
203 status[ptidx] = false;
204 err[ptidx] = FLT_MAX;
209 float a = prevPt.x - iprevPt.x;
210 float b = prevPt.y - iprevPt.y;
211 float w00 = (1.f - a)*(1.f - b), w01 = a*(1.f - b);
212 float w10 = (1.f - a)*b, w11 = a*b;
213 size_t stepI = derivI.step/derivI.elemSize1();
214 size_t stepJ = derivJ.step/derivJ.elemSize1();
215 int cnI = cn*6, cnJ = cn*3;
216 double A11 = 0, A12 = 0, A22 = 0;
217 double iA11 = 0, iA12 = 0, iA22 = 0;
219 // extract the patch from the first image
221 for( y = 0; y < winSize.height; y++ )
223 const float* src = (const float*)(derivI.data +
224 (y + iprevPt.y)*derivI.step) + iprevPt.x*cnI;
225 float* dst = (float*)(derivIWinBuf.data + y*derivIWinBuf.step);
227 for( x = 0; x < winSize.width*cnI; x += cnI, src += cnI )
229 float I = src[0]*w00 + src[cnI]*w01 + src[stepI]*w10 + src[stepI+cnI]*w11;
232 float Ix = src[1]*w00 + src[cnI+1]*w01 + src[stepI+1]*w10 + src[stepI+cnI+1]*w11;
233 float Iy = src[2]*w00 + src[cnI+2]*w01 + src[stepI+2]*w10 + src[stepI+cnI+2]*w11;
234 dst[x+1] = Ix; dst[x+2] = Iy;
236 float Ixx = src[3]*w00 + src[cnI+3]*w01 + src[stepI+3]*w10 + src[stepI+cnI+3]*w11;
237 float Ixy = src[4]*w00 + src[cnI+4]*w01 + src[stepI+4]*w10 + src[stepI+cnI+4]*w11;
238 float Iyy = src[5]*w00 + src[cnI+5]*w01 + src[stepI+5]*w10 + src[stepI+cnI+5]*w11;
239 dst[x+3] = Ixx; dst[x+4] = Ixy; dst[x+5] = Iyy;
241 iA11 += (double)Ix*Ix;
242 iA12 += (double)Ix*Iy;
243 iA22 += (double)Iy*Iy;
245 A11 += (double)Ixx*Ixx + (double)Ixy*Ixy;
246 A12 += Ixy*((double)Ixx + Iyy);
247 A22 += (double)Ixy*Ixy + (double)Iyy*Iyy;
251 A11 = lambda1*iA11 + lambda2*A11;
252 A12 = lambda1*iA12 + lambda2*A12;
253 A22 = lambda1*iA22 + lambda2*A22;
255 double D = A11*A22 - A12*A12;
256 double minEig = (A22 + A11 - std::sqrt((A11-A22)*(A11-A22) +
257 4.*A12*A12))/(2*winSize.width*winSize.height);
258 err[ptidx] = (float)minEig;
260 if( D < DBL_EPSILON )
263 status[ptidx] = false;
272 for( int j = 0; j < criteria.maxCount; j++ )
274 inextPt.x = cvFloor(nextPt.x);
275 inextPt.y = cvFloor(nextPt.y);
277 if( inextPt.x < -winSize.width || inextPt.x >= derivJ.cols ||
278 inextPt.y < -winSize.height || inextPt.y >= derivJ.rows )
281 status[ptidx] = false;
285 a = nextPt.x - inextPt.x;
286 b = nextPt.y - inextPt.y;
287 w00 = (1.f - a)*(1.f - b); w01 = a*(1.f - b);
288 w10 = (1.f - a)*b; w11 = a*b;
290 double b1 = 0, b2 = 0, ib1 = 0, ib2 = 0;
292 for( y = 0; y < winSize.height; y++ )
294 const float* src = (const float*)(derivJ.data +
295 (y + inextPt.y)*derivJ.step) + inextPt.x*cnJ;
296 const float* Ibuf = (float*)(derivIWinBuf.data + y*derivIWinBuf.step);
298 for( x = 0; x < winSize.width; x++, src += cnJ, Ibuf += cnI )
300 double It = src[0]*w00 + src[cnJ]*w01 + src[stepJ]*w10 +
301 src[stepJ+cnJ]*w11 - Ibuf[0];
302 double Ixt = src[1]*w00 + src[cnJ+1]*w01 + src[stepJ+1]*w10 +
303 src[stepJ+cnJ+1]*w11 - Ibuf[1];
304 double Iyt = src[2]*w00 + src[cnJ+2]*w01 + src[stepJ+2]*w10 +
305 src[stepJ+cnJ+2]*w11 - Ibuf[2];
306 b1 += Ixt*Ibuf[3] + Iyt*Ibuf[4];
307 b2 += Ixt*Ibuf[4] + Iyt*Ibuf[5];
313 b1 = lambda1*ib1 + lambda2*b1;
314 b2 = lambda1*ib2 + lambda2*b2;
315 Point2f delta( (float)((A12*b2 - A22*b1) * D),
316 (float)((A12*b1 - A11*b2) * D));
320 nextPts[ptidx] = nextPt + halfWin;
322 if( delta.ddot(delta) <= criteria.epsilon )
325 if( j > 0 && std::abs(delta.x + prevDelta.x) < 0.01 &&
326 std::abs(delta.y + prevDelta.y) < 0.01 )
328 nextPts[ptidx] -= delta*0.5f;
340 intersect( CvPoint2D32f pt, CvSize win_size, CvSize imgSize,
341 CvPoint* min_pt, CvPoint* max_pt )
345 ipt.x = cvFloor( pt.x );
346 ipt.y = cvFloor( pt.y );
348 ipt.x -= win_size.width;
349 ipt.y -= win_size.height;
351 win_size.width = win_size.width * 2 + 1;
352 win_size.height = win_size.height * 2 + 1;
354 min_pt->x = MAX( 0, -ipt.x );
355 min_pt->y = MAX( 0, -ipt.y );
356 max_pt->x = MIN( win_size.width, imgSize.width - ipt.x );
357 max_pt->y = MIN( win_size.height, imgSize.height - ipt.y );
361 static int icvMinimalPyramidSize( CvSize imgSize )
363 return cvAlign(imgSize.width,8) * imgSize.height / 3;
368 icvInitPyramidalAlgorithm( const CvMat* imgA, const CvMat* imgB,
369 CvMat* pyrA, CvMat* pyrB,
370 int level, CvTermCriteria * criteria,
371 int max_iters, int flags,
372 uchar *** imgI, uchar *** imgJ,
373 int **step, CvSize** size,
374 double **scale, cv::AutoBuffer<uchar>* buffer )
377 int pyrBytes, bufferBytes = 0, elem_size;
378 int level1 = level + 1;
381 CvSize imgSize, levelSize;
388 /* check input arguments */
389 if( ((flags & CV_LKFLOW_PYR_A_READY) != 0 && !pyrA) ||
390 ((flags & CV_LKFLOW_PYR_B_READY) != 0 && !pyrB) )
391 CV_Error( CV_StsNullPtr, "Some of the precomputed pyramids are missing" );
394 CV_Error( CV_StsOutOfRange, "The number of pyramid layers is negative" );
396 switch( criteria->type )
398 case CV_TERMCRIT_ITER:
399 criteria->epsilon = 0.f;
401 case CV_TERMCRIT_EPS:
402 criteria->max_iter = max_iters;
404 case CV_TERMCRIT_ITER | CV_TERMCRIT_EPS:
408 CV_Error( CV_StsBadArg, "Invalid termination criteria" );
411 /* compare squared values */
412 criteria->epsilon *= criteria->epsilon;
414 /* set pointers and step for every level */
417 imgSize = cvGetSize(imgA);
418 elem_size = CV_ELEM_SIZE(imgA->type);
421 for( i = 1; i < level1; i++ )
423 levelSize.width = (levelSize.width + 1) >> 1;
424 levelSize.height = (levelSize.height + 1) >> 1;
426 int tstep = cvAlign(levelSize.width,ALIGN) * elem_size;
427 pyrBytes += tstep * levelSize.height;
430 assert( pyrBytes <= imgSize.width * imgSize.height * elem_size * 4 / 3 );
432 /* buffer_size = <size for patches> + <size for pyramids> */
433 bufferBytes = (int)((level1 >= 0) * ((pyrA->data.ptr == 0) +
434 (pyrB->data.ptr == 0)) * pyrBytes +
435 (sizeof(imgI[0][0]) * 2 + sizeof(step[0][0]) +
436 sizeof(size[0][0]) + sizeof(scale[0][0])) * level1);
438 buffer->allocate( bufferBytes );
440 *imgI = (uchar **) (uchar*)(*buffer);
441 *imgJ = *imgI + level1;
442 *step = (int *) (*imgJ + level1);
443 *scale = (double *) (*step + level1);
444 *size = (CvSize *)(*scale + level1);
446 imgI[0][0] = imgA->data.ptr;
447 imgJ[0][0] = imgB->data.ptr;
448 step[0][0] = imgA->step;
450 size[0][0] = imgSize;
454 uchar *bufPtr = (uchar *) (*size + level1);
455 uchar *ptrA = pyrA->data.ptr;
456 uchar *ptrB = pyrB->data.ptr;
469 /* build pyramids for both frames */
470 for( i = 1; i <= level; i++ )
473 CvMat prev_level, next_level;
475 levelSize.width = (levelSize.width + 1) >> 1;
476 levelSize.height = (levelSize.height + 1) >> 1;
478 size[0][i] = levelSize;
479 step[0][i] = cvAlign( levelSize.width, ALIGN ) * elem_size;
480 scale[0][i] = scale[0][i - 1] * 0.5;
482 levelBytes = step[0][i] * levelSize.height;
483 imgI[0][i] = (uchar *) ptrA;
486 if( !(flags & CV_LKFLOW_PYR_A_READY) )
488 prev_level = cvMat( size[0][i-1].height, size[0][i-1].width, CV_8UC1 );
489 next_level = cvMat( size[0][i].height, size[0][i].width, CV_8UC1 );
490 cvSetData( &prev_level, imgI[0][i-1], step[0][i-1] );
491 cvSetData( &next_level, imgI[0][i], step[0][i] );
492 cvPyrDown( &prev_level, &next_level );
495 imgJ[0][i] = (uchar *) ptrB;
498 if( !(flags & CV_LKFLOW_PYR_B_READY) )
500 prev_level = cvMat( size[0][i-1].height, size[0][i-1].width, CV_8UC1 );
501 next_level = cvMat( size[0][i].height, size[0][i].width, CV_8UC1 );
502 cvSetData( &prev_level, imgJ[0][i-1], step[0][i-1] );
503 cvSetData( &next_level, imgJ[0][i], step[0][i] );
504 cvPyrDown( &prev_level, &next_level );
511 /* compute dI/dx and dI/dy */
513 icvCalcIxIy_32f( const float* src, int src_step, float* dstX, float* dstY, int dst_step,
514 CvSize src_size, const float* smooth_k, float* buffer0 )
516 int src_width = src_size.width, dst_width = src_size.width-2;
517 int x, height = src_size.height - 2;
518 float* buffer1 = buffer0 + src_width;
520 src_step /= sizeof(src[0]);
521 dst_step /= sizeof(dstX[0]);
523 for( ; height--; src += src_step, dstX += dst_step, dstY += dst_step )
525 const float* src2 = src + src_step;
526 const float* src3 = src + src_step*2;
528 for( x = 0; x < src_width; x++ )
530 float t0 = (src3[x] + src[x])*smooth_k[0] + src2[x]*smooth_k[1];
531 float t1 = src3[x] - src[x];
532 buffer0[x] = t0; buffer1[x] = t1;
535 for( x = 0; x < dst_width; x++ )
537 float t0 = buffer0[x+2] - buffer0[x];
538 float t1 = (buffer1[x] + buffer1[x+2])*smooth_k[0] + buffer1[x+1]*smooth_k[1];
539 dstX[x] = t0; dstY[x] = t1;
546 cvCalcOpticalFlowPyrLK( const void* arrA, const void* arrB,
547 void* pyrarrA, void* pyrarrB,
548 const CvPoint2D32f * featuresA,
549 CvPoint2D32f * featuresB,
550 int count, CvSize winSize, int level,
551 char *status, float *error,
552 CvTermCriteria criteria, int flags )
554 cv::AutoBuffer<uchar> pyrBuffer;
555 cv::AutoBuffer<uchar> buffer;
556 cv::AutoBuffer<char> _status;
558 const int MAX_ITERS = 100;
560 CvMat stubA, *imgA = (CvMat*)arrA;
561 CvMat stubB, *imgB = (CvMat*)arrB;
562 CvMat pstubA, *pyrA = (CvMat*)pyrarrA;
563 CvMat pstubB, *pyrB = (CvMat*)pyrarrB;
565 static const float smoothKernel[] = { 0.09375, 0.3125, 0.09375 }; /* 3/32, 10/32, 3/32 */
574 int threadCount = cvGetNumThreads();
575 float* _patchI[CV_MAX_THREADS];
576 float* _patchJ[CV_MAX_THREADS];
577 float* _Ix[CV_MAX_THREADS];
578 float* _Iy[CV_MAX_THREADS];
582 CvSize patchSize = cvSize( winSize.width * 2 + 1, winSize.height * 2 + 1 );
583 int patchLen = patchSize.width * patchSize.height;
584 int srcPatchLen = (patchSize.width + 2)*(patchSize.height + 2);
586 imgA = cvGetMat( imgA, &stubA );
587 imgB = cvGetMat( imgB, &stubB );
589 if( CV_MAT_TYPE( imgA->type ) != CV_8UC1 )
590 CV_Error( CV_StsUnsupportedFormat, "" );
592 if( !CV_ARE_TYPES_EQ( imgA, imgB ))
593 CV_Error( CV_StsUnmatchedFormats, "" );
595 if( !CV_ARE_SIZES_EQ( imgA, imgB ))
596 CV_Error( CV_StsUnmatchedSizes, "" );
598 if( imgA->step != imgB->step )
599 CV_Error( CV_StsUnmatchedSizes, "imgA and imgB must have equal steps" );
601 imgSize = cvGetMatSize( imgA );
605 pyrA = cvGetMat( pyrA, &pstubA );
607 if( pyrA->step*pyrA->height < icvMinimalPyramidSize( imgSize ) )
608 CV_Error( CV_StsBadArg, "pyramid A has insufficient size" );
618 pyrB = cvGetMat( pyrB, &pstubB );
620 if( pyrB->step*pyrB->height < icvMinimalPyramidSize( imgSize ) )
621 CV_Error( CV_StsBadArg, "pyramid B has insufficient size" );
632 if( !featuresA || !featuresB )
633 CV_Error( CV_StsNullPtr, "Some of arrays of point coordinates are missing" );
636 CV_Error( CV_StsOutOfRange, "The number of tracked points is negative or zero" );
638 if( winSize.width <= 1 || winSize.height <= 1 )
639 CV_Error( CV_StsBadSize, "Invalid search window size" );
641 for( i = 0; i < threadCount; i++ )
642 _patchI[i] = _patchJ[i] = _Ix[i] = _Iy[i] = 0;
644 icvInitPyramidalAlgorithm( imgA, imgB, pyrA, pyrB,
645 level, &criteria, MAX_ITERS, flags,
646 &imgI, &imgJ, &step, &size, &scale, &pyrBuffer );
650 _status.allocate(count);
654 /* buffer_size = <size for patches> + <size for pyramids> */
655 bufferBytes = (srcPatchLen + patchLen * 3) * sizeof( _patchI[0][0] ) * threadCount;
656 buffer.allocate(bufferBytes);
658 for( i = 0; i < threadCount; i++ )
660 _patchI[i] = i == 0 ? (float*)(uchar*)buffer : _Iy[i-1] + patchLen;
661 _patchJ[i] = _patchI[i] + srcPatchLen;
662 _Ix[i] = _patchJ[i] + patchLen;
663 _Iy[i] = _Ix[i] + patchLen;
666 memset( status, 1, count );
668 memset( error, 0, count*sizeof(error[0]) );
670 if( !(flags & CV_LKFLOW_INITIAL_GUESSES) )
671 memcpy( featuresB, featuresA, count*sizeof(featuresA[0]));
673 /* do processing from top pyramid level (smallest image)
674 to the bottom (original image) */
675 for( l = level; l >= 0; l-- )
677 CvSize levelSize = size[l];
678 int levelStep = step[l];
682 #pragma omp parallel for num_threads(threadCount) schedule(dynamic)
684 /* find flow for each given point */
685 for( i = 0; i < count; i++ )
688 CvPoint minI, maxI, minJ, maxJ;
692 CvPoint prev_minJ = { -1, -1 }, prev_maxJ = { -1, -1 };
693 double Gxx = 0, Gxy = 0, Gyy = 0, D = 0, minEig = 0;
694 float prev_mx = 0, prev_my = 0;
696 int threadIdx = cvGetThreadNum();
697 float* patchI = _patchI[threadIdx];
698 float* patchJ = _patchJ[threadIdx];
699 float* Ix = _Ix[threadIdx];
700 float* Iy = _Iy[threadIdx];
702 v.x = featuresB[i].x;
703 v.y = featuresB[i].y;
711 v.x = (float)(v.x * scale[l]);
712 v.y = (float)(v.y * scale[l]);
715 pt_status = status[i];
719 minI = maxI = minJ = maxJ = cvPoint( 0, 0 );
721 u.x = (float) (featuresA[i].x * scale[l]);
722 u.y = (float) (featuresA[i].y * scale[l]);
724 intersect( u, winSize, levelSize, &minI, &maxI );
725 isz = jsz = cvSize(maxI.x - minI.x + 2, maxI.y - minI.y + 2);
726 u.x += (minI.x - (patchSize.width - maxI.x + 1))*0.5f;
727 u.y += (minI.y - (patchSize.height - maxI.y + 1))*0.5f;
729 if( isz.width < 3 || isz.height < 3 ||
730 icvGetRectSubPix_8u32f_C1R( imgI[l], levelStep, levelSize,
731 patchI, isz.width*sizeof(patchI[0]), isz, u ) < 0 )
733 /* point is outside the image. take the next */
738 icvCalcIxIy_32f( patchI, isz.width*sizeof(patchI[0]), Ix, Iy,
739 (isz.width-2)*sizeof(patchI[0]), isz, smoothKernel, patchJ );
741 for( j = 0; j < criteria.max_iter; j++ )
743 double bx = 0, by = 0;
747 intersect( v, winSize, levelSize, &minJ, &maxJ );
749 minJ.x = MAX( minJ.x, minI.x );
750 minJ.y = MAX( minJ.y, minI.y );
752 maxJ.x = MIN( maxJ.x, maxI.x );
753 maxJ.y = MIN( maxJ.y, maxI.y );
755 jsz = cvSize(maxJ.x - minJ.x, maxJ.y - minJ.y);
757 _v.x = v.x + (minJ.x - (patchSize.width - maxJ.x + 1))*0.5f;
758 _v.y = v.y + (minJ.y - (patchSize.height - maxJ.y + 1))*0.5f;
760 if( jsz.width < 1 || jsz.height < 1 ||
761 icvGetRectSubPix_8u32f_C1R( imgJ[l], levelStep, levelSize, patchJ,
762 jsz.width*sizeof(patchJ[0]), jsz, _v ) < 0 )
764 /* point is outside image. take the next */
769 if( maxJ.x == prev_maxJ.x && maxJ.y == prev_maxJ.y &&
770 minJ.x == prev_minJ.x && minJ.y == prev_minJ.y )
772 for( y = 0; y < jsz.height; y++ )
774 const float* pi = patchI +
775 (y + minJ.y - minI.y + 1)*isz.width + minJ.x - minI.x + 1;
776 const float* pj = patchJ + y*jsz.width;
777 const float* ix = Ix +
778 (y + minJ.y - minI.y)*(isz.width-2) + minJ.x - minI.x;
779 const float* iy = Iy + (ix - Ix);
781 for( x = 0; x < jsz.width; x++ )
783 double t0 = pi[x] - pj[x];
792 for( y = 0; y < jsz.height; y++ )
794 const float* pi = patchI +
795 (y + minJ.y - minI.y + 1)*isz.width + minJ.x - minI.x + 1;
796 const float* pj = patchJ + y*jsz.width;
797 const float* ix = Ix +
798 (y + minJ.y - minI.y)*(isz.width-2) + minJ.x - minI.x;
799 const float* iy = Iy + (ix - Ix);
801 for( x = 0; x < jsz.width; x++ )
803 double t = pi[x] - pj[x];
804 bx += (double) (t * ix[x]);
805 by += (double) (t * iy[x]);
806 Gxx += ix[x] * ix[x];
807 Gxy += ix[x] * iy[x];
808 Gyy += iy[x] * iy[x];
812 D = Gxx * Gyy - Gxy * Gxy;
813 if( D < DBL_EPSILON )
819 // Adi Shavit - 2008.05
820 if( flags & CV_LKFLOW_GET_MIN_EIGENVALS )
821 minEig = (Gyy + Gxx - sqrt((Gxx-Gyy)*(Gxx-Gyy) + 4.*Gxy*Gxy))/(2*jsz.height*jsz.width);
829 mx = (float) ((Gyy * bx - Gxy * by) * D);
830 my = (float) ((Gxx * by - Gxy * bx) * D);
835 if( mx * mx + my * my < criteria.epsilon )
838 if( j > 0 && fabs(mx + prev_mx) < 0.01 && fabs(my + prev_my) < 0.01 )
849 status[i] = (char)pt_status;
850 if( l == 0 && error && pt_status )
854 if( flags & CV_LKFLOW_GET_MIN_EIGENVALS )
858 for( y = 0; y < jsz.height; y++ )
860 const float* pi = patchI +
861 (y + minJ.y - minI.y + 1)*isz.width + minJ.x - minI.x + 1;
862 const float* pj = patchJ + y*jsz.width;
864 for( x = 0; x < jsz.width; x++ )
866 double t = pi[x] - pj[x];
872 error[i] = (float)err;
874 } // end of point processing loop (i)
876 } // end of pyramid levels loop (l)
880 /* Affine tracking algorithm */
883 cvCalcAffineFlowPyrLK( const void* arrA, const void* arrB,
884 void* pyrarrA, void* pyrarrB,
885 const CvPoint2D32f * featuresA,
886 CvPoint2D32f * featuresB,
887 float *matrices, int count,
888 CvSize winSize, int level,
889 char *status, float *error,
890 CvTermCriteria criteria, int flags )
892 const int MAX_ITERS = 100;
894 cv::AutoBuffer<char> _status;
895 cv::AutoBuffer<uchar> buffer;
896 cv::AutoBuffer<uchar> pyr_buffer;
898 CvMat stubA, *imgA = (CvMat*)arrA;
899 CvMat stubB, *imgB = (CvMat*)arrB;
900 CvMat pstubA, *pyrA = (CvMat*)pyrarrA;
901 CvMat pstubB, *pyrB = (CvMat*)pyrarrB;
903 static const float smoothKernel[] = { 0.09375, 0.3125, 0.09375 }; /* 3/32, 10/32, 3/32 */
920 CvSize patchSize = cvSize( winSize.width * 2 + 1, winSize.height * 2 + 1 );
921 int patchLen = patchSize.width * patchSize.height;
922 int patchStep = patchSize.width * sizeof( patchI[0] );
924 CvSize srcPatchSize = cvSize( patchSize.width + 2, patchSize.height + 2 );
925 int srcPatchLen = srcPatchSize.width * srcPatchSize.height;
926 int srcPatchStep = srcPatchSize.width * sizeof( patchI[0] );
928 float eps = (float)MIN(winSize.width, winSize.height);
930 imgA = cvGetMat( imgA, &stubA );
931 imgB = cvGetMat( imgB, &stubB );
933 if( CV_MAT_TYPE( imgA->type ) != CV_8UC1 )
934 CV_Error( CV_StsUnsupportedFormat, "" );
936 if( !CV_ARE_TYPES_EQ( imgA, imgB ))
937 CV_Error( CV_StsUnmatchedFormats, "" );
939 if( !CV_ARE_SIZES_EQ( imgA, imgB ))
940 CV_Error( CV_StsUnmatchedSizes, "" );
942 if( imgA->step != imgB->step )
943 CV_Error( CV_StsUnmatchedSizes, "imgA and imgB must have equal steps" );
946 CV_Error( CV_StsNullPtr, "" );
948 imgSize = cvGetMatSize( imgA );
952 pyrA = cvGetMat( pyrA, &pstubA );
954 if( pyrA->step*pyrA->height < icvMinimalPyramidSize( imgSize ) )
955 CV_Error( CV_StsBadArg, "pyramid A has insufficient size" );
965 pyrB = cvGetMat( pyrB, &pstubB );
967 if( pyrB->step*pyrB->height < icvMinimalPyramidSize( imgSize ) )
968 CV_Error( CV_StsBadArg, "pyramid B has insufficient size" );
979 /* check input arguments */
980 if( !featuresA || !featuresB || !matrices )
981 CV_Error( CV_StsNullPtr, "" );
983 if( winSize.width <= 1 || winSize.height <= 1 )
984 CV_Error( CV_StsOutOfRange, "the search window is too small" );
987 CV_Error( CV_StsOutOfRange, "" );
989 icvInitPyramidalAlgorithm( imgA, imgB,
990 pyrA, pyrB, level, &criteria, MAX_ITERS, flags,
991 &imgI, &imgJ, &step, &size, &scale, &pyr_buffer );
993 /* buffer_size = <size for patches> + <size for pyramids> */
994 bufferBytes = (srcPatchLen + patchLen*3)*sizeof(patchI[0]) + (36*2 + 6)*sizeof(double);
996 buffer.allocate(bufferBytes);
1000 _status.allocate(count);
1004 patchI = (float *)(uchar*)buffer;
1005 patchJ = patchI + srcPatchLen;
1006 Ix = patchJ + patchLen;
1010 memset( status, 1, count );
1012 if( !(flags & CV_LKFLOW_INITIAL_GUESSES) )
1014 memcpy( featuresB, featuresA, count * sizeof( featuresA[0] ));
1015 for( i = 0; i < count * 4; i += 4 )
1017 matrices[i] = matrices[i + 3] = 1.f;
1018 matrices[i + 1] = matrices[i + 2] = 0.f;
1022 for( i = 0; i < count; i++ )
1024 featuresB[i].x = (float)(featuresB[i].x * scale[level] * 0.5);
1025 featuresB[i].y = (float)(featuresB[i].y * scale[level] * 0.5);
1028 /* do processing from top pyramid level (smallest image)
1029 to the bottom (original image) */
1030 for( l = level; l >= 0; l-- )
1032 CvSize levelSize = size[l];
1033 int levelStep = step[l];
1035 /* find flow for each given point at the particular level */
1036 for( i = 0; i < count; i++ )
1041 double meanI = 0, meanJ = 0;
1043 int pt_status = status[i];
1049 Av[0] = matrices[i*4];
1050 Av[1] = matrices[i*4+1];
1051 Av[3] = matrices[i*4+2];
1052 Av[4] = matrices[i*4+3];
1054 Av[2] = featuresB[i].x += featuresB[i].x;
1055 Av[5] = featuresB[i].y += featuresB[i].y;
1057 u.x = (float) (featuresA[i].x * scale[l]);
1058 u.y = (float) (featuresA[i].y * scale[l]);
1060 if( u.x < -eps || u.x >= levelSize.width+eps ||
1061 u.y < -eps || u.y >= levelSize.height+eps ||
1062 icvGetRectSubPix_8u32f_C1R( imgI[l], levelStep,
1063 levelSize, patchI, srcPatchStep, srcPatchSize, u ) < 0 )
1065 /* point is outside the image. take the next */
1071 icvCalcIxIy_32f( patchI, srcPatchStep, Ix, Iy,
1072 (srcPatchSize.width-2)*sizeof(patchI[0]), srcPatchSize,
1073 smoothKernel, patchJ );
1075 /* repack patchI (remove borders) */
1076 for( k = 0; k < patchSize.height; k++ )
1077 memcpy( patchI + k * patchSize.width,
1078 patchI + (k + 1) * srcPatchSize.width + 1, patchStep );
1080 memset( G, 0, sizeof( G ));
1082 /* calculate G matrix */
1083 for( y = -winSize.height, k = 0; y <= winSize.height; y++ )
1085 for( x = -winSize.width; x <= winSize.width; x++, k++ )
1087 double ixix = ((double) Ix[k]) * Ix[k];
1088 double ixiy = ((double) Ix[k]) * Iy[k];
1089 double iyiy = ((double) Iy[k]) * Iy[k];
1112 // G[13] == G[8] == G[4]
1143 meanI /= patchSize.width*patchSize.height;
1149 // fill part of G below its diagonal
1150 for( y = 1; y < 6; y++ )
1151 for( x = 0; x < y; x++ )
1152 G[y * 6 + x] = G[x * 6 + y];
1154 cvInitMatHeader( &mat, 6, 6, CV_64FC1, G );
1156 if( cvInvert( &mat, &mat, CV_SVD ) < 1e-4 )
1158 /* bad matrix. take the next point */
1164 for( j = 0; j < criteria.max_iter; j++ )
1166 double b[6] = {0,0,0,0,0,0}, eta[6];
1167 double t0, t1, s = 0;
1169 if( Av[2] < -eps || Av[2] >= levelSize.width+eps ||
1170 Av[5] < -eps || Av[5] >= levelSize.height+eps ||
1171 icvGetQuadrangleSubPix_8u32f_C1R( imgJ[l], levelStep,
1172 levelSize, patchJ, patchStep, patchSize, Av ) < 0 )
1178 for( y = -winSize.height, k = 0, meanJ = 0; y <= winSize.height; y++ )
1179 for( x = -winSize.width; x <= winSize.width; x++, k++ )
1182 meanJ = meanJ / (patchSize.width * patchSize.height) - meanI;
1184 for( y = -winSize.height, k = 0; y <= winSize.height; y++ )
1186 for( x = -winSize.width; x <= winSize.width; x++, k++ )
1188 double t = patchI[k] - patchJ[k] + meanJ;
1189 double ixt = Ix[k] * t;
1190 double iyt = Iy[k] * t;
1203 for( k = 0; k < 6; k++ )
1204 eta[k] = G[k*6]*b[0] + G[k*6+1]*b[1] + G[k*6+2]*b[2] +
1205 G[k*6+3]*b[3] + G[k*6+4]*b[4] + G[k*6+5]*b[5];
1207 Av[2] = (float)(Av[2] + Av[0] * eta[0] + Av[1] * eta[1]);
1208 Av[5] = (float)(Av[5] + Av[3] * eta[0] + Av[4] * eta[1]);
1210 t0 = Av[0] * (1 + eta[2]) + Av[1] * eta[4];
1211 t1 = Av[0] * eta[3] + Av[1] * (1 + eta[5]);
1215 t0 = Av[3] * (1 + eta[2]) + Av[4] * eta[4];
1216 t1 = Av[3] * eta[3] + Av[4] * (1 + eta[5]);
1220 if( eta[0] * eta[0] + eta[1] * eta[1] < criteria.epsilon )
1224 if( pt_status != 0 || l == 0 )
1226 status[i] = (char)pt_status;
1227 featuresB[i].x = Av[2];
1228 featuresB[i].y = Av[5];
1230 matrices[i*4] = Av[0];
1231 matrices[i*4+1] = Av[1];
1232 matrices[i*4+2] = Av[3];
1233 matrices[i*4+3] = Av[4];
1236 if( pt_status && l == 0 && error )
1241 for( y = 0, k = 0; y < patchSize.height; y++ )
1243 for( x = 0; x < patchSize.width; x++, k++ )
1245 double t = patchI[k] - patchJ[k] + meanJ;
1249 error[i] = (float)sqrt(err);
1258 icvGetRTMatrix( const CvPoint2D32f* a, const CvPoint2D32f* b,
1259 int count, CvMat* M, int full_affine )
1263 double sa[36], sb[6];
1264 CvMat A = cvMat( 6, 6, CV_64F, sa ), B = cvMat( 6, 1, CV_64F, sb );
1265 CvMat MM = cvMat( 6, 1, CV_64F, M->data.db );
1269 memset( sa, 0, sizeof(sa) );
1270 memset( sb, 0, sizeof(sb) );
1272 for( i = 0; i < count; i++ )
1274 sa[0] += a[i].x*a[i].x;
1275 sa[1] += a[i].y*a[i].x;
1278 sa[6] += a[i].x*a[i].y;
1279 sa[7] += a[i].y*a[i].y;
1286 sb[0] += a[i].x*b[i].x;
1287 sb[1] += a[i].y*b[i].x;
1289 sb[3] += a[i].x*b[i].y;
1290 sb[4] += a[i].y*b[i].y;
1304 cvSolve( &A, &B, &MM, CV_SVD );
1308 double sa[16], sb[4], m[4], *om = M->data.db;
1309 CvMat A = cvMat( 4, 4, CV_64F, sa ), B = cvMat( 4, 1, CV_64F, sb );
1310 CvMat MM = cvMat( 4, 1, CV_64F, m );
1314 memset( sa, 0, sizeof(sa) );
1315 memset( sb, 0, sizeof(sb) );
1317 for( i = 0; i < count; i++ )
1319 sa[0] += a[i].x*a[i].x + a[i].y*a[i].y;
1325 sa[5] += a[i].x*a[i].x + a[i].y*a[i].y;
1339 sb[0] += a[i].x*b[i].x + a[i].y*b[i].y;
1340 sb[1] += a[i].x*b[i].y - a[i].y*b[i].x;
1345 cvSolve( &A, &B, &MM, CV_SVD );
1347 om[0] = om[4] = m[0];
1357 cvEstimateRigidTransform( const CvArr* _A, const CvArr* _B, CvMat* _M, int full_affine )
1359 const int COUNT = 15;
1360 const int WIDTH = 160, HEIGHT = 120;
1361 const int RANSAC_MAX_ITERS = 500;
1362 const int RANSAC_SIZE0 = 3;
1363 const double RANSAC_GOOD_RATIO = 0.5;
1365 cv::Ptr<CvMat> sA, sB;
1366 cv::AutoBuffer<CvPoint2D32f> pA, pB;
1367 cv::AutoBuffer<int> good_idx;
1368 cv::AutoBuffer<char> status;
1369 cv::Ptr<CvMat> gray;
1371 CvMat stubA, *A = cvGetMat( _A, &stubA );
1372 CvMat stubB, *B = cvGetMat( _B, &stubB );
1374 int cn, equal_sizes;
1376 int count_x, count_y, count = 0;
1378 CvRNG rng = cvRNG(-1);
1380 CvMat M = cvMat( 2, 3, CV_64F, m );
1384 if( !CV_IS_MAT(_M) )
1385 CV_Error( _M ? CV_StsBadArg : CV_StsNullPtr, "Output parameter M is not a valid matrix" );
1387 if( !CV_ARE_SIZES_EQ( A, B ) )
1388 CV_Error( CV_StsUnmatchedSizes, "Both input images must have the same size" );
1390 if( !CV_ARE_TYPES_EQ( A, B ) )
1391 CV_Error( CV_StsUnmatchedFormats, "Both input images must have the same data type" );
1393 if( CV_MAT_TYPE(A->type) == CV_8UC1 || CV_MAT_TYPE(A->type) == CV_8UC3 )
1395 cn = CV_MAT_CN(A->type);
1397 sz1 = cvSize(WIDTH, HEIGHT);
1399 scale = MAX( (double)sz1.width/sz0.width, (double)sz1.height/sz0.height );
1400 scale = MIN( scale, 1. );
1401 sz1.width = cvRound( sz0.width * scale );
1402 sz1.height = cvRound( sz0.height * scale );
1404 equal_sizes = sz1.width == sz0.width && sz1.height == sz0.height;
1406 if( !equal_sizes || cn != 1 )
1408 sA = cvCreateMat( sz1.height, sz1.width, CV_8UC1 );
1409 sB = cvCreateMat( sz1.height, sz1.width, CV_8UC1 );
1413 gray = cvCreateMat( sz0.height, sz0.width, CV_8UC1 );
1414 cvCvtColor( A, gray, CV_BGR2GRAY );
1415 cvResize( gray, sA, CV_INTER_AREA );
1416 cvCvtColor( B, gray, CV_BGR2GRAY );
1417 cvResize( gray, sB, CV_INTER_AREA );
1422 cvResize( A, sA, CV_INTER_AREA );
1423 cvResize( B, sB, CV_INTER_AREA );
1431 count_x = cvRound((double)COUNT*sz1.width/sz1.height);
1432 count = count_x * count_y;
1436 status.allocate(count);
1438 for( i = 0, k = 0; i < count_y; i++ )
1439 for( j = 0; j < count_x; j++, k++ )
1441 pA[k].x = (j+0.5f)*sz1.width/count_x;
1442 pA[k].y = (i+0.5f)*sz1.height/count_y;
1445 // find the corresponding points in B
1446 cvCalcOpticalFlowPyrLK( A, B, 0, 0, pA, pB, count, cvSize(10,10), 3,
1447 status, 0, cvTermCriteria(CV_TERMCRIT_ITER,40,0.1), 0 );
1449 // repack the remained points
1450 for( i = 0, k = 0; i < count; i++ )
1463 else if( CV_MAT_TYPE(A->type) == CV_32FC2 || CV_MAT_TYPE(A->type) == CV_32SC2 )
1465 count = A->cols*A->rows;
1469 _pA = cvMat( A->rows, A->cols, CV_32FC2, pA );
1470 _pB = cvMat( B->rows, B->cols, CV_32FC2, pB );
1471 cvConvert( A, &_pA );
1472 cvConvert( B, &_pB );
1475 CV_Error( CV_StsUnsupportedFormat, "Both input images must have either 8uC1 or 8uC3 type" );
1477 good_idx.allocate(count);
1479 if( count < RANSAC_SIZE0 )
1482 CvMat _pB = cvMat(1, count, CV_32FC2, pB);
1483 brect = cvBoundingRect(&_pB, 1);
1486 // 1. find the consensus
1487 for( k = 0; k < RANSAC_MAX_ITERS; k++ )
1489 int idx[RANSAC_SIZE0];
1493 memset( a, 0, sizeof(a) );
1494 memset( b, 0, sizeof(b) );
1496 // choose random 3 non-complanar points from A & B
1497 for( i = 0; i < RANSAC_SIZE0; i++ )
1499 for( k1 = 0; k1 < RANSAC_MAX_ITERS; k1++ )
1501 idx[i] = cvRandInt(&rng) % count;
1503 for( j = 0; j < i; j++ )
1505 if( idx[j] == idx[i] )
1507 // check that the points are not very close one each other
1508 if( fabs(pA[idx[i]].x - pA[idx[j]].x) +
1509 fabs(pA[idx[i]].y - pA[idx[j]].y) < FLT_EPSILON )
1511 if( fabs(pB[idx[i]].x - pB[idx[j]].x) +
1512 fabs(pB[idx[i]].y - pB[idx[j]].y) < FLT_EPSILON )
1519 if( i+1 == RANSAC_SIZE0 )
1521 // additional check for non-complanar vectors
1530 double dax1 = a[1].x - a[0].x, day1 = a[1].y - a[0].y;
1531 double dax2 = a[2].x - a[0].x, day2 = a[2].y - a[0].y;
1532 double dbx1 = b[1].x - b[0].y, dby1 = b[1].y - b[0].y;
1533 double dbx2 = b[2].x - b[0].x, dby2 = b[2].y - b[0].y;
1534 const double eps = 0.01;
1536 if( fabs(dax1*day2 - day1*dax2) < eps*sqrt(dax1*dax1+day1*day1)*sqrt(dax2*dax2+day2*day2) ||
1537 fabs(dbx1*dby2 - dby1*dbx2) < eps*sqrt(dbx1*dbx1+dby1*dby1)*sqrt(dbx2*dbx2+dby2*dby2) )
1543 if( k1 >= RANSAC_MAX_ITERS )
1547 if( i < RANSAC_SIZE0 )
1550 // estimate the transformation using 3 points
1551 icvGetRTMatrix( a, b, 3, &M, full_affine );
1553 for( i = 0, good_count = 0; i < count; i++ )
1555 if( fabs( m[0]*pA[i].x + m[1]*pA[i].y + m[2] - pB[i].x ) +
1556 fabs( m[3]*pA[i].x + m[4]*pA[i].y + m[5] - pB[i].y ) < MAX(brect.width,brect.height)*0.05 )
1557 good_idx[good_count++] = i;
1560 if( good_count >= count*RANSAC_GOOD_RATIO )
1564 if( k >= RANSAC_MAX_ITERS )
1567 if( good_count < count )
1569 for( i = 0; i < good_count; i++ )
1577 icvGetRTMatrix( pA, pB, good_count, &M, full_affine );
1580 cvConvert( &M, _M );
1588 Mat estimateRigidTransform( const Mat& A,
1592 Mat M(2, 3, CV_64F);
1593 CvMat _A = A, _B = B, _M = M;
1594 cvEstimateRigidTransform(&_A, &_B, &_M, fullAffine);