Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include "opencv2/opencv.hpp"
  2. #include <fstream>
  3. #include <vector>
  4. void CalcBoardCornerPositions(cv::Size boardSize, std::vector<cv::Point3f>& corners) {
  5.     corners.clear();
  6.     for (int i = 0; i < boardSize.height; ++i)
  7.         for (int j = 0; j < boardSize.width; ++j)
  8.             corners.push_back(cv::Point3f(float(j), float(i), 0));
  9. }
  10. int main() {
  11.  
  12.     std::vector<cv::Point2f> left_corners;
  13.     std::vector<cv::Point2f> right_corners;
  14.  
  15.     cv::VideoCapture cam1(1);
  16.     cv::VideoCapture cam2(2);
  17.  
  18.     cv::namedWindow("Left");
  19.     cv::namedWindow("Right");
  20.     cv::Mat frame1, frame2;
  21.     cv::Mat cameraMatrix[2], distCoeffs[2];
  22.     cv::Mat R, T, E, F;
  23.     cv::Size boardSize(9, 6);
  24.     double rms;
  25.  
  26.     std::vector<cv::Point3f> obj;
  27.     CalcBoardCornerPositions(boardSize, obj);
  28.     std::vector<std::vector<cv::Point2f> >imagePoints1;
  29.     std::vector<std::vector<cv::Point2f> >imagePoints2;
  30.     std::vector<std::vector<cv::Point3f> >objectPoints;
  31.     int success = -1;
  32.  
  33.     while (1) {
  34.         cam1 >> frame1;
  35.         cam2 >> frame2;
  36.  
  37.         bool foundLeft = false, foundRight = false;
  38.  
  39.         cv::Mat cameraMatrix[2], distCoeffs[2];
  40.         cameraMatrix[0] = cv::Mat::eye(3, 3, CV_64F);
  41.         cameraMatrix[1] = cv::Mat::eye(3, 3, CV_64F);
  42.  
  43.         foundLeft = cv::findChessboardCorners(frame1, boardSize, left_corners);
  44.         foundRight = cv::findChessboardCorners(frame2, boardSize, right_corners);
  45.         cv::drawChessboardCorners(frame1, boardSize, left_corners, foundLeft);
  46.         cv::drawChessboardCorners(frame2, boardSize, right_corners, foundRight);
  47.         if (foundLeft && foundRight) {
  48.             imagePoints1.push_back(left_corners);
  49.             imagePoints2.push_back(left_corners);
  50.             objectPoints.push_back(obj);
  51.             printf("Corners stored\n");
  52.             ++success;
  53.         }
  54.         cv::imshow("Left", frame1);
  55.         cv::imshow("Right", frame2);
  56.  
  57.         if (cv::waitKey(1000) == 32 || success > 10) break;
  58.  
  59.     }
  60.  
  61.     std::ofstream file("param.txt");
  62.  
  63.     rms = cv::stereoCalibrate(objectPoints, imagePoints1, imagePoints2,
  64.         cameraMatrix[0], distCoeffs[0],
  65.         cameraMatrix[1], distCoeffs[1],
  66.         frame1.size(), R, T, E, F);
  67.     file << "RMS Error: " << rms << "\n";
  68.     file << "global_rms: " << rms << std::endl;
  69.     file << "global_R: " << R << std::endl;
  70.     file << "global_T: " << T << std::endl;
  71.     file << "global_E: " << E << std::endl;
  72.     file << "global_F: " << F << std::endl;
  73.     file << "global_cameraMatrix[0]: " << cameraMatrix[0] << std::endl;
  74.     file << "global_cameraMatrix[1]: " << cameraMatrix[0] << std::endl;
  75.     file << "global_distCoeffs[0]: " << distCoeffs[0] << std::endl;
  76.     file << "global_distCoeffs[1]: " << distCoeffs[1] << std::endl;
  77.     file.close();
  78.     cv::Mat R1, R2, P1, P2, Q;
  79.     cv::Rect validROI[2];
  80.     stereoRectify(cameraMatrix[0], distCoeffs[0], cameraMatrix[1], distCoeffs[1], frame1.size(),
  81.         R, T, R1, R2, P1, P2, Q, cv::CALIB_ZERO_DISPARITY, 1, frame1.size(), &validROI[0], &validROI[1]);
  82.  
  83.     cv::Mat rmap[2][2];
  84.     cv::Size imageSize = frame1.size();
  85.     cv::initUndistortRectifyMap(cameraMatrix[0], distCoeffs[0], R1, P1, imageSize, CV_16SC2, rmap[0][0], rmap[0][1]);
  86.     cv::initUndistortRectifyMap(cameraMatrix[1], distCoeffs[1], R2, P2, imageSize, CV_16SC2, rmap[1][0], rmap[1][1]);
  87.     cv::Mat canvas;
  88.     double sf;
  89.     int w, h;
  90.     w = cvRound(imageSize.width);
  91.     h = cvRound(imageSize.height);
  92.     canvas.create(h, w * 2, CV_8UC3);
  93.  
  94.  
  95.     cv::namedWindow("rectified");
  96.  
  97.     imshow("rectified", canvas);
  98.  
  99.  
  100.     for (int j = 0; j < 2; j++) {
  101.  
  102.         cv::Mat img, rimg, cimg;
  103.         if (j == 0)
  104.             img = frame1;
  105.         else
  106.             img = frame2;
  107.         remap(img, rimg, rmap[j][0], rmap[j][1], cv::INTER_LINEAR);
  108.         cimg = rimg;
  109.         cv::Mat canvasPart = canvas(cv::Rect(w*j, 0, w, h));
  110.         resize(cimg, canvasPart, canvasPart.size(), 0, 0, cv::INTER_AREA);
  111.         cv::Rect vroi(cvRound(validROI[j].x), cvRound(validROI[j].y),
  112.             cvRound(validROI[j].width), cvRound(validROI[j].height));
  113.         cv::rectangle(canvasPart, vroi, cv::Scalar(0, 0, 255), 3, 8);
  114.     }
  115.     for (int j = 0; j < canvas.rows; j += 16)
  116.         line(canvas, cv::Point(0, j), cv::Point(canvas.cols, j), cv::Scalar(0, 255, 0), 1, 8);
  117.     imshow("rectified", canvas);
  118.     cv::waitKey();
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement