// //测试行列式是否计算正确; void Matrix::TestDet() { int test_sum=0; // /*/输入; char * ssb = " \ 4 \ 1 1 \ 5 \ 4 4 \ 1 1 1 1 \ 1 2 4 8 \ 1 3 9 27 \ 1 4 16 64 \ 3 3 \ 2 0 1 \ 1 -4 -1 \ -1 8 3 \ 4 4 \ 4 1 2 4 \ 1 2 0 2 \ 10 5 2 0 \ 0 1 1 7 \ "; strstream sst(ssb,strlen(ssb)); cin = sst;//*/ //答案; double ans[] = {5,12,-4,0}; // cin >> test_sum; for(int count=0;count<test_sum;count++) { int n,m; cin >> n >> m; // Matrix mat; mat.NewMatrix(n,m); mat.ReadStd(); int swp = mat.RowSimplify(); // if(Equal(mat.Det(swp),ans[count])) cout << count << ".DetRight" << endl; else cout << count << ".DetWrong" << endl; // mat.DeleteMatrix(); } cout << endl; }
T Det(){ T res(0); if (n == 2){ res.SetVal(res.sub(res.mul((mx[0])[0], (mx[1])[1]), res.mul((mx[1])[0], (mx[0])[1]))); return res; } for (int j = 0; j < m; j++){ Matrix<T> sm = GetTruncated(0, j); T a = sm.Det(); a.SetVal(a.mul(a, (mx[0])[j])); a.SetVal(a.mul(a, a.up(j))); res.SetVal(res.add(res, a)); } return res; }
// The affine parameters can be calculated by: // // |xf| = |a11 a12| * |xm| + |xt| // |yf| |a21 a22| |ym| |yt| // // Construct in the form of Gauss Markov Model as // // |xf| = |xm ym 0 0 1 0| * |a11| // |yf| |0 0 xm ym 0 1| |a12| // |a21| // |a22| // |xt | // |yt | // y = AX // // X = inv(AT A) (AT y) // // bool CFMDoc::CalculateAffineParameters() { if (m_bCalculatedAffine == true) return true; int pID; ITER_MATCHED iter; MATCHING_RESULT point; bool bCreateDlgLocal = false; // Construct matrix for Least Square calculation. Matrix A(MAX_MATCHING*2,6); // Construct the A matrix with size (16*6) Matrix y(MAX_MATCHING*2,1); // Construct the y matrix with size (16*1) Matrix X; // Construct the X matrix as a result, with size (6*1) // Matrix A is constructed from the matching results. // If less than 8 matching points found, exit. if (m_mapMatchingPoints.size() < MAX_MATCHING) { MessageBox(NULL, "Failed to calculate the affine parameters.\nThe application failed to locate eight matching points.", "Notification", MB_ICONINFORMATION|MB_OK); return false; } // Matrix y is constructed from the fiducial data. if (NULL == m_pCalibrationDlg) { m_pCalibrationDlg = new CCalibrationDlg; // Set flag if it is created for this purpose. bCreateDlgLocal = true; } // If failed to optain the calibration data, exit. if (false == m_pCalibrationDlg->ReadCalibrationData()) { MessageBox(NULL, "Failed to calculate the affine parameters.\nThe application failed to obtain the calibration data.", "Notification", MB_ICONINFORMATION|MB_OK); return false; } for (iter = m_mapMatchingPoints.begin(); iter != m_mapMatchingPoints.end(); iter++) { // Get the value of the matched point. pID = (*iter).first; point = (*iter).second; // Assign the value to the matrix. // [ xm ym 0 0 1 0; <-- row (pID-1)*2 // 0 0 xm ym 0 1] <-- row (pID-1)*2 + 1 // Set the value for the first row of each point A.Set( (pID-1)*2, 0, point.fX); A.Set( (pID-1)*2, 1, point.fY); A.Set( (pID-1)*2, 2, 0); A.Set( (pID-1)*2, 3, 0); A.Set( (pID-1)*2, 4, 1); A.Set( (pID-1)*2, 5, 0); // Set the value for the second row of each point A.Set( (pID-1)*2 +1, 0, 0); A.Set( (pID-1)*2 +1, 1, 0); A.Set( (pID-1)*2 +1, 2, point.fX); A.Set( (pID-1)*2 +1, 3, point.fY); A.Set( (pID-1)*2 +1, 4, 0); A.Set( (pID-1)*2 +1, 5, 1); // Assign the value to the matrix y. // [ xf; <-- row (pID-1)*2 // yf ] <-- row (pID-1)*2 + 1 y.Set( (pID-1)*2, 0, m_pCalibrationDlg->m_FC[(pID-1)].fX); y.Set( (pID-1)*2 +1, 0, m_pCalibrationDlg->m_FC[(pID-1)].fY); } // Calculate the results as "X = inv(AT A) (AT y)" Matrix N = A.Trans() * A; // Check if N inverse exist. double det = N.Det(); if(det == 0.0 || det <= 1e-10) { MessageBox(NULL, "Failed to calculate the affine parameters.\nThe coordinates are not in a good condition.", "Notification", MB_ICONINFORMATION|MB_OK); return false; } // Find the X matrix. X = N.Inv() * A.Trans() * y; // Then obtain the affine transformation parameters. m_a11 = X.Get(0, 0); m_a12 = X.Get(1, 0); m_a21 = X.Get(2, 0); m_a22 = X.Get(3, 0); m_xt = X.Get(4, 0); m_yt = X.Get(5, 0); // Delete the object since it is no longer used. if (true == bCreateDlgLocal) { delete m_pCalibrationDlg; m_pCalibrationDlg = NULL; } m_bCalculatedAffine = true; return true; }