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

如何在OpenCV Python中导入/导出相机内参及相关参数?

OpenCV Python 相机参数导出/导入方案

核心结论

OpenCV 没有提供一键导出所有标定相关信息的内置函数,但可以通过cv.FileStorage类手动实现XML/YAML格式的参数读写,完全支持存储相机内参、分辨率、像素尺寸等额外信息,且生成的文件能兼容Agisoft等支持OpenCV格式的软件。

导出参数实现

使用cv.FileStorage写入XML/YAML文件,你可以自定义存储所有需要的参数:

import cv2 as cv
import numpy as np

# 假设已完成标定,得到以下参数(替换为你的实际标定结果)
camera_matrix = np.array([[1200, 0, 960], [0, 1200, 540], [0, 0, 1]], dtype=np.float32)
dist_coeffs = np.array([-0.3, 0.1, 0, 0, 0], dtype=np.float32)
image_size = (1920, 1080)  # 图像分辨率(宽,高)
pixel_size = (3.45e-6, 3.45e-6)  # 像素物理尺寸(宽,高,单位:米)

# 写入XML文件
with cv.FileStorage('camera_calib_params.xml', cv.FILE_STORAGE_WRITE) as fs:
    fs.write('camera_matrix', camera_matrix)
    fs.write('dist_coeffs', dist_coeffs)
    fs.write('image_width', image_size[0])
    fs.write('image_height', image_size[1])
    fs.write('pixel_width', pixel_size[0])
    fs.write('pixel_height', pixel_size[1])

导入参数实现

同样用cv.FileStorage读取文件,解析出存储的所有参数:

import cv2 as cv
import numpy as np

# 从XML文件读取参数
with cv.FileStorage('camera_calib_params.xml', cv.FILE_STORAGE_READ) as fs:
    camera_matrix = fs.getNode('camera_matrix').mat()
    dist_coeffs = fs.getNode('dist_coeffs').mat()
    image_width = int(fs.getNode('image_width').real())
    image_height = int(fs.getNode('image_height').real())
    pixel_width = fs.getNode('pixel_width').real()
    pixel_height = fs.getNode('pixel_height').real()
    
    # 重组为常用格式
    image_size = (image_width, image_height)
    pixel_size = (pixel_width, pixel_height)

关于Agisoft兼容性

用上述方法生成的XML文件完全符合OpenCV的标准格式,只要参数命名遵循常规(比如camera_matrix、dist_coeffs),就能直接被Agisoft识别读取,无需额外格式转换。

为什么说“需要手动编写”

论坛提到的“手动编写”是指OpenCV没有封装好的一键保存函数(比如save_all_camera_params()),你需要自己调用FileStorage的接口来组织和写入参数,但这个过程非常简单,且能完全自定义存储的内容,灵活性很高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 03:01:22