【Eigen开源库】linux系统如何安装使用Eigen库
1.安装eigen.
sudo apt-get install libeigen3-dev
默认安装路径是/usr/include/eigen3
文件包含
/usr/include/eigen3$ ls Eigen signature_of_eigen3_matrix_library unsupported
使用的时候需要将目录中的Eigen文件夹复制到/usr/inlcude目录;
/usr/include/eigen3$ sudo cp Eigen/ .. -R
2. 简单代码测试.
#include <iostream> #include <Eigen/Dense> using Eigen::MatrixXd; int main() { MatrixXd m(2,2); m(0,0) = 3; m(1,0) = 2.5; m(0,1) = -1; m(1,1) = m(1,0) + m(0,1); std::cout << m << std::endl; return 0; } |
编译链接生成可执行文件
1 | g++ myeigen.cpp -o myeigen |
3. 应用实例.
code

/* * File : haedPose.cpp * Coder: * Date : 20181126 * Refer: https://www.learnopencv.com/head-pose-estimation-using-opencv-and-dlib/ */ #include <opencv2/opencv.hpp> #include <Eigen/Eigen> using namespace std; using namespace cv; int main(int argc, char **argv) { // Read input image cv::Mat im = cv::imread("headPose.jpg"); // 2D image points. If you change the image, you need to change vector std::vector<cv::Point2d> image_points; image_points.push_back( cv::Point2d(359, 391) ); // Nose tip image_points.push_back( cv::Point2d(399, 561) ); // Chin image_points.push_back( cv::Point2d(337, 297) ); // Left eye left corner image_points.push_back( cv::Point2d(513, 301) ); // Right eye right corner image_points.push_back( cv::Point2d(345, 465) ); // Left Mouth corner image_points.push_back( cv::Point2d(453, 469) ); // Right mouth corner // 3D model points. std::vector<cv::Point3d> model_points; model_points.push_back(cv::Point3d(0.0f, 0.0f, 0.0f)); // Nose tip model_points.push_back(cv::Point3d(0.0f, -330.0f, -65.0f)); // Chin model_points.push_back(cv::Point3d(-225.0f, 170.0f, -135.0f)); // Left eye left corner model_points.push_back(cv::Point3d(225.0f, 170.0f, -135.0f)); // Right eye right corner model_points.push_back(cv::Point3d(-150.0f, -150.0f, -125.0f)); // Left Mouth corner model_points.push_back(cv::Point3d(150.0f, -150.0f, -125.0f)); // Right mouth corner // Camera internals double focal_length = im.cols; // Approximate focal length. Point2d center = cv::Point2d(im.cols/2,im.rows/2); cv::Mat camera_matrix = (cv::Mat_<double>(3,3) << focal_length, 0, center.x, 0 , focal_length, center.y, 0, 0, 1); cv::Mat dist_coeffs = cv::Mat::zeros(4,1,cv::DataType<double>::type); // Assuming no lens distortion cout << "Camera Matrix " << endl << camera_matrix << endl ; // Output rotation and translation cv::Mat rotation_vector; // Rotation in axis-angle form cv::Mat translation_vector; // Solve for pose cv::solvePnP(model_points, image_points, camera_matrix, dist_coeffs, rotation_vector, translation_vector); // Project a 3D point (0, 0, 1000.0) onto the image plane. // We use this to draw a line sticking out of the nose vector<Point3d> nose_end_point3D; vector<Point2d> nose_end_point2D; nose_end_point3D.push_back(Point3d(0,0,1000.0)); projectPoints(nose_end_point3D, rotation_vector, translation_vector, camera_matrix, dist_coeffs, nose_end_point2D); for(int i=0; i < image_points.size(); i++) { circle(im, image_points[i], 3, Scalar(0,255,255), -1); } cv::line(im,image_points[0], nose_end_point2D[0], cv::Scalar(255,255,0), 2); cout << "Rotation Vector " << endl << rotation_vector << endl; cout << "Translation Vector" << endl << translation_vector << endl; cout << nose_end_point2D << endl; //Eigen. cv::Mat rMatrix = cv::Mat(3, 3, CV_64F); cv::Rodrigues(rotation_vector, rMatrix); Eigen::Matrix3d R; R << rMatrix.at<double>(0, 0), rMatrix.at<double>(0, 1), rMatrix.at<double>(0, 2), rMatrix.at<double>(1, 0), rMatrix.at<double>(1, 1), rMatrix.at<double>(1, 2), rMatrix.at<double>(2, 0), rMatrix.at<double>(2, 1), rMatrix.at<double>(2, 2); Eigen::Vector3d eular_radian = R.eulerAngles(0, 1, 2);//radian. Eigen::Vector3d eular_angle = R.eulerAngles(0, 1, 2)*180.f/M_PI;//angle. // Display image. std::stringstream ss; ss << eular_angle[0]; std::string txt = "Pitch: " + ss.str(); cv::putText(im, txt, cv::Point(60, 20), 0.5,0.5, cv::Scalar(0,0,255)); std::stringstream ss1; ss1 << eular_angle[1]; std::string txt1 = "Yaw: " + ss1.str(); cv::putText(im, txt1, cv::Point(60, 40), 0.5,0.5, cv::Scalar(0,0,255)); std::stringstream ss2; ss2 << eular_angle[2]; std::string txt2 = "Roll: " + ss2.str(); cv::putText(im, txt2, cv::Point(60, 60), 0.5,0.5, cv::Scalar(0,0,255)); cv::imshow("Output", im); cv::waitKey(0); }
4. 基于opencv的矩阵乘法;

Eigen::MatirxXf tmp1(values.rows, values.cols); cv2eigen(values, tmp1); Eigen::MatirxXf tmp2(this->weights.rows, this->weights.cols); cv2eigen(this->weights, tmp2); Eigen::MatrixXf tmp = tmp1 * tmp2; cv::Mat out; eigen2cv(tmp, out); std::cout << "out.type: " << out.type() << std::endl; out.convertTo(out, CV_32FC1); return out; // return values*this->weights;
使用eigen实现矩阵乘法之后,发现效率并没有提升。
5. eigen版本查询.
1 | vi /usr/include/eigen3/Eigen/src/Core/util/Macros .h |
参考
2. 位姿估计_1;
3. learnopencv_head-pose-estimation-using-opencv-and-dlib;
4. eigen_document;
完
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
分类:
c/c++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】