You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过OpenCV相机校准获取4参数畸变矩阵?

OpenCV中实现4参数畸变模型的方法
  • OpenCV没有专门的4参数畸变模型标志位,但完全可以手动构造或通过标定约束实现4参数畸变矩阵。

  • OpenCV默认的5参数畸变向量结构为[k1, k2, p1, p2, k3],4参数模型本质是移除径向畸变的第三个参数k3,仅保留前四个参数[k1, k2, p1, p2]。

  • 具体实现方式:

    1. 直接构造4参数畸变矩阵:如果已有MATLAB得到的4参数畸变结果,提取对应的k1,k2,p1,p2值,构造长度为4的np.float32类型数组作为畸变系数,传入OpenCV的校正函数即可。OpenCV会自动识别参数数量,只应用前四个参数对应的畸变模型。
    2. 标定阶段约束生成4参数:若需要直接从标定得到4参数模型,在使用cv2.calibrateCamera()或cv2.stereoCalibrate()时,设置flags参数为cv2.CALIB_FIX_K3,强制将k3固定为0,标定输出的畸变系数即为4参数形式(可直接取前四个值使用)。
  • 代码示例:

import cv2
import numpy as np

# 从MATLAB获取的4参数畸变值
k1, k2, p1, p2 = 0.12, -0.34, 0.001, 0.002
dist_coeffs_4param = np.array([k1, k2, p1, p2], dtype=np.float32)

# 示例相机内参
camera_matrix = np.array([[1000, 0, 320],
                          [0, 1000, 240],
                          [0, 0, 1]], dtype=np.float32)

# 单目图像校正
img = cv2.imread('left_img.jpg')
undistorted_img = cv2.undistort(img, camera_matrix, dist_coeffs_4param)

# 立体校正示例(假设已获取右相机参数)
R, T = np.eye(3), np.array([0.1, 0, 0], dtype=np.float32)
camera_matrix_right = camera_matrix.copy()
dist_coeffs_4param_right = dist_coeffs_4param.copy()

R1, R2, P1, P2, Q, _, _ = cv2.stereoRectify(
    camera_matrix, dist_coeffs_4param,
    camera_matrix_right, dist_coeffs_4param_right,
    img.shape[:2], R, T
)

内容的提问来源于stack exchange,提问作者Arya Zhang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 15:35:59