cvEstimateRigidTransform是opencv中求取仿射变换的函数,定义在lkpyramid.cpp文件中,该函数先利用ransac算法从所有特征点中选取一定数目的特征点,选取出的这些特征点性质都较好,然后利用icvGetRTMatrix函数求取仿射变换系数,下面是cvEstimateRigidTransform函数的详细注解。
CV_IMPL int
cvEstimateRigidTransform( const CvArr* matA, const CvArr* matB, CvMat* matM, int full_affine )
{
const int COUNT = 15;
const int WIDTH = 160, HEIGHT = 120;
const int RANSAC_MAX_ITERS = 500;
const int RANSAC_SIZE0 = 3;
const double RANSAC_GOOD_RATIO = 0.5;
cv::Ptr<CvMat> sA, sB; //智能指针,相当于c++中的shared_ptr
cv::AutoBuffer<CvPoint2D32f> pA, pB;
cv::AutoBuffer<int> good_idx;
cv::AutoBuffer<char> status;
cv::Ptr<CvMat> gray;
CvMat stubA, *A = cvGetMat( matA, &stubA ); //将CvArr*类型的matA转化为CvMat类型的stubA,A是1*192
CvMat stubB, *B = cvGetMat( matB, &stubB );
CvSize sz0, sz1;
int cn, equal_sizes;
int i, j, k, k1;
int count_x, count_y, count = 0;
double scale = 1;
CvRNG rng = cvRNG(-1);//初始化随机数发生器
double m[6]={0};
CvMat M = cvMat( 2, 3, CV_64F, m );
int good_count = 0;
CvRect brect;
if( !CV_IS_MAT(matM) )
CV_Error( matM ? CV_StsBadArg : CV_StsNullPtr, "Output parameter M is not a valid matrix" );
if( !CV_ARE_SIZES_EQ( A, B ) )
CV_Error( CV_StsUnmatchedSizes, "Both input images must have the same size" );
if( !CV_ARE_TYPES_EQ( A, B ) )
CV_Error( CV_StsUnmatchedFormats, "Both input images must have the same data type" );
if( CV_MAT_TYPE(A->type) == CV_8UC1 || CV_MAT_TYPE(A->type) == CV_8UC3 ) //8位无符号
{
cn = CV_MAT_CN(A->type); //返回通道数
sz