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)
相关推荐
fie88892 天前
基于MATLAB的LBFGS优化算法实现
算法·matlab
wuk9982 天前
基于有限差分法的二维平面热传导模型MATLAB实现
开发语言·matlab·平面
csdn_aspnet3 天前
分享MATLAB在数据分析与科学计算中的高效算法案例
算法·matlab·数据分析
弈风千秋万古愁3 天前
【PID】连续PID和数字PID chapter1(补充) 学习笔记
笔记·学习·算法·matlab
成长痕迹3 天前
【Python与Matlab数据分析对比】
python·matlab·数据分析
机器学习之心4 天前
MATLAB基于IOWA算子的投影法加权几何平均组合预测模型
matlab·iowa算子·加权几何平均组合预测模型
秋风战士4 天前
通信算法之336 :3GPPMixed Mode Turbo Decoder
算法·matlab·fpga开发·信息与通信·基带工程
fengfuyao9854 天前
基于小波变换的图像阈值去噪MATLAB实现
matlab
foundbug9995 天前
基于最小二乘(LS)信道估计的MATLAB实现
matlab
柠檬07115 天前
MATLAB相机标定入门:Camera Calibration工具包详解
开发语言·数码相机·matlab