• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MKT-porter
博客园    首页    新随笔    联系   管理    订阅  订阅
matlab相机内参标定
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

1视频抽帧

1是否要缩放

2保存的文件夹名字不许有中文 ,自动创建文件夹

3间隔帧数

 

# -*- coding:utf8 -*-
import cv2
import os
import shutil
  
  
def get_frame_from_video(video_name, save_path,interval,resize):

    cv2.namedWindow('Read_Image', cv2.WINDOW_NORMAL)
    
    """
    Args:
        video_name:输入视频名字
        interval: 保存图片的帧率间隔
    Returns:
    """
  
    # 保存图片的路径
    #save_path = video_name.split('.MOV')[0] + '/'
    save_path=save_path+str(resize)+"/" # 保存在对应缩放倍数文件夹
    is_exists = os.path.exists(save_path)
    if not is_exists:
        os.makedirs(save_path)
        print('path of %s is build' % save_path)
    else:
        shutil.rmtree(save_path)
        os.makedirs(save_path)
        print('path of %s already exist and rebuild' % save_path)
  
    # 开始读视频
    video_capture = cv2.VideoCapture(video_name)
    i = 0
    j = 0

    frame_nums = video_capture.get(7)  # 获取视频总帧数
    print("视频的总帧数为:", int(frame_nums))
    frame_rete = video_capture.get(5)  # 获取视频帧率
    print("视频的帧率为:", int(frame_rete))

    while True:
        success, frame = video_capture.read()
        if not success:
            print('video is all read')
            break

        
        i += 1
        if i % interval == 0:
            if resize>1:
                width = int(frame.shape[1] / resize )
                height = int(frame.shape[0] / resize )
                dim = (width, height)
                frame=cv2.resize(frame,dim)
            cv2.imshow("Read_Image", frame)
            cv2.waitKey (1) 
            # 保存图片
            j += 1
            #save_name = save_path + str(j) + '_' + str(i) + '.jpg'
            save_name = save_path + str(j)  + '.jpg'
            cv2.imwrite(save_name, frame)
            print('保存图像' ,save_name)

  
  
if __name__ == '__main__':
    # 视频文件名字
    video_name = '视频畸变.MOV' #视频非畸变
    save_path="video_distortion/" #  video_undistortion 保存路径不能有中文
    interval = 20 #间隔帧数
    resize=2 # 是否需要图像改变分辨率 1/resize
    get_frame_from_video(video_name, save_path,interval,resize)
    cv2.destroyAllWindows()

  

2 标定

2-1打开相机标定工具

 选择标定板的参数

 

2-2 选择照片

 2-3设置参数

非畸变的话

 复杂一点的

 

Radial Distortion 径向畸变
其中:
2 Coefficients选项的官方含义:
Use fourth degree polynomial to estimate the radial distortion of a lens.
使用四次多项式来估计透镜的径向畸变。
3 Coefficients选项的官方含义:
This setting is recommended only for wide field of view camera.
Use sixth degree polynomial to estimate the radial distortion of a lens.
此设置仅适用于大视场相机。
使用六次多项式来估计透镜的径向畸变。

综上所述:
对于一般的相机选择2 Coefficients选项即可,对于大视场相机则选择3 Coefficients选项。

  

2.1关于Skew
Assume that X-axis and Y-axis are not perpendicular. This means that the image pixels are not rectangular. Most modern cameras do not exhibit this issue.
假设X轴和Y轴不垂直。这意味着图像像素不是矩形的。大多数现代相机都没有这个问题。
所以,Skew选项一般不需要选择。

2.2关于Tangential Distortion
Tangential distortion occurs when the lens’ principal axis is not perpendicular to the camera sensor.
当镜头的主轴与相机传感器不垂直时,会发生切向失真。

  

 

2-4开始标定

 2-5 最终平均误差要小于0.5

 删除误差大的

 进一步删除

 

3导出数据

 

 

ImageSize:图像大小
Radial Distortion:径向畸变
Tangential Distortion:切向畸变
World Points:世界坐标系下的点
World Units:世界坐标下的单位
Estimate Skew:估计倾斜
Num Radial Distortion Coefficient:径向畸变系数个数
Estimate Tangential Distortion:估计切向畸变
Translation Vectors:平移向量
Reprojection Errors: 重投影误差
Detected Keypoints:检测到的关键点
Rotation Vectors:旋转向量
Num Patterns:模态数
Intrinsics:内参
Intrinsic Matrix:内参矩阵
Focal Length:焦距
Principal Point:主点偏移
Skew:偏斜
Mean Reprojection Error:平均重投影误差
Reprojected Points:重投影点
Rotation Matrices:旋转矩阵

  

制作单目相机yaml文件

%YAML:1.0

#--------------------------------------------------------------------------------------------
# Camera Parameters. Adjust them!
#--------------------------------------------------------------------------------------------
Camera.type: "PinHole"

# Camera calibration and distortion parameters (OpenCV) 
Camera.fx: 1715.3730
Camera.fy: 1702.5771
Camera.cx: 1129.8076
Camera.cy: 642.5709

Camera.k1: 0.02724
Camera.k2: -0.097636
Camera.k3: 0.10436
Camera.p1: 0.0000127185
Camera.p2: -0.001243

# Camera frames per second 
Camera.fps: 30.0

# Color order of the images (0: BGR, 1: RGB. It is ignored if images are grayscale)
Camera.RGB: 0

# Camera resolution
Camera.width: 640
Camera.height: 480

#--------------------------------------------------------------------------------------------
# ORB Parameters
#--------------------------------------------------------------------------------------------

# ORB Extractor: Number of features per image
ORBextractor.nFeatures: 1000

# ORB Extractor: Scale factor between levels in the scale pyramid 	
ORBextractor.scaleFactor: 1.2

# ORB Extractor: Number of levels in the scale pyramid	
ORBextractor.nLevels: 8

# ORB Extractor: Fast threshold
# Image is divided in a grid. At each cell FAST are extracted imposing a minimum response.
# Firstly we impose iniThFAST. If no corners are detected we impose a lower value minThFAST
# You can lower these values if your images have low contrast			
ORBextractor.iniThFAST: 20
ORBextractor.minThFAST: 7

#--------------------------------------------------------------------------------------------
# Viewer Parameters
#--------------------------------------------------------------------------------------------
Viewer.KeyFrameSize: 0.05
Viewer.KeyFrameLineWidth: 5
Viewer.GraphLineWidth: 0.9
Viewer.PointSize: 2
Viewer.CameraSize: 0.08
Viewer.CameraLineWidth: 3
Viewer.ViewpointX: 0
Viewer.ViewpointY: -0.7
Viewer.ViewpointZ: -1.8
Viewer.ViewpointF: 500

  

 

1 -1 视频无畸变 缩放1 光圈5.6

 

分辨率

1080 1920

c_dis2.RadialDistortion 径向

0.00976668633301165 -0.0256100675922523

c_dis2.TangentialDistortion 切向

0 0

c_dis2.IntrinsicMatrix 内参矩阵

2662.11557532935 0 0
0 2662.50587647848 0
1929.70732638328 1082.81002724506 1

焦距

2662.11557532935 2662.50587647848

中心

1929.70732638328 1082.81002724506

1-2 视频无畸变 缩放2 光圈5.6  - 2个参数矫正

 

 

分辨率

1080 1920

c_dis2.RadialDistortion 径向

0.00860533403973605 -0.0280705816129454

c_dis2.TangentialDistortion 切向

0 0

c_dis2.IntrinsicMatrix 内参矩阵

1329.72479114946 0 0
0 1329.65375225699 0
961.340052491983 542.650028093206 1

焦距

1329.72479114946 1329.65375225699

中心

961.340052491983 542.650028093206

 

 

视频无畸变 缩放2 光圈2.8

 

分辨率

1080 1920

c_dis2.RadialDistortion 径向

0.00794509098829319 -0.0206369212331861

c_dis2.TangentialDistortion 切向

0 0

c_dis2.IntrinsicMatrix 内参矩阵

1344.20596718591 0 0
0 1344.59802793993 0
962.172523533900 539.549826481578 1

焦距

1344.20596718591 1344.59802793993

中心

962.172523533900 539.549826481578

 

视频无畸变 缩放2 光圈8

分辨率

1080 1920

c_dis2.RadialDistortion 径向

0.0117700365744874 -0.0298473030704834

c_dis2.TangentialDistortion 切向

-0.000461855530517975 -0.000981266673604629

c_dis2.IntrinsicMatrix 内参矩阵

1332.58133444975 0 0
0.638870210231364 1333.53093918829 0
960.214030443123 543.851518742835 1

焦距

1332.58133444975 1333.53093918829

中心

960.214030443123 543.851518742835

 

posted on 2024-07-03 15:28  MKT-porter  阅读(949)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3