Levenberg-Marquardt algorithm explained
Levenberg-Marquardt algorithm explained Read More »
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
double ramp(double x) { if (x > 0) return x; else return 0; } void unaryExprExample() { //unaryExpr is a const function so it will not give you the possibility to modify values in place Eigen::ArrayXd x = Eigen::ArrayXd::Random(5); std::cout<<x <<std::endl; x = x.unaryExpr([](double elem) // changed type of parameter { return elem < 0.0 ? 0.0 : 1.0; // return instead of assignment }); std::cout<<x <<std::endl; std::cout << x.unaryExpr(std::ptr_fun(ramp)) << std::endl; } |
Eigen unaryExpr (Function Pointer, Lambda Expression) Example Read More »
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
void qRDecomposition(Eigen::MatrixXd &A,Eigen::MatrixXd &Q, Eigen::MatrixXd &R) { /* A=QR Q: is orthogonal matrix-> columns of Q are orthonormal R: is upper triangulate matrix this is possible when columns of A are linearly indipendent */ Eigen::MatrixXd thinQ(A.rows(),A.cols() ), q(A.rows(),A.rows()); Eigen::HouseholderQR<Eigen::MatrixXd> householderQR(A); q = householderQR.householderQ(); thinQ.setIdentity(); Q = householderQR.householderQ() * thinQ; R=Q.transpose()*A; } void qRExample() { Eigen::MatrixXd A; A.setRandom(3,4); std::cout<<"A" <<std::endl; std::cout<<A <<std::endl; Eigen::MatrixXd Q(A.rows(),A.rows()); Eigen::MatrixXd R(A.rows(),A.cols()); /////////////////////////////////HouseholderQR//////////////////////// Eigen::MatrixXd thinQ(A.rows(),A.cols() ), q(A.rows(),A.rows()); Eigen::HouseholderQR<Eigen::MatrixXd> householderQR(A); q = householderQR.householderQ(); thinQ.setIdentity(); Q = householderQR.householderQ() * thinQ; std::cout << "HouseholderQR" <<std::endl; std::cout << "Q" <<std::endl; std::cout << Q <<std::endl; R = householderQR.matrixQR().template triangularView<Eigen::Upper>(); std::cout << R<<std::endl; std::cout << R.rows()<<std::endl; std::cout << R.cols()<<std::endl; R=Q.transpose()*A; // std::cout << "R" <<std::endl; // std::cout << R<<std::endl; std::cout << "A-Q*R" <<std::endl; std::cout << A-Q*R <<std::endl; /////////////////////////////////ColPivHouseholderQR//////////////////////// Eigen::ColPivHouseholderQR<Eigen::MatrixXd> colPivHouseholderQR(A.rows(), A.cols()); colPivHouseholderQR.compute(A); //R = colPivHouseholderQR.matrixR().template triangularView<Upper>(); R = colPivHouseholderQR.matrixR(); Q = colPivHouseholderQR.matrixQ(); std::cout << "ColPivHouseholderQR" <<std::endl; std::cout << "Q" <<std::endl; std::cout << Q <<std::endl; std::cout << "R" <<std::endl; std::cout << R <<std::endl; std::cout << "A-Q*R" <<std::endl; std::cout << A-Q*R <<std::endl; /////////////////////////////////FullPivHouseholderQR//////////////////////// std::cout << "FullPivHouseholderQR" <<std::endl; Eigen::FullPivHouseholderQR<Eigen::MatrixXd> fullPivHouseholderQR(A.rows(), A.cols()); fullPivHouseholderQR.compute(A); Q=fullPivHouseholderQR.matrixQ(); R=fullPivHouseholderQR.matrixQR().template triangularView<Eigen::Upper>(); std::cout << "Q" <<std::endl; std::cout << Q <<std::endl; std::cout << "R" <<std::endl; std::cout << R <<std::endl; std::cout << "A-Q*R" <<std::endl; std::cout << A-Q*R <<std::endl; } void lDUDecomposition() { /* L: lower triangular matrix L U: upper triangular matrix U D: is a diagonal matrix A=LDU */ } void lUDecomposition() { /* L: lower triangular matrix L U: upper triangular matrix U A=LU */ } double exp(double x) // the functor we want to apply { std::setprecision(5); return std::trunc(x); } void gramSchmidtOrthogonalization(Eigen::MatrixXd &matrix,Eigen::MatrixXd &orthonormalMatrix) { /* In this method you make every column perpendicular to it's previous columns, here if a and b are representation vector of two columns, c=b-((b.a)/|a|).a ^ / b / / / ----------> a ^ /| b / | / | c / | ----------> a you just have to normilze every vector after make it perpendicular to previous columns so: q1=a.normalized(); q2=b-(b.q1).q1 q2=q2.normalized(); q3=c-(c.q1).q1 - (c.q2).q2 q3=q3.normalized(); Now we have Q, but we want A=QR so we just multiply both side by Q.transpose(), since Q is orthonormal, Q*Q.transpose() is I A=QR; Q.transpose()*A=R; */ Eigen::VectorXd col; for(int i=0;i<matrix.cols();i++) { col=matrix.col(i); col=col.normalized(); for(int j=0;j<i-1;j++) { //orthonormalMatrix.col(i) } orthonormalMatrix.col(i)=col; } Eigen::MatrixXd A(4,3); A<<1,2,3,-1,1,1,1,1,1,1,1,1; Eigen::Vector4d a=A.col(0); Eigen::Vector4d b=A.col(1); Eigen::Vector4d c=A.col(2); Eigen::Vector4d q1= a.normalized(); Eigen::Vector4d q2=b-(b.dot(q1))*q1; q2=q2.normalized(); Eigen::Vector4d q3=c-(c.dot(q1))*q1 - (c.dot(q2))*q2; q3=q3.normalized(); std::cout<< "q1:"<<std::endl; std::cout<< q1<<std::endl; std::cout<< "q2"<<std::endl; std::cout<< q2<<std::endl; std::cout<< "q3:"<<std::endl; std::cout<< q3<<std::endl; Eigen::MatrixXd Q(4,3); Q.col(0)=q1; Q.col(1)=q2; Q.col(2)=q3; Eigen::MatrixXd R(3,3); R=Q.transpose()*(A); std::cout<<"Q"<<std::endl; std::cout<< Q<<std::endl; std::cout<<"R"<<std::endl; std::cout<< R.unaryExpr(std::ptr_fun(exp))<<std::endl; //MatrixXd A(4,3), thinQ(4,3), Q(4,4); Eigen::MatrixXd thinQ(4,3), q(4,4); //A.setRandom(); Eigen::HouseholderQR<Eigen::MatrixXd> qr(A); q = qr.householderQ(); thinQ.setIdentity(); thinQ = qr.householderQ() * thinQ; std::cout << "Q computed by Eigen" << "\n\n" << thinQ << "\n\n"; std::cout << q << "\n\n" << thinQ << "\n\n"; } void gramSchmidtOrthogonalizationExample() { Eigen::MatrixXd matrix(3,4),orthonormalMatrix(3,4) ; matrix=Eigen::MatrixXd::Random(3,4);////A.setRandom(); gramSchmidtOrthogonalization(matrix,orthonormalMatrix); } void choleskyDecompositionExample() { /* Positive-definite matrix: Matrix Mnxn is said to be positive definite if the scalar zTMz is strictly positive for every non-zero column vector z n real numbers. zTMz>0 Hermitian matrix: Matrix Mnxn is said to be positive definite if the scalar z*Mz is strictly positive for every non-zero column vector z n real numbers. z*Mz>0 z* is the conjugate transpose of z. Positive semi-definite same as above except zTMz>=0 or z*Mz>=0 Example: ┌2 -1 0┐ M= |-1 2 -1| |0 - 1 2| └ ┘ ┌ a ┐ z=| b | | c | └ ┘ zTMz=a^2 +c^2+ (a-b)^2+ (b-c)^2 Cholesky decomposition: Cholesky decomposition of a Hermitian positive-definite matrix A is: A=LL* L is a lower triangular matrix with real and positive diagonal entries L* is the conjugate transpose of L */ Eigen::MatrixXd A(3,3); A << 6, 0, 0, 0, 4, 0, 0, 0, 7; Eigen::MatrixXd L( A.llt().matrixL() ); Eigen::MatrixXd L_T=L.adjoint();//conjugate transpose std::cout << "L" << std::endl; std::cout << L << std::endl; std::cout << "L_T" << std::endl; std::cout << L_T << std::endl; std::cout << "A" << std::endl; std::cout << A << std::endl; std::cout << "L*L_T" << std::endl; std::cout << L*L_T << std::endl; } |
Matrix Decomposition with Eigen: QR, Cholesky Decomposition LU, UL Read More »
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 |
std::cout <<"/////////////////Definition////////////////"<<std::endl; /* Definition of vectors and matrices in Eigen comes in the following form: Eigen::MatrixSizeType Eigen::VectorSizeType Eigen::ArraySizeType Size can be 2,3,4 for fixed size square matrices or X for dynamic size Type can be: i for integer, f for float, d for double, c for complex, cf for complex float, cd for complex double. */ // Vector3f is a fixed column vector of 3 floats: Eigen::Vector3f objVector3f; // RowVector2i is a fixed row vector of 3 integer: Eigen::RowVector2i objRowVector2i; // VectorXf is a column vector of size 10 floats: Eigen::VectorXf objv(10); Eigen::Matrix4d m; // 4x4 double Eigen::Matrix4cd objMatrix4cd; // 4x4 double complex //a is a 3x3 matrix, with a static float[9] array of uninitialized coefficients, Eigen::Matrix3f a; //b is a dynamic-size matrix whose size is currently 0x0, and whose array of coefficients hasn't yet been allocated at all. Eigen::MatrixXf b; //A is a 10x15 dynamic-size matrix, with allocated but currently uninitialized coefficients. Eigen::MatrixXf A(10, 15); //V is a dynamic-size vector of size 30, with allocated but currently uninitialized coefficients. Eigen::VectorXf V(30); //Template style definition // Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> &matrix Eigen::Matrix<double, 2, 3> my_matrix; my_matrix << 1, 2, 3, 4, 5, 6; // ArrayXf Eigen::Array<float, Eigen::Dynamic, 1> a1; // Array3f Eigen::Array<float, 3, 1> a2; // ArrayXXd Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> a3; // Array33d Eigen::Array<double, 3, 3> a4; Eigen::Matrix3d matrix_from_array = a4.matrix(); std::cout <<"///////////////////Initialization//////////////////"<< std::endl; Eigen::Matrix2d a_2d; a_2d.setRandom(); a_2d.setConstant(4.3); Eigen::MatrixXd identity=Eigen::MatrixXd::Identity(6,6); Eigen::MatrixXd zeros=Eigen::MatrixXd::Zero(3, 3); Eigen::ArrayXXf table(10, 4); table.col(0) = Eigen::ArrayXf::LinSpaced(10, 0, 90); std::cout <<"/////////////Matrix Coefficient Wise Operations///////////////////"<< std::endl; int i,j; std::cout << my_matrix << std::endl; std::cout << my_matrix.transpose()<< std::endl; std::cout<<my_matrix.minCoeff(&i, &j)<<std::endl; std::cout<<my_matrix.maxCoeff(&i, &j)<<std::endl; std::cout<<my_matrix.prod()<<std::endl; std::cout<<my_matrix.sum()<<std::endl; std::cout<<my_matrix.mean()<<std::endl; std::cout<<my_matrix.trace()<<std::endl; std::cout<<my_matrix.colwise().mean()<<std::endl; std::cout<<my_matrix.rowwise().maxCoeff()<<std::endl; std::cout<<my_matrix.lpNorm<2>()<<std::endl; std::cout<<my_matrix.lpNorm<Eigen::Infinity>()<<std::endl; std::cout<<(my_matrix.array()>0).all()<<std::endl;// if all elemnts are positive std::cout<<(my_matrix.array()>2).any()<<std::endl;//if any element is greater than 2 std::cout<<(my_matrix.array()>1).count()<<std::endl;// count the number of elements greater than 1 std::cout << my_matrix.array() - 2 << std::endl; std::cout << my_matrix.array().abs() << std::endl; std::cout << my_matrix.array().square() << std::endl; std::cout << my_matrix.array() * my_matrix.array() << std::endl; std::cout << my_matrix.array().exp() << std::endl; std::cout << my_matrix.array().log() << std::endl; std::cout << my_matrix.array().sqrt() << std::endl; std::cout <<"//////////////////Block Elements Access////////////////////"<< std::endl; //Block of size (p,q), starting at (i,j) matrix.block(i,j,p,q) Eigen::MatrixXf mat(4, 4); mat << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16; std::cout << "Block in the middle" << std::endl; std::cout << mat.block<2, 2>(1, 1) << std::endl; for (int i = 1; i <= 3; ++i) { std::cout << "Block of size " << i << "x" << i << std::endl; std::cout << mat.block(0, 0, i, i) << std::endl; } /////////////build matrix from vector, resizing matrix, dynamic size //////////////// Eigen::MatrixXd dynamicMatrix; int rows, cols; rows=3; cols=2; dynamicMatrix.resize(rows,cols); dynamicMatrix<<-1,7,3,4,5,1; //If you want a conservative variant of resize() which does not change the coefficients, use conservativeResize() dynamicMatrix.conservativeResize(dynamicMatrix.rows(), dynamicMatrix.cols()+1); dynamicMatrix.col(dynamicMatrix.cols()-1) = Eigen::Vector3d(1, 4, 0); dynamicMatrix.conservativeResize(dynamicMatrix.rows(), dynamicMatrix.cols()+1); dynamicMatrix.col(dynamicMatrix.cols()-1) = Eigen::Vector3d(5, -8, 6); /* you should expect this: 1 7 1 5 3 4 4 -8 5 1 0 6 */ std::cout<< dynamicMatrix<<std::endl; |
n this tutorial, I created an ellipsoid in 3D space and create two cameras in right and left and captured the image:
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 |
cv::Mat leftCameraRotation,rightCameraRotation; double rollLeft, pitchLeft, yawLeft,rollRight, pitchRight, yawRight ,txLeft,tyLeft,tzLeft,txRight,tyRight,tzRight; cv::Vec3d thetaLeft,thetaRight; rollLeft=0 ; pitchLeft=+M_PI/10; yawLeft=0; thetaLeft[0]=rollLeft; thetaLeft[1]=pitchLeft; thetaLeft[2]=yawLeft; rollRight=0; pitchRight= -M_PI/10; yawRight= 0; thetaRight[0]=rollRight; thetaRight[1]=pitchRight; thetaRight[2]=yawRight; txLeft=-1; tyLeft=0.0; tzLeft=-4.0; txRight=1.0; tyRight=0.0; tzRight=-4.0; leftCameraRotation =eulerAnglesToRotationMatrix(thetaLeft); rightCameraRotation =eulerAnglesToRotationMatrix(thetaRight); cv::Mat leftCameraTranslation = (cv::Mat_<double>(3,1) <<txLeft,tyLeft,tzLeft); cv::Mat rightCameraTranslation = (cv::Mat_<double>(3,1) <<txRight,tyRight,tzRight); std::vector<cv::Point3d> objectPointsInWorldCoordinate; double X,Y,Z,radius; ////////////////////////////////////////creating ellipsoid in the world coordinate/////////////////////// double phiStepSize,thetaStepSize; phiStepSize=0.7; thetaStepSize=0.6; double a,b,c; a=2; b=3; c=1.6; for(double phi=-M_PI;phi<M_PI;phi=phi+phiStepSize) { for(double theta=-M_PI/2;theta<M_PI/2;theta=theta+thetaStepSize) { X=a*cos(theta)*cos(phi); Y=b*cos(theta)*sin(phi); Z=c*sin(theta); objectPointsInWorldCoordinate.push_back(cv::Point3d(X, Y, Z)); } } int numberOfPixelInHeight,numberOfPixelInWidth; double heightOfSensor, widthOfSensor; double focalLength=2.0; double mx, my, U0, V0; numberOfPixelInHeight=600; numberOfPixelInWidth=600; heightOfSensor=10; widthOfSensor=10; my=(numberOfPixelInHeight)/heightOfSensor ; U0=(numberOfPixelInHeight)/2 ; mx=(numberOfPixelInWidth)/widthOfSensor; V0=(numberOfPixelInWidth)/2; cv::Mat K = (cv::Mat_<double>(3,3) << focalLength*mx, 0, V0, 0,focalLength*my,U0, 0,0,1); std::vector<cv::Point2d> imagePointsLeftCamera,imagePointsRightCamera; cv::projectPoints(objectPointsInWorldCoordinate, leftCameraRotation, leftCameraTranslation, K, cv::noArray(), imagePointsLeftCamera); cv::projectPoints(objectPointsInWorldCoordinate, rightCameraRotation, rightCameraTranslation, K, cv::noArray(), imagePointsRightCamera); ////////////////////////////////////////////////storing images from right and left camera////////////////////////////////////////////// std::string fileName; cv::Mat rightImage,leftImage; int U,V; leftImage=cv::Mat::zeros(numberOfPixelInHeight,numberOfPixelInWidth,CV_8UC1); for(std::size_t i=0;i<imagePointsLeftCamera.size();i++) { V=int(imagePointsLeftCamera.at(i).x); U=int(imagePointsLeftCamera.at(i).y); leftImage.at<char>(U,V)=255; } fileName=std::string("imagePointsLeftCamera")+std::to_string(focalLength)+ std::string("_.jpg"); cv::imwrite(fileName,leftImage); rightImage=cv::Mat::zeros(numberOfPixelInHeight,numberOfPixelInWidth,CV_8UC1); for(std::size_t i=0;i<imagePointsRightCamera.size();i++) { V=int(imagePointsRightCamera.at(i).x); U=int(imagePointsRightCamera.at(i).y); rightImage.at<char>(U,V)=255; } fileName=std::string("imagePointsRightCamera")+std::to_string(focalLength)+ std::string("_.jpg"); cv::imwrite(fileName,rightImage); |
Creating a Simulated Stereo Vision Cameras With OpenCV and C++ Read More »