OpenCV相机标定与3D重建(36)计算两幅图像之间基本矩阵(Fundamental Matrix)的函数findFundamentalMat()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

从两幅图像中的对应点计算基本矩阵。

cv::findFundamentalMat 是 OpenCV 中用于计算两幅图像之间基本矩阵(Fundamental Matrix)的函数。基本矩阵描述了两个未校准摄像机之间的几何关系,它在计算机视觉中用于立体视觉、运动结构恢复(Structure from Motion, SfM)、视觉里程计等任务。

函数原型

cpp 复制代码
Mat cv::findFundamentalMat
(
	InputArray 	points1,
	InputArray 	points2,
	int 	method,
	double 	ransacReprojThreshold,
	double 	confidence,
	int 	maxIters,
	OutputArray 	mask = noArray() 
)		

参数

  • 参数points1:来自第一幅图像的 N 个点数组。点的坐标应该是浮点数(单精度或双精度)。
  • 参数points2:第二幅图像的点数组,与 points1 具有相同的大小和格式。
  • 参数method:计算基本矩阵的方法。
    • FM_7POINT:用于7点算法。N=7
    • FM_8POINT:用于8点算法。N≥8
    • FM_RANSAC:用于RANSAC算法。N≥8
    • FM_LMEDS:用于最小中值法(LMedS)算法。N≥8
  • 参数ransacReprojThreshold:仅用于 RANSAC 的参数。它是点到极线的最大距离(以像素为单位),超过该距离的点被认为是离群点,并不用于计算最终的基本矩阵。根据点定位的准确性、图像分辨率和图像噪声,它可以设置为1-3等。
  • 参数confidence:仅用于 RANSAC 和 LMedS 方法的参数。它指定了估计矩阵正确的期望置信水平(概率)。
  • 参数[out] mask:可选输出掩码。
  • 参数maxIters:稳健方法的最大迭代次数。

说明

极几何由以下方程描述:

p 2 ; 1 \] T F \[ p 1 ; 1 \] = 0 \[p_2; 1\]\^T F \[p_1; 1\] = 0 \[p2;1\]TF\[p1;1\]=0 其中 F 是基本矩阵,p1和p2分别是第一幅和第二幅图像中的对应点。 该函数使用上述列出的四种方法之一来计算基本矩阵,并返回找到的基本矩阵。通常只找到一个矩阵。但在7点算法的情况下,该函数可能返回多达3个解(一个 9×3 矩阵,按顺序存储所有3个矩阵)。 ```cpp // Example. Estimation of fundamental matrix using the RANSAC algorithm int point_count = 100; vector points1(point_count); vector points2(point_count); // initialize the points here ... for( int i = 0; i < point_count; i++ ) { points1[i] = ...; points2[i] = ...; } Mat fundamental_matrix = findFundamentalMat(points1, points2, FM_RANSAC, 3, 0.99); ``` ### 代码示例 ```cpp #include #include using namespace cv; using namespace std; int main( int argc, char** argv ) { // 创建虚拟的匹配点数据(假设我们有8对匹配点) vector< Point2f > points1 = { Point2f( 154.0f, 38.0f ), Point2f( 285.0f, 176.0f ), Point2f( 279.0f, 238.0f ), Point2f( 276.0f, 284.0f ), Point2f( 273.0f, 342.0f ), Point2f( 267.0f, 397.0f ), Point2f( 262.0f, 446.0f ), Point2f( 254.0f, 495.0f ) }; vector< Point2f > points2 = { Point2f( 149.0f, 49.0f ), Point2f( 280.0f, 187.0f ), Point2f( 274.0f, 249.0f ), Point2f( 271.0f, 295.0f ), Point2f( 268.0f, 353.0f ), Point2f( 262.0f, 408.0f ), Point2f( 257.0f, 457.0f ), Point2f( 249.0f, 506.0f ) }; // 定义输出的基本矩阵和掩码 Mat fundamentalMatrix, mask; // 使用 RANSAC 方法计算基本矩阵 fundamentalMatrix = findFundamentalMat( points1, points2, FM_RANSAC, // 使用RANSAC方法 1.0, // 点到极线的最大重投影误差 0.99, // 置信水平 2000, // 最大迭代次数 mask ); // 输出掩码 // 打印结果 cout << "Fundamental Matrix:\n" << fundamentalMatrix << endl; // 打印哪些点被认为是内点 cout << "Inliers mask:\n"; for ( size_t i = 0; i < mask.total(); ++i ) { if ( mask.at< uchar >( i ) ) { cout << "Point " << i + 1 << " is an inlier." << endl; } else { cout << "Point " << i + 1 << " is an outlier." << endl; } } return 0; } ``` ### 运行结果 ```bash Fundamental Matrix: [-3.247212965698772e-20, -0.0008949509319799827, 0.704568065615863; 0.0008949509319799836, 3.892534466973619e-19, 0.229349120734492; -0.7144125258676433, -0.2338238753943923, 1] Inliers mask: Point 1 is an inlier. Point 2 is an inlier. Point 3 is an inlier. Point 4 is an inlier. Point 5 is an inlier. Point 6 is an inlier. Point 7 is an inlier. Point 8 is an inlier. ```

相关推荐
audyxiao0018 小时前
为了更强大的空间智能,如何将2D图像转换成完整、具有真实尺度和外观的3D场景?
人工智能·计算机视觉·3d·iccv·空间智能
范男11 小时前
基于Pytochvideo训练自己的的视频分类模型
人工智能·pytorch·python·深度学习·计算机视觉·3d·视频
点云SLAM14 小时前
SLAM文献之-Globally Consistent and Tightly Coupled 3D LiDAR Inertial Mapping
3d·机器人·slam·vgicp算法·gpu 加速·lidar-imu 建图方法·全局匹配代价最小化
LetsonH17 小时前
⭐CVPR2025 给3D高斯穿 “UV 衣” 框架[特殊字符]
3d·uv
Enougme18 小时前
python-使用鼠标对图片进行涂抹&自定义绘图
python·opencv
lxmyzzs1 天前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv
新启航-光学3D测量1 天前
从 48 小时到 4 小时:三维逆向工程中自动化工具链如何重构扫描建模效率
科技·3d·制造
彩旗工作室1 天前
腾讯混元3D系列开源模型:从工业级到移动端的本地部署
3d·开源·腾讯混元
CG_MAGIC1 天前
主流 3D 模型格式(FBX/OBJ/DAE/GLTF)材质支持与转换操作指南
3d·渲染·动画·材质·贴图·3d 模型格式·材质支持与转换操作指南
山烛1 天前
OpenCV 图像处理基础操作指南(二)
人工智能·python·opencv·计算机视觉