Blame view

src/RectifierAffine.cpp 4.34 KB
b0bb08d1c   tristan   init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  #include "RectifierAffine.hpp"
  
  RectifierAffine::RectifierAffine()
  {
  
  }
  
  RectifierAffine::~RectifierAffine()
  {
  
  }
  
  void RectifierAffine::init(cv::Mat*im1, cv::Mat *im2, cv::Mat *F, std::vector<cv::Point2d>* inliers1, std::vector<cv::Point2d>* inliers2) {
      _imL = im1;
      _imR = im2;
      _F = F;
  
      _inliers1 = inliers1;
      _inliers2 = inliers2;
  }
  
  void RectifierAffine::rectify()
  {
      std::cout << "Rectification started... " << std::endl;
      cv::Mat epR;
      cv::Mat epL ;
  
      cv::computeCorrespondEpilines(*_inliers1,1, *_F,epR);
      cv::computeCorrespondEpilines(*_inliers2, 2, *_F,epL);
  
  
      //std::cout << _F.toMat() << "sdsdsds
  
  " << _F.toMat().t() << "
  
  ";
  
      angleL = atan(-epL.at<double>(0)/epL.at<double>(1));
      angleR = atan(-epR.at<double>(0)/epR.at<double>(1));
  
      std::cout << "Angles (L,R) : (" << angleL << ", " << angleR << ")" << std::endl;
  
      cv::Mat Rl = get2DRotationMatrixLeft();
      cv::Mat Rr = get2DRotationMatrixRight();
      cv::warpAffine(*_imL, imLrect, Rl, _imL->size());
      cv::warpAffine(*_imR, imRrect, Rr, _imR->size());
      std::cout<< "ok"<< std::endl;
      //cv::cvtColor(imLrect, imLrect, CV_BGR2GRAY);
      //cv::cvtColor(imRrect, imRrect, CV_BGR2GRAY);
  
      cv::Mat imLrectShifted;
      cv::Mat imRrectShifted;
  
      std::vector<cv::Point2d> inliersLeftTransformed;
      std::vector<cv::Point2d> inliersRightTransformed;
  
      for(int i = 0; i < _inliers1->size(); i++) {
          cv::Mat tempPoint;
          tempPoint = (cv::Mat_<double>(3,1) << (*_inliers1)[i].x, (*_inliers1)[i].y, 1);
          tempPoint = Rl*tempPoint;
          inliersLeftTransformed.push_back(cv::Point2d(tempPoint.at<double>(0,0),tempPoint.at<double>(1,0)));
  
          tempPoint = (cv::Mat_<double>(3,1) << (*_inliers2)[i].x, (*_inliers2)[i].y, 1);
          tempPoint = Rr*tempPoint;
          inliersRightTransformed.push_back(cv::Point2d(tempPoint.at<double>(0,0),tempPoint.at<double>(1,0)));
      }
  
      cv::Mat rectErrors = cv::Mat::zeros(inliersLeftTransformed.size(), 1, cv::DataType<double>::type);
      for(int i = 0; i < inliersLeftTransformed.size(); i++) {
          rectErrors.at<double>(i,0) = inliersLeftTransformed[i].y - inliersRightTransformed[i].y;
      }
      cv::Scalar mean, std;
      cv::meanStdDev ( rectErrors, mean, std );
  
      shift = round(abs(mean[0]));
      std::cout<<shift<<std::endl;
      int rowNb;
    
      if (mean[0] > 0){ // features of 2nd are higher than the 1st - shift to the second
          imLrectShifted = imLrect;
          imRrectShifted = cv::Mat::zeros(shift,imRrect.cols, imRrect.type());
          imLrectShifted.push_back(imRrectShifted);
          imRrectShifted.push_back(imRrect);
          for (int i = 0; i < inliersRightTransformed.size(); i++) {
              inliersRightTransformed[i].y = inliersRightTransformed[i].y + shift;
          }
      }
      else{
          imRrectShifted = imRrect;
          imLrectShifted = cv::Mat::zeros(shift,imLrect.cols, imLrect.type());
          imRrectShifted.push_back(imLrectShifted);
          imLrectShifted.push_back(imLrect);
          for (int i = 0; i < inliersLeftTransformed.size(); i++) {
              inliersLeftTransformed[i].y = inliersLeftTransformed[i].y + shift;
          }
      }
      
  
      for(int i = 0; i < inliersLeftTransformed.size(); i++) {
          rectErrors.at<double>(i,0) = inliersLeftTransformed[i].y - inliersRightTransformed[i].y;
      }
      cv::meanStdDev ( rectErrors, mean, std );
  
      std::cout << "Rectification errors:" << std::endl;
      std::cout << "      mean: " << mean << std::endl;
      std::cout << "       std: " << std << std::endl;
  
      imLrect = imLrectShifted;
      imRrect = imRrectShifted;
      *_inliers1=inliersLeftTransformed;
      *_inliers2= inliersRightTransformed;
      std::cout << "Done" << std::endl;
      //cv::imwrite("_R01.jpg", imLrect);
      //cv::imwrite("_R02.jpg", imRrect);
  }
  
  bool RectifierAffine::isReady()
  {
      return (! _F->empty()) && (! this->_inliers1->empty()) && (! this->_inliers2->empty());
  }
  
  cv::Mat RectifierAffine::get2DRotationMatrixLeft()
  {
      return cv::getRotationMatrix2D(cv::Point2d(_imR->cols/2.0,_imR->rows/2.0),angleL*180.0/CV_PI,1);
  }
  
  cv::Mat RectifierAffine::get2DRotationMatrixRight()
  {
      return cv::getRotationMatrix2D(cv::Point2d(_imR->cols/2.0,_imR->rows/2.0),angleR*180.0/CV_PI,1);
  
  }
  
  cv::Mat RectifierAffine::get2DShiftMatrix()
  {
      cv::Mat shiftMat = cv::Mat::zeros( 3, 3, cv::DataType<double>::type);
      shiftMat.at<double>(1,2) = shift;
      return shiftMat;
  
  }