
正文
【Eigen开源库】linux系统如何安装使用Eigen库
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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(, ) ); // Nose tip
image_points.push_back( cv::Point2d(, ) ); // Chin
image_points.push_back( cv::Point2d(, ) ); // Left eye left corner
image_points.push_back( cv::Point2d(, ) ); // Right eye right corner
image_points.push_back( cv::Point2d(, ) ); // Left Mouth corner
image_points.push_back( cv::Point2d(, ) ); // 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/,im.rows/);
cv::Mat camera_matrix = (cv::Mat_<double>(,) << focal_length, , center.x, , focal_length, center.y, , , );
cv::Mat dist_coeffs = cv::Mat::zeros(,,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(,,1000.0)); projectPoints(nose_end_point3D, rotation_vector, translation_vector, camera_matrix, dist_coeffs, nose_end_point2D); for(int i=; i < image_points.size(); i++)
{
circle(im, image_points[i], , Scalar(,,), -);
} cv::line(im,image_points[], nose_end_point2D[], cv::Scalar(,,), ); 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(, , CV_64F);
cv::Rodrigues(rotation_vector, rMatrix);
Eigen::Matrix3d R;
R << rMatrix.at<double>(, ), rMatrix.at<double>(, ), rMatrix.at<double>(, ),
rMatrix.at<double>(, ), rMatrix.at<double>(, ), rMatrix.at<double>(, ),
rMatrix.at<double>(, ), rMatrix.at<double>(, ), rMatrix.at<double>(, );
Eigen::Vector3d eular_radian = R.eulerAngles(, , );//radian.
Eigen::Vector3d eular_angle = R.eulerAngles(, , )*.f/M_PI;//angle.
// Display image.
std::stringstream ss;
ss << eular_angle[];
std::string txt = "Pitch: " + ss.str();
cv::putText(im, txt, cv::Point(, ), 0.5,0.5, cv::Scalar(,,));
std::stringstream ss1;
ss1 << eular_angle[];
std::string txt1 = "Yaw: " + ss1.str();
cv::putText(im, txt1, cv::Point(, ), 0.5,0.5, cv::Scalar(,,));
std::stringstream ss2;
ss2 << eular_angle[];
std::string txt2 = "Roll: " + ss2.str();
cv::putText(im, txt2, cv::Point(, ), 0.5,0.5, cv::Scalar(,,));
cv::imshow("Output", im);
cv::waitKey();}
参考
1. https://blog.csdn.net/ttomchy/article/details/56859841
2. https://blog.csdn.net/qq_31806429/article/details/78844305
3. https://www.learnopencv.com/head-pose-estimation-using-opencv-and-dlib/
完







