Decomposing Projection Using OpenCV and C++
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
#include <opencv/cv.hpp> #include <opencv2/core.hpp> #include <opencv2/calib3d.hpp> #include "transformation.hpp" //https://www.cnblogs.com/shengguang/p/5932522.html void HouseHolderQR(const cv::Mat &A, cv::Mat &Q, cv::Mat &R) { assert ( A.channels() == 1 ); assert ( A.rows >= A.cols ); auto sign = [](double value) { return value >= 0 ? 1: -1; }; const auto totalRows = A.rows; const auto totalCols = A.cols; R = A.clone(); Q = cv::Mat::eye ( totalRows, totalRows, A.type() ); for ( int col = 0; col < A.cols; ++ col ) { cv::Mat matAROI = cv::Mat ( R, cv::Range ( col, totalRows ), cv::Range ( col, totalCols ) ); cv::Mat y = matAROI.col ( 0 ); auto yNorm = norm ( y ); cv::Mat e1 = cv::Mat::eye ( y.rows, 1, A.type() ); cv::Mat w = y + sign(y.at<double>(0,0)) * yNorm * e1; cv::Mat v = w / norm( w ); cv::Mat vT; cv::transpose(v, vT ); cv::Mat I = cv::Mat::eye( matAROI.rows, matAROI.rows, A.type() ); cv::Mat I_2VVT = I - 2 * v * vT; cv::Mat matH = cv::Mat::eye ( totalRows, totalRows, A.type() ); cv::Mat matHROI = cv::Mat(matH, cv::Range ( col, totalRows ), cv::Range ( col, totalRows ) ); I_2VVT.copyTo ( matHROI ); R = matH * R; Q = Q * matH; } } int main() { /* Thera are two notations for Projection Matrix 1)P=K[R|t] ┌X┐ ┌R,t┐ |Y| X_Cam=R*X+t ==> Homogeneous Coordinate ==> X_Cam=|0,1| |Z| └ ┘4x4└1┘4x1 ┌X┐ ┌ ┐ |Y| image plane <--x=K[I|0]3x4 |R,t| |Z| └0,1┘4x4 └1┘4x1 P=K[R|t] 2)P=KR[I|-X0] ┌X┐ ┌R -Rc┐|Y| X_Cam=R*[X_w-C] ==> Homogeneous Coordinate ==> | ||Z| └0 1 ┘└1┘ ┌X┐ ┌R -Rc┐|Y| image plane <--x=K[I|0]3x4 |0 1 ||Z| └ ┘└1┘ P=K[R|-RC]= KR[I|-C] (1) and (2) ==> t=-RC */ int numberOfPixelInHeight,numberOfPixelInWidth; double heightOfSensor, widthOfSensor; double focalLength=1.50; double mx, my, U0, V0; numberOfPixelInHeight=600; numberOfPixelInWidth=800; heightOfSensor=10; widthOfSensor=10; my=(numberOfPixelInHeight)/heightOfSensor ; U0=(numberOfPixelInHeight)/2 ; mx=(numberOfPixelInWidth)/widthOfSensor; V0=(numberOfPixelInWidth)/2; cv::Mat cameraMatrix = (cv::Mat_<double>(3,3) << focalLength*mx, 0, V0, 0,focalLength*my,U0, 0,0,1); double tx,ty,tz,roll,pitch,yaw; tx=1.0; ty=2.1; tz=-1.4; cv::Mat translation = (cv::Mat_<double>(3,1) <<tx,ty,tz); cv::Vec3d theta; roll=+M_PI/4 ; pitch=+M_PI/10; yaw=-+M_PI/6; theta[0]=roll; theta[1]=pitch; theta[2]=yaw; cv::Mat rotation=eulerAnglesToRotationMatrix(theta); //1)P=K[R|t] cv::Mat R_T; cv::hconcat(rotation, translation, R_T); cv::Mat projectionMatrix=cameraMatrix*R_T; cv::Mat calculatedCameraMatrix,calculatedRotation,calculatedTranslation; cv::decomposeProjectionMatrix(projectionMatrix,calculatedCameraMatrix,calculatedRotation,calculatedTranslation); std::cout<<"==============================Ground Truth==============================" <<std::endl; std::cout<<"Rotation Matrix (Ground Truth)" <<std::endl; std::cout<<rotation <<std::endl; std::cout<<"Translation Matrix (Ground Truth)" <<std::endl; std::cout<<translation <<std::endl; std::cout<<"Camera Matrix (Ground Truth)" <<std::endl; std::cout<<cameraMatrix <<std::endl; std::cout<<"==============================Decomposing Using OpenCV==============================" <<std::endl; std::cout<<"Computed Rotation Matrix (OpenCV)" <<std::endl; std::cout<<calculatedRotation <<std::endl; std::cout<<"Computed Translation Matrix (OpenCV)" <<std::endl; //std::cout<<calculatedTranslation/calculatedTranslation.at<double>(3,0) <<std::endl; cv::Mat tempT=(cv::Mat_<double>(3,1)<< calculatedTranslation.at<double>(0,0)/calculatedTranslation.at<double>(3,0), calculatedTranslation.at<double>(1,0)/calculatedTranslation.at<double>(3,0), calculatedTranslation.at<double>(2,0)/calculatedTranslation.at<double>(3,0)); std::cout<<-rotation*tempT<<std::endl; std::cout<<"Computed Camera Matrix (OpenCV)" <<std::endl; std::cout<<calculatedCameraMatrix <<std::endl; ///////////////////////////////////////decomposing the projection matrix/////////////////////////////////////////// /* P=KR[I|-X0]=[H_inf3x3|h3x1] KR=H_inf3x3 1)X0 -KRX0=h3x1 => X0=-(KR)^-1*h3x1 ==>X0=-(H_inf3x3)^-1*h3x1 2)K,R KR=H_inf3x3 =>(KR)^-1= H_inf3x3^-1 =>R^-1*K^-1=H_inf3x3^-1 | R^-1*K^-1=Q*R => R=Q^-1, K=R^-1 H_inf3x3^-1=Q*R | */ cv::Mat H_inf3x3=(cv::Mat_<double>(3,3)<< projectionMatrix.at<double>(0,0),projectionMatrix.at<double>(0,1),projectionMatrix.at<double>(0,2) ,projectionMatrix.at<double>(1,0),projectionMatrix.at<double>(1,1),projectionMatrix.at<double>(1,2) ,projectionMatrix.at<double>(2,0),projectionMatrix.at<double>(2,1),projectionMatrix.at<double>(2,2)); cv::Mat h3x1=(cv::Mat_<double>(3,1)<<projectionMatrix.at<double>(0,3),projectionMatrix.at<double>(1,3),projectionMatrix.at<double>(2,3) ); cv::Mat Q,R; cv::Mat H_inf3x3_inv=H_inf3x3.inv(); //R=Q^-1, K=R^-1 HouseHolderQR(H_inf3x3_inv, Q, R); cv::Mat K=R.inv(); std::cout<<"==============================Decomposing Using My Code==============================" <<std::endl; //due to homogeneity we divide it by last element std::cout<<"Estimated Camera Matrix\n"<<K/K.at<double>(2,2) <<std::endl; cv::Mat rotationMatrix=Q.inv(); std::cout<<"Estimated Camera Rotation\n"<<rotationMatrix*-1 <<std::endl; std::cout<<"Estimated Camera Translation" <<std::endl; //t=-R*C, Q.inv()=R std::cout<<-1*(-Q.inv()*(-H_inf3x3.inv()*h3x1 ))<<std::endl; |
And the output is:
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 |
==============================Ground Truth============================== Rotation Matrix (Ground Truth) [0.823639103546332, 0.5427868801100539, -0.1643199010764935; -0.4755282581475767, 0.5031184295835893, -0.7216264418079997; -0.3090169943749474, 0.6724985119639573, 0.6724985119639574] Translation Matrix (Ground Truth) [1; 2.1; -1.4] Camera Matrix (Ground Truth) [120, 0, 400; 0, 90, 300; 0, 0, 1] ==============================Decomposing Using OpenCV============================== Computed Rotation Matrix (OpenCV) [0.823639103546332, 0.5427868801100539, -0.1643199010764936; -0.4755282581475769, 0.5031184295835893, -0.7216264418079997; -0.3090169943749474, 0.6724985119639573, 0.6724985119639574] Computed Translation Matrix (OpenCV) [0.9999999999999988; 2.1; -1.4] Computed Camera Matrix (OpenCV) [120, -1.4210854715202e-14, 400; 0, 90.00000000000001, 300; 0, 0, 1] ==============================Decomposing Using My Code============================== Estimated Camera Matrix [120, -5.776629175002766e-14, 399.9999999999999; -5.608036291626306e-15, 89.99999999999994, 299.9999999999999; -1.49192016182457e-17, -1.561251128379126e-16, 1] Estimated Camera Rotation [0.8236391035463322, 0.5427868801100542, -0.1643199010764936; -0.4755282581475767, 0.5031184295835892, -0.7216264418079998; -0.3090169943749475, 0.6724985119639573, 0.6724985119639575] Estimated Camera Translation [1; 2.099999999999999; -1.4] |
Decomposing Projection Using OpenCV and C++ Read More »