Ask Your Question

Revision history [back]

Try this :

     SVDecomp(x, S, U, VT, cv::SVD::FULL_UV);
     /*Please note that S is a column vector and S should be a square diagonal matrix, Assigning the components of the S    to the diagonal components of Sigma*/
     for (int i = 0; i < VT.rows; i++)
         VT.row(i) = VT.row(i) * S.at<double>(i, 0);
     x_hat = U * VT;
     cout << "U * Sigma* VT= " << endl << x_hat << endl;
     absdiff(x, x_hat, err);
     minMaxIdx(err, &minVal, &maxVal, minLoc, maxLoc);
     cout << "Reconstruction err: MIN= " << minVal << ", MAX= " << maxVal << ", sum= " << sum(err) << endl;

and it is not Sigma.at<double>(i1, i1) but S.at<double>(i1,0); results are

U * Sigma* VT=
[23, 24.00000000000001, 23.99999999999999, 23.00000000000001, 22.5, 21.49999999999999, 20.00000000000002, 19.99999999999999, 19, 20, 21.99999999999999, 23, 23, 23.5, 24, 25.5;
 23.50000000000001, 24.00000000000003, 22.99999999999999, 21.00000000000001, 21, 21.99999999999999, 21.00000000000001, 20, 18.5, 18, 18.99999999999999, 20, 20.50000000000001, 23, 24, 24;
 24.00000000000002, 24.00000000000002, 22, 21.00000000000003, 21.5, 21.5, 21.00000000000001, 20, 19.5, 20.00000000000001, 21.00000000000001, 21, 22.00000000000001, 23.00000000000001, 24, 24;
 24.49999999999999, 24, 22.99999999999998, 23.00000000000001, 22.99999999999998, 20.99999999999999, 20.00000000000001, 19.99999999999998, 20, 22, 21.99999999999999, 21.99999999999999, 23, 23, 23.99999999999999, 24;
 24.00000000000001, 24.00000000000002, 24, 24.00000000000001, 24, 22, 20.00000000000002, 20, 20.00000000000001, 21.00000000000001, 21.00000000000001, 21, 21.00000000000001, 22.00000000000001, 24.00000000000001, 24.00000000000001;
 23.99999999999999, 24.5, 24.99999999999998, 24.50000000000001, 23.99999999999998, 21.99999999999999, 20, 20.99999999999999, 21, 21, 21.99999999999999, 22, 22.00000000000001, 22.99999999999999, 24, 24;
 23.99999999999999, 24.5, 24.99999999999998, 24.50000000000001, 23.99999999999999, 21.99999999999999, 20.00000000000002, 21.99999999999999, 23.00000000000001, 23, 24, 23.99999999999999, 23.00000000000001, 22, 22.5, 23;
 24.5, 24.5, 24, 24.00000000000001, 22.99999999999999, 22, 22.00000000000002, 22.99999999999999, 24.5, 24.50000000000001, 23, 21.49999999999999, 21.00000000000001, 21.00000000000001, 22.5, 22.5;
 23.5, 24.5, 25.49999999999998, 25, 23.49999999999999, 24.49999999999999, 26.00000000000001, 25.49999999999999, 24, 25, 22.5, 20.5, 20.5, 21, 21, 20.50000000000001;
 24.5, 25.50000000000002, 25.99999999999999, 25.5, 24.5, 24.99999999999998, 25.5, 24.99999999999999, 24, 24, 21.99999999999998, 20.49999999999999, 20.49999999999999, 19.5, 18.49999999999999, 19.5;
 25.49999999999999, 25.49999999999999, 25.99999999999998, 25.99999999999999, 24.99999999999998, 24.99999999999998, 25, 24.99999999999998, 23.49999999999999, 24, 22, 20, 20.00000000000001, 19.50000000000001, 18.5, 21;
 24, 24, 23.99999999999998, 24.00000000000001, 23.99999999999998, 24.49999999999999, 23, 22.49999999999999, 24, 24.50000000000001, 23, 20.49999999999999, 20.00000000000001, 20.00000000000001, 20.5, 22.5;
 24.00000000000002, 24.00000000000003, 24.00000000000001, 24.00000000000003, 24, 24, 23.00000000000003, 22.5, 24.00000000000002, 23.50000000000001, 23.00000000000001, 21.50000000000002, 20.00000000000003, 19.50000000000003, 22.00000000000002, 24.00000000000002;
 22.5, 23.00000000000001, 22.49999999999998, 22, 22.99999999999999, 22.99999999999999, 22.00000000000001, 21.49999999999999, 23.00000000000001, 22.00000000000001, 22.00000000000001, 22, 20.00000000000001, 20.50000000000001, 23.00000000000001, 24.00000000000001;
 22.50000000000001, 21.50000000000002, 21.5, 22.00000000000001, 23, 23.99999999999999, 22.50000000000001, 20, 21.00000000000001, 20.50000000000001, 20.5, 22, 22.00000000000001, 23, 23.5, 23.5;
 22.5, 21.00000000000001, 20.99999999999999, 21.00000000000001, 21, 22.99999999999999, 21.5, 19.99999999999999, 20, 20.5, 20.5, 21.99999999999999, 24.00000000000001, 24.00000000000001, 23.50000000000001, 23.50000000000001]
Reconstruction err: MIN= 0, MAX= 3.19744e-14, sum= [2.17426e-12, 0, 0, 0]

OpenCV(4.0.0-pre) Error: Assertion failed ((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())) in cv::Mat::at, file g:\lib\install\opencv\include\opencv2\core\mat.inl.hpp, line 1144 OpenCV: terminate handler is called! The last OpenCV error is: OpenCV(4.0.0-pre) Error: Assertion failed ((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())) in cv::Mat::at, file g:\lib\install\opencv\include\opencv2\core\mat.inl.hpp, line 1144 Appuyez sur une touche pour continuer...

Try this :

     SVDecomp(x, S, U, VT, cv::SVD::FULL_UV);
     /*Please note that S is a column vector and S should be a square diagonal matrix, Assigning the components of the S    to the diagonal components of Sigma*/
     for (int i = 0; i < VT.rows; i++)
         VT.row(i) = VT.row(i) * S.at<double>(i, 0);
     x_hat = U * VT;
     cout << "U * Sigma* VT= " << endl << x_hat << endl;
     absdiff(x, x_hat, err);
     minMaxIdx(err, &minVal, &maxVal, minLoc, maxLoc);
     cout << "Reconstruction err: MIN= " << minVal << ", MAX= " << maxVal << ", sum= " << sum(err) << endl;

and it is not Sigma.at<double>(i1, i1) but S.at<double>(i1,0); results are

U * Sigma* VT=
[23, 24.00000000000001, 23.99999999999999, 23.00000000000001, 22.5, 21.49999999999999, 20.00000000000002, 19.99999999999999, 19, 20, 21.99999999999999, 23, 23, 23.5, 24, 25.5;
 23.50000000000001, 24.00000000000003, 22.99999999999999, 21.00000000000001, 21, 21.99999999999999, 21.00000000000001, 20, 18.5, 18, 18.99999999999999, 20, 20.50000000000001, 23, 24, 24;
 24.00000000000002, 24.00000000000002, 22, 21.00000000000003, 21.5, 21.5, 21.00000000000001, 20, 19.5, 20.00000000000001, 21.00000000000001, 21, 22.00000000000001, 23.00000000000001, 24, 24;
 24.49999999999999, 24, 22.99999999999998, 23.00000000000001, 22.99999999999998, 20.99999999999999, 20.00000000000001, 19.99999999999998, 20, 22, 21.99999999999999, 21.99999999999999, 23, 23, 23.99999999999999, 24;
 24.00000000000001, 24.00000000000002, 24, 24.00000000000001, 24, 22, 20.00000000000002, 20, 20.00000000000001, 21.00000000000001, 21.00000000000001, 21, 21.00000000000001, 22.00000000000001, 24.00000000000001, 24.00000000000001;
 23.99999999999999, 24.5, 24.99999999999998, 24.50000000000001, 23.99999999999998, 21.99999999999999, 20, 20.99999999999999, 21, 21, 21.99999999999999, 22, 22.00000000000001, 22.99999999999999, 24, 24;
 23.99999999999999, 24.5, 24.99999999999998, 24.50000000000001, 23.99999999999999, 21.99999999999999, 20.00000000000002, 21.99999999999999, 23.00000000000001, 23, 24, 23.99999999999999, 23.00000000000001, 22, 22.5, 23;
 24.5, 24.5, 24, 24.00000000000001, 22.99999999999999, 22, 22.00000000000002, 22.99999999999999, 24.5, 24.50000000000001, 23, 21.49999999999999, 21.00000000000001, 21.00000000000001, 22.5, 22.5;
 23.5, 24.5, 25.49999999999998, 25, 23.49999999999999, 24.49999999999999, 26.00000000000001, 25.49999999999999, 24, 25, 22.5, 20.5, 20.5, 21, 21, 20.50000000000001;
 24.5, 25.50000000000002, 25.99999999999999, 25.5, 24.5, 24.99999999999998, 25.5, 24.99999999999999, 24, 24, 21.99999999999998, 20.49999999999999, 20.49999999999999, 19.5, 18.49999999999999, 19.5;
 25.49999999999999, 25.49999999999999, 25.99999999999998, 25.99999999999999, 24.99999999999998, 24.99999999999998, 25, 24.99999999999998, 23.49999999999999, 24, 22, 20, 20.00000000000001, 19.50000000000001, 18.5, 21;
 24, 24, 23.99999999999998, 24.00000000000001, 23.99999999999998, 24.49999999999999, 23, 22.49999999999999, 24, 24.50000000000001, 23, 20.49999999999999, 20.00000000000001, 20.00000000000001, 20.5, 22.5;
 24.00000000000002, 24.00000000000003, 24.00000000000001, 24.00000000000003, 24, 24, 23.00000000000003, 22.5, 24.00000000000002, 23.50000000000001, 23.00000000000001, 21.50000000000002, 20.00000000000003, 19.50000000000003, 22.00000000000002, 24.00000000000002;
 22.5, 23.00000000000001, 22.49999999999998, 22, 22.99999999999999, 22.99999999999999, 22.00000000000001, 21.49999999999999, 23.00000000000001, 22.00000000000001, 22.00000000000001, 22, 20.00000000000001, 20.50000000000001, 23.00000000000001, 24.00000000000001;
 22.50000000000001, 21.50000000000002, 21.5, 22.00000000000001, 23, 23.99999999999999, 22.50000000000001, 20, 21.00000000000001, 20.50000000000001, 20.5, 22, 22.00000000000001, 23, 23.5, 23.5;
 22.5, 21.00000000000001, 20.99999999999999, 21.00000000000001, 21, 22.99999999999999, 21.5, 19.99999999999999, 20, 20.5, 20.5, 21.99999999999999, 24.00000000000001, 24.00000000000001, 23.50000000000001, 23.50000000000001]
Reconstruction err: MIN= 0, MAX= 3.19744e-14, sum= [2.17426e-12, 0, 0, 0]

OpenCV(4.0.0-pre) Error: Assertion failed ((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())) in cv::Mat::at, file g:\lib\install\opencv\include\opencv2\core\mat.inl.hpp, line 1144 OpenCV: terminate handler is called! The last OpenCV error is: OpenCV(4.0.0-pre) Error: Assertion failed ((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())) in cv::Mat::at, file g:\lib\install\opencv\include\opencv2\core\mat.inl.hpp, line 1144 Appuyez sur une touche pour continuer...