OpenCV相机标定与3D重建(25)计算两个三维点集之间的最优仿射变换矩阵(3x4)函数estimateAffine3D()的使用

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

算法描述

计算两个3D点集之间的最优仿射变换。

它计算 [ x y z ] = [ a 11 a 12 a 13 a 21 a 22 a 23 a 31 a 32 a 33 ] [ X Y Z ] + [ b 1 b 2 b 3 ] \begin{bmatrix} x\\ y\\ z\\ \end{bmatrix} = \begin{bmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33}\\ \end{bmatrix} \begin{bmatrix} X\\ Y\\ Z\\ \end{bmatrix} + \begin{bmatrix} b_1\\ b_2\\ b_3\\ \end{bmatrix} xyz = a11a21a31a12a22a32a13a23a33 XYZ + b1b2b3

cv::estimateAffine3D 是 OpenCV 库中的一个函数,用于计算两个三维点集之间的最优仿射变换矩阵(3x4)。此函数通常用于3D图像配准、物体识别和追踪等领域。它通过最小化源点集与目标点集之间的几何误差来估计变换,并且可以选择使用鲁棒方法(如RANSAC)来处理异常值(outliers)。

函数原型

cpp 复制代码
int cv::estimateAffine3D
(
	InputArray 	src,
	InputArray 	dst,
	OutputArray 	out,
	OutputArray 	inliers,
	double 	ransacThreshold = 3,
	double 	confidence = 0.99 
)		

参数

src 第一个输入的3D点集,包含 (X,Y,Z) 坐标。

dst 第二个输入的3D点集,包含 (x,y,z) 坐标。

out 输出的3D仿射变换矩阵 3×4,形式如下:

a 11 a 12 a 13 b 1 a 21 a 22 a 23 b 2 a 31 a 32 a 33 b 3 \] \\begin{bmatrix} a_{11} \& a_{12} \& a_{13} \& b_1\\\\ a_{21} \& a_{22} \& a_{23} \& b_2\\\\ a_{31} \& a_{32} \& a_{33} \& b_3\\\\ \\end{bmatrix} a11a21a31a12a22a32a13a23a33b1b2b3 inliers 输出向量,指示哪些点是内点(1-内点,0-外点)。 ransacThreshold 在RANSAC算法中,考虑一个点为内点的最大重投影误差。 confidence 对估计变换的置信水平,在0和1之间。通常0.95到0.99之间的值就足够了。过于接近1的值可能会显著减慢估计过程。低于0.8-0.9的值可能导致变换估计不准确。 该函数使用RANSAC算法估计两个3D点集之间的最优3D仿射变换。 ### 代码示例 ```cpp #include #include #include using namespace cv; using namespace std; int main() { // 定义两组对应的3D点 (X, Y, Z) - 源点集和目标点集 vector< Point3f > src = { Point3f( 0, 0, 0 ), Point3f( 1, 0, 0 ), Point3f( 0, 1, 0 ), Point3f( 0, 0, 1 ) }; vector< Point3f > dst = { Point3f( 1, 1, 1 ), Point3f( 2, 1, 1 ), Point3f( 1, 2, 1 ), Point3f( 1, 1, 2 ) }; // 定义一个 Mat 来接收输出的仿射变换矩阵 Mat affineMatrix; // 定义一个 Mat 来接收内点信息 vector< uchar > inliers; // 调用 estimateAffine3D 函数 int inlierCount = estimateAffine3D( src, dst, affineMatrix, inliers ); if ( !affineMatrix.empty() ) { cout << "Estimated Affine Matrix:\n" << affineMatrix << endl; cout << "Number of inliers: " << inlierCount << endl; // 打印哪些点被认为是内点 for ( size_t i = 0; i < inliers.size(); ++i ) { if ( inliers[ i ] ) { cout << "Point pair (" << src[ i ] << ", " << dst[ i ] << ") is an inlier.\n"; } else { cout << "Point pair (" << src[ i ] << ", " << dst[ i ] << ") is an outlier.\n"; } } } else { cout << "Failed to estimate affine transformation." << endl; } return 0; } ``` ### 运行结果 ```bash Estimated Affine Matrix: [0.9999999999999998, 3.483324739761429e-15, -1.838806884535416e-15, 0.9999999999999998; -4.649058915617843e-16, 1.000000000000004, -1.595945597898663e-15, 1; -4.371503159461554e-16, 3.337607967779377e-15, 0.9999999999999983, 0.9999999999999994] Number of inliers: 1 Point pair ([0, 0, 0], [1, 1, 1]) is an inlier. Point pair ([1, 0, 0], [2, 1, 1]) is an inlier. Point pair ([0, 1, 0], [1, 2, 1]) is an inlier. Point pair ([0, 0, 1], [1, 1, 2]) is an inlier. ```

相关推荐
lxmyzzs5 分钟前
【图像算法 - 14】精准识别路面墙体裂缝:基于YOLO12与OpenCV的实例分割智能检测实战(附完整代码)
人工智能·opencv·算法·计算机视觉·裂缝检测·yolo12
二川bro4 小时前
第16节:自定义几何体 - 从顶点构建3D世界
3d
迈火16 小时前
ComfyUI-3D-Pack:3D创作的AI神器
人工智能·gpt·3d·ai·stable diffusion·aigc·midjourney
jndingxin1 天前
OpenCV图像注册模块
人工智能·opencv·计算机视觉
R-G-B1 天前
【P14 3-6 】OpenCV Python——视频加载、摄像头调用、视频基本信息获取(宽、高、帧率、总帧数)
python·opencv·视频加载·摄像头调用·获取视频基本信息·获取视频帧率·获取视频帧数
荼蘼1 天前
OpenCv(三)——图像平滑处理
人工智能·opencv·计算机视觉
R-G-B2 天前
OpenCV Python——报错AttributeError: module ‘cv2‘ has no attribute ‘bgsegm‘,解决办法
人工智能·python·opencv·opencv python·attributeerror·module ‘cv2‘·no attribute
似乎很简单2 天前
【opencv-Python学习笔记(5):几何变换】
笔记·opencv·学习
新启航光学频率梳2 天前
【新启航】起落架大型结构件深孔检测探究 - 激光频率梳 3D 轮廓检测
科技·3d·制造
荼蘼2 天前
OpenCv(二)——边界填充、阈值处理
人工智能·opencv·计算机视觉