熟悉仿射的代碼中的OpenCV /模塊/ imgproc/SRC/imgwarp.cpp變換:它確實只是兩件事情:
一個。重新排列輸入以創建一個系統Ax = B;
b。然後調用解決(A,B,X);
注意:忽略openCV代碼中的函數註釋 - 它們令人困惑,並且不能反映矩陣中元素的實際排序。如果您正在解決[U,V] =仿射* [X,Y,1]的重排:
x1 y1 1 0 0 1
0 0 0 x1 y1 1
x2 y2 1 0 0 1
A = 0 0 0 x2 y2 1
x3 y3 1 0 0 1
0 0 0 x3 y3 1
X = [Affine11, Affine12, Affine13, Affine21, Affine22, Affine23]’
u1 v1
B = u2 v2
u3 v3
所有你需要做的是增加更多的點。要使求解(A,B,X)在超定系統上工作,請添加DECOMP_SVD參數。要查看該主題的幻燈片,請使用link。如果您想了解更多關於計算機視覺背景下的僞逆,最好的資料來源是:ComputerVision,請參閱第15章和附錄C.
如果您仍不確定如何添加更多點,請參閱我的代碼如下:
// extension for n points;
cv::Mat getAffineTransformOverdetermined(const Point2f src[], const Point2f dst[], int n)
{
Mat M(2, 3, CV_64F), X(6, 1, CV_64F, M.data); // output
double* a = (double*)malloc(12*n*sizeof(double));
double* b = (double*)malloc(2*n*sizeof(double));
Mat A(2*n, 6, CV_64F, a), B(2*n, 1, CV_64F, b); // input
for(int i = 0; i < n; i++)
{
int j = i*12; // 2 equations (in x, y) with 6 members: skip 12 elements
int k = i*12+6; // second equation: skip extra 6 elements
a[j] = a[k+3] = src[i].x;
a[j+1] = a[k+4] = src[i].y;
a[j+2] = a[k+5] = 1;
a[j+3] = a[j+4] = a[j+5] = 0;
a[k] = a[k+1] = a[k+2] = 0;
b[i*2] = dst[i].x;
b[i*2+1] = dst[i].y;
}
solve(A, B, X, DECOMP_SVD);
delete a;
delete b;
return M;
}
// call original transform
vector<Point2f> src(3);
vector<Point2f> dst(3);
src[0] = Point2f(0.0, 0.0);src[1] = Point2f(1.0, 0.0);src[2] = Point2f(0.0, 1.0);
dst[0] = Point2f(0.0, 0.0);dst[1] = Point2f(1.0, 0.0);dst[2] = Point2f(0.0, 1.0);
Mat M = getAffineTransform(Mat(src), Mat(dst));
cout<<M<<endl;
// call new transform
src.resize(4); src[3] = Point2f(22, 2);
dst.resize(4); dst[3] = Point2f(22, 2);
Mat M2 = getAffineTransformOverdetermined(src.data(), dst.data(), src.size());
cout<<M2<<endl;
也許[estimatedRigidTransform](http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking。html#estimaterigidtransform)將符合您的需求。 – cgat