Advertisement
jack06215

[OpenCV] decomposeHomographyMat

Jul 9th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5.  
  6. int main()
  7. {
  8.     // set up a virtual camera
  9.     float f = 640;
  10.     float w = 640;
  11.     float h = 480;
  12.  
  13.     cv::Mat1f K = (cv::Mat1f(3, 3) <<
  14.         f, 0, w / 2,
  15.         0, f, h / 2,
  16.         0, 0, 1);
  17.  
  18.     // set transformation from 1st to 2nd camera (assume K is unchanged)
  19.     cv::Mat1f rvecDeg = (cv::Mat1f(3, 1) << 45, 12, 66);
  20.     cv::Mat1f t = (cv::Mat1f(3, 1) << 100, 200, 300);
  21.  
  22.     std::cout << "-------------------------------------------\n";
  23.     std::cout << "Ground truth:\n";
  24.  
  25.     std::cout << "K = \n" << K << std::endl << std::endl;
  26.     std::cout << "rvec = \n" << rvecDeg << std::endl << std::endl;
  27.     std::cout << "t = \n" << t << std::endl << std::endl;
  28.  
  29.     // set up points on a plane
  30.     std::vector<cv::Point3f> p3d{
  31.         { 0, 0, 10 },
  32.         { 100, 0, 10 },
  33.         { 0, 100, 10 },
  34.         { 100, 100, 10 }
  35.     };
  36.  
  37.     // project on both cameras
  38.     std::vector<cv::Point2f> Q, P;
  39.  
  40.     cv::projectPoints(p3d, cv::Mat1d::zeros(3, 1), cv::Mat1d::zeros(3, 1), K, cv::noArray(), Q);
  41.     cv::projectPoints(p3d, rvecDeg * CV_PI / 180, t, K, cv::noArray(), P);
  42.  
  43.     // find homography
  44.     cv::Mat H = cv::findHomography(Q, P);
  45.  
  46.     std::cout << "-------------------------------------------\n";
  47.     std::cout << "Estimated H = \n" << H << std::endl << std::endl;
  48.  
  49.  
  50.     // check by re-projection
  51.     std::vector<cv::Point2f> P_(P.size());
  52.     cv::perspectiveTransform(Q, P_, H);
  53.  
  54.     float sumError = 0;
  55.  
  56.     for (size_t i = 0; i < P.size(); i++)
  57.     {
  58.         sumError += cv::norm(P[i] - P_[i]);
  59.     }
  60.  
  61.     std::cout << "-------------------------------------------\n";
  62.     std::cout << "Average re-projection error = " << sumError / P.size() << std::endl << std::endl;
  63.  
  64.  
  65.     // decompose using identity as internal parameters matrix
  66.     std::vector<cv::Mat> Rs, Ts, Ns;
  67.     cv::decomposeHomographyMat(H, K, Rs, Ts, Ns);
  68.  
  69.     std::cout << "-------------------------------------------\n";
  70.     std::cout << "Estimated decomposition:\n\n";
  71.     std::cout << "rvec = " << std::endl;
  72.     for (auto R_ : Rs)
  73.     {
  74.         cv::Mat1d rvec;
  75.         cv::Rodrigues(R_, rvec);
  76.         std::cout << rvec * 180 / CV_PI << std::endl << std::endl;
  77.     }
  78.  
  79.     std::cout << std::endl;
  80.  
  81.     std::cout << "t = " << std::endl;
  82.     for (auto t_ : Ts)
  83.     {
  84.         std::cout << t_ << std::endl << std::endl;
  85.     }
  86.  
  87.     std::cout << std::endl;
  88.  
  89.     std::cout << "n = " << std::endl;
  90.     for (auto n_ : Ns)
  91.     {
  92.         std::cout << n_ << std::endl << std::endl;
  93.     }
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement