headPose.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <opencv2/opencv.hpp>
  2. using namespace std;
  3. using namespace cv;
  4. int main(int argc, char **argv)
  5. {
  6. // Read input image
  7. cv::Mat im = cv::imread("headPose.jpg");
  8. // 2D image points. If you change the image, you need to change vector
  9. std::vector<cv::Point2d> image_points;
  10. image_points.push_back( cv::Point2d(359, 391) ); // Nose tip
  11. image_points.push_back( cv::Point2d(399, 561) ); // Chin
  12. image_points.push_back( cv::Point2d(337, 297) ); // Left eye left corner
  13. image_points.push_back( cv::Point2d(513, 301) ); // Right eye right corner
  14. image_points.push_back( cv::Point2d(345, 465) ); // Left Mouth corner
  15. image_points.push_back( cv::Point2d(453, 469) ); // Right mouth corner
  16. // 3D model points.
  17. std::vector<cv::Point3d> model_points;
  18. model_points.push_back(cv::Point3d(0.0f, 0.0f, 0.0f)); // Nose tip
  19. model_points.push_back(cv::Point3d(0.0f, -330.0f, -65.0f)); // Chin
  20. model_points.push_back(cv::Point3d(-225.0f, 170.0f, -135.0f)); // Left eye left corner
  21. model_points.push_back(cv::Point3d(225.0f, 170.0f, -135.0f)); // Right eye right corner
  22. model_points.push_back(cv::Point3d(-150.0f, -150.0f, -125.0f)); // Left Mouth corner
  23. model_points.push_back(cv::Point3d(150.0f, -150.0f, -125.0f)); // Right mouth corner
  24. // Camera internals
  25. double focal_length = im.cols; // Approximate focal length.
  26. Point2d center = cv::Point2d(im.cols/2,im.rows/2);
  27. cv::Mat camera_matrix = (cv::Mat_<double>(3,3) << focal_length, 0, center.x, 0 , focal_length, center.y, 0, 0, 1);
  28. cv::Mat dist_coeffs = cv::Mat::zeros(4,1,cv::DataType<double>::type); // Assuming no lens distortion
  29. cout << "Camera Matrix " << endl << camera_matrix << endl ;
  30. // Output rotation and translation
  31. cv::Mat rotation_vector; // Rotation in axis-angle form
  32. cv::Mat translation_vector;
  33. // Solve for pose
  34. cv::solvePnP(model_points, image_points, camera_matrix, dist_coeffs, rotation_vector, translation_vector);
  35. // Project a 3D point (0, 0, 1000.0) onto the image plane.
  36. // We use this to draw a line sticking out of the nose
  37. vector<Point3d> nose_end_point3D;
  38. vector<Point2d> nose_end_point2D;
  39. nose_end_point3D.push_back(Point3d(0,0,1000.0));
  40. projectPoints(nose_end_point3D, rotation_vector, translation_vector, camera_matrix, dist_coeffs, nose_end_point2D);
  41. for(int i=0; i < image_points.size(); i++)
  42. {
  43. circle(im, image_points[i], 3, Scalar(0,0,255), -1);
  44. }
  45. cv::line(im,image_points[0], nose_end_point2D[0], cv::Scalar(255,0,0), 2);
  46. cout << "Rotation Vector " << endl << rotation_vector << endl;
  47. cout << "Translation Vector" << endl << translation_vector << endl;
  48. cout << nose_end_point2D << endl;
  49. // Display image.
  50. cv::imshow("Output", im);
  51. cv::waitKey(0);
  52. }