undistortpoints
时间: 2025-07-05 22:03:56 浏览: 8
### 关于 `undistortPoints` 函数的使用方法
OpenCV 提供了丰富的图像处理功能,其中包括校正由镜头畸变引起的失真。`undistortPoints` 是用于纠正这些畸变影响的关键函数之一。
#### 函数原型
该函数可以按照如下方式调用:
```cpp
void cv::undistortPoints(
InputArray src,
OutputArray dst,
InputArray cameraMatrix,
InputArray distCoeffs,
InputArray R=noArray(),
InputArray P=noArray()
);
```
参数说明:
- `src`: 输入点集(通常是带有畸变的坐标),格式为 Nx2 或者 1xN 的二维浮点数数组。
- `dst`: 输出经过矫正后的点集,具有相同的尺寸和类型作为输入。
- `cameraMatrix`: 相机内参矩阵 \([fx\;0\;cx;\;0\;fy\;cy;\;0\;0\;1]\),其中 \(f\) 表示焦距而 \(c\) 则为中心偏移量。
- `distCoeffs`: 畸变系数向量 (k_1,k_2,p_1,p_2[,k_3[,k_4,k_5,k_6],[s_1,s_2,s_3,s_4]]) 如果提供的是零长度矢量,则假定无任何畸变存在。
- `R`: 可选旋转矩阵,默认情况下不应用额外变换。
- `P`: 新投影矩阵,默认为空则采用原始相机矩阵乘以单位阵[^3]。
#### 使用实例
下面给出一段简单的代码片段来展示如何利用上述接口去除图片中的径向畸变效果:
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat image = imread("image.jpg");
// 假设已知相机内部参数以及畸变系数
Mat camera_matrix = (Mat_<double>(3, 3) << fx, 0, cx, 0, fy, cy, 0, 0, 1);
Vec<double, 5> distortion_coeffs{ k1, k2, p1, p2, k3 };
vector<Point2f> distorted_points;
findChessboardCorners(image, Size(9, 6), distorted_points);
vector<Point2f> undistorted_points;
undistortPoints(distorted_points, undistorted_points, camera_matrix, distortion_coeffs);
// 绘制结果...
}
```
这段程序首先加载了一张棋盘格图案的照片,并通过 `findChessboardCorners()` 方法获取到了角点位置;接着调用了 `undistortPoints()` 来计算并返回去除了畸变效应的新坐标集合。
阅读全文
相关推荐




















