Matlab R2022b使用Camera Calibrator工具箱张正友标定法进行相机标定附带标定前后对比代码

打开Camera Calibrator

在这添加你拍摄的图片


根据你每个方块的实际边长填写,我是15mm。

通俗一点,要k3就选3 Coefficients,否则为0;要p1、p2就选Tangential Distortion。然后进行计算。

可以点击右侧误差高的选中图像进行移除来提高精度。

移除后会自动重新计算,然后导出参数。

导出后在工作区查看变量

K就是IntrinsicMatrix

TangentialDistortion分别是p1,p2

RadialDistortion分别是k1,k2,k3(选用2 Coefficients为0不显示)

标定后效果如下:

标定前后对比代码:

python 复制代码
import cv2  
import numpy as np  
#IntrinsicMatrix
fx,cx,fy,cy=326.398894622712,284.954173978833,328.070507959748,218.798886581291
#TangentialDistortion
p1,p2=-0.000517178855500132,-0.00236634006321519
#RadialDistortion
k1,k2,k3=0.229371939302846,-0.194046239909587,0

def undistort_video(camera_matrix, dist_coeffs, video_source=0):  
    # 打开视频流  
    cap = cv2.VideoCapture(video_source)  
  
    # 读取第一帧以获取其尺寸  
    ret, frame = cap.read()  
    if not ret:  
        print("无法打开视频流或文件")  
        return  
  
    h, w = frame.shape[:2]  
  
    # 创建一个窗口用于显示校正前的视频  
    cv2.namedWindow('Original Video', cv2.WINDOW_AUTOSIZE)  
  
    # 创建一个窗口用于显示校正后的视频  
    cv2.namedWindow('Undistorted Video', cv2.WINDOW_AUTOSIZE)  
  
    while True:  
        # 读取视频帧  
        ret, frame = cap.read()  
        if not ret:  
            break  
  
        # 校正图像  
        undistorted_frame = cv2.undistort(frame, camera_matrix, dist_coeffs, None, camera_matrix)  
  
        # 显示校正前和校正后的视频  
        cv2.imshow('Original Video', frame)  
        cv2.imshow('Undistorted Video', undistorted_frame)  
  
        # 按'q'键退出  
        if cv2.waitKey(1) & 0xFF == ord('q'):  
            break  
  
    # 释放资源和关闭窗口  
    cap.release()  
    cv2.destroyAllWindows()  
  
# 假设你已经有了这些参数,这里只是示例值  
camera_matrix = np.array([[fx, 0, cx],  
                          [0, fy, cy],  
                          [0, 0,  1]], dtype=np.float32)  
dist_coeffs = np.array([k1, k2, p1, p2, k3], dtype=np.float32)  
  
# 注意:将 fx, fy, cx, cy, k1, k2, p1, p2, k3 替换为你的实际标定值  
  
# 调用函数  
undistort_video(camera_matrix, dist_coeffs)
相关推荐
东荷新绿9 小时前
MATLAB 2018a 安装教程:30分钟搞定安装
开发语言·matlab·matlab2018a
jllllyuz1 天前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
Dev7z2 天前
基于Matlab传统图像处理的风景图像多风格转换与优化
图像处理·matlab·风景
t198751282 天前
基于MATLAB的指纹识别系统完整实现
开发语言·matlab
gihigo19982 天前
基于MATLAB的IEEE 14节点系统牛顿-拉夫逊潮流算法实现
开发语言·算法·matlab
云纳星辰怀自在2 天前
MATLAB: m脚本-fixdt数据类型数据范围
matlab·m脚本·fixdt
一叶知秋h2 天前
matlab实现PID参数功能的简单仿真_gif
matlab·gif·pid
技术净胜3 天前
MATLAB 基因表达数据处理与可视化全流程案例
开发语言·matlab
机器学习之心3 天前
SSA-SVMD麻雀算法优化逐次变分模态分解(15种不同的适应度)MATLAB代码
matlab·ssa-svmd·麻雀算法优化逐次变分模态分解
三维空间3 天前
在MATLAB中对基因表达数据进行数据预处理
matlab