1. 摄像机成像模型
1.1 针孔模型
针孔模型是最基本的摄像机成像模型,描述了 3D 空间中的点如何通过摄像机投影到 2D 图像平面上。
数学表示:
其中:
-
(X,Y,Z) 是 3D 空间中点的坐标。
-
(x,y) 是 2D 图像平面上的点坐标。
-
f 是焦距。
-
(x0,y0) 是主点坐标。
齐次坐标表示:
其中 w=Z,最终的 2D 坐标为 (x/w,y/w)。
1.2 薄透镜模型
薄透镜模型考虑了镜头的实际物理特性,适用于实际摄像机的成像过程。
数学表示:
其中:
-
f 是焦距。
-
u 是物距。
-
v 是像距。
2. 2D 到 3D 的变换
2.1 透视投影
透视投影是将 3D 空间中的点投影到 2D 平面上的过程,模拟人眼观察的透视效果。
数学表示:
其中:
-
K 是内参矩阵。
-
R 是旋转矩阵。
-
t 是平移向量。
内参矩阵:
外参矩阵:
2.2 正交投影
正交投影是一种平行投影,不考虑透视效果,适用于某些特定的 3D 场景。
数学表示:
3. 单应性矩阵
单应性矩阵(Homography Matrix)用于描述平面场景的 2D 到 2D 变换,也可以用于 2D 到 3D 的变换。
数学表示:
其中 H 是 3×3 的单应性矩阵。
求解单应性矩阵: 给定四对对应点 (xi,xi′),可以构建一个线性方程组,求解 H 的元素。
代码示例(OpenCV):
#include <opencv2/opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main() {
vector<Point2f> srcPoints = { {0, 0}, {100, 0}, {100, 100}, {0, 100} };
vector<Point2f> dstPoints = { {10, 10}, {110, 10}, {110, 110}, {10, 110} };
Mat H = findHomography(srcPoints, dstPoints);
cout << "Homography Matrix:" << endl << H << endl;
return 0;
4. PnP 问题
PnP(Perspective-n-Point)问题是求解摄像机位姿的经典问题,给定 2D 图像中的特征点和它们在 3D 世界中的对应点,估计摄像机的旋转矩阵和平移向量。
数学表示:
其中:
-
xi 是 2D 图像中的点。
-
Xi 是 3D 世界中的点。
-
K 是内参矩阵。
-
R 和 t 是外参。
求解方法:
-
EPnP:Efficient PnP 算法,适用于实时应用。
-
DLS:Direct Least Squares 算法。
-
基于 RANSAC 的 PnP:用于处理含有外点的数据。
代码示例(OpenCV):
#include <opencv2/opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main() {
vector<Point3f> objectPoints = { {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0} };
vector<Point2f> imagePoints = { {10, 10}, {110, 10}, {110, 110}, {10, 110} };
Mat cameraMatrix = (Mat_<double>(3, 3) << 500, 0, 320, 0, 500, 240, 0, 0, 1);
Mat distCoeffs = Mat::zeros(5, 1, CV_64F);
Mat rvec, tvec;
solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs, rvec, tvec);
Mat R;
Rodrigues(rvec, R);
cout << "Rotation Matrix:" << endl << R << endl;
cout << "Translation Vector:" << endl << tvec << endl;
return 0;
}