位姿估计(二):基于PnP的相机位姿估计
PnP (Perspective-n-Point) 是求解3D到2D点运动的方法。他描述了当我们知道n个3D空间点(世界坐标系)以及它们的投影位置时,如何估计相机的姿态。
PnP问题有很多求解方法,例如用三对点估计位姿的P3P,直接线性变换 (DLT),EPnP (Efficient PnP),UPnP等。此外,还可以通过非线性优化的方式,构建最小二乘问题并迭代求解,也就是万金油式的Bundle Adjustment
opencv-python Code Example
# https://github.com/xucong-zhang/ETH-XGaze/blob/master/demo.py#L19-L26
def estimateHeadPose(landmarks, face_model, camera, distortion, iterate=True):
ret, rvec, tvec = cv2.solvePnP(face_model, landmarks, camera, distortion, flags=cv2.SOLVEPNP_EPNP)
## further optimize
if iterate:
ret, rvec, tvec = cv2.solvePnP(face_model, landmarks, camera, distortion, rvec, tvec, True)
return rvec, tvec
OpenCV (C++) Code Example
包含头文件:#include <opencv2/calib3d.hpp>
// https://github.com/spmallick/learnopencv/blob/master/HeadPose/headPose.cpp#L31-L42
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);
pose estimation pipeline
- find correspondence (2D-3D点对应)
- solve PnP
- PnP DLT解超定方程 (至少6个点)
- 非线性优化:BA优化
- Rodrigues
cv::Rodrigues(in, out)
: Converts a rotation matrix to a rotation vector or vice versa. (将旋转向量转为旋转矩阵,或反之)
Blog & Code 🔥🔥🔥
- https://learnopencv.com/head-pose-estimation-using-opencv-and-dlib/
- https://github.com/spmallick/learnopencv/tree/master/HeadPose
Visualize Head Pose
- 首先需要将旋转矩阵转为Euler角,TODO
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人