双目摄像头Matlab参数标定

双目摄像头Matlab参数标定

https://blog.csdn.net/qq_40700822/article/details/124251201?spm=1001.2014.3001.5501

一、前期准备

1、安装好python3,可以在anaconda中安装python3。
2、一个合适的双目摄像头。
3、一台可以运行Matlab的电脑。
4、一张棋盘图(可A4打印,若效果不佳,则可A3打印)。
棋盘图如下图所示:需要测量小方框的边长(一般单位为毫米:mm)。

二、使用双目摄像头(左+右)拍摄棋盘图

1、注意事项
注意:
1、左、右摄像头图像中必须包含单独的完整的棋盘图。
2、可适当前后、左右、上下翻转棋盘图,在符合上述条件1的情况下。
3、拍摄左右双目的照片40~50张比较合适。

如图所示:

2、双目拍照代码(python)
take_photo.py内容如下:

import cv2
import sys
#引入库

cap = cv2.VideoCapture(1) #读取笔记本内置摄像头或者0号摄像头

i = 0
while True:
    ret, frame = cap.read()
    if (ret):
    cv2.namedWindow("Video01",0)   #创建一个名为Video01的窗口,0表示窗口大小可调
    #cv2.resizeWindow("Video01",1280,720) ##创建一个名为Video01的窗口,设置窗口大小为 1920 * 1080 与上一个设置的 0 有冲突
    cv2.imshow("Video01", frame)

    #等待按键按下
    c = cv2.waitKey(1) & 0xff

    #r若按下w则保存一张照片
    if c ==ord("w"):
        cv2.imwrite("./val_001/%d.bmp" %i, frame) #自己设置拍摄的照片的存储位置
        print("Save images %d succeed!" %i)
        i+=1

    #若按下Q键,则退出循环
    if c == ord("q"):
        break

#随时准备按q退出
cap.release()
#关掉所有窗口
cv2.destroyAllWindows()

注意:
1、运行take_photo.py前,注意设置左右照片的存储位置。
2、运行take_photo.py后,按下键盘上的“W”键拍摄一张照片。当拍摄的照片数量足够时,按下“Q”键退出程序运行。
3、程序退出后,打开存储照片的文件夹查看照片是否合适。

3、双目左右照片分割(python)
resize.py内容如下:

import numpy as np
import cv2

#img1 = cv2.imread(r"/Users/inbc/Desktop/zuo/Left1.bmp")
#img2 = cv2.imread(r"/Users/inbc/Desktop/you/Right1.bmp")
for i in range(0,7) :
    #imgT = cv2.imdecode(np.fromfile('./images/%d.bmp'  %i ,dtype=np.uint8), -1)
    imgT = cv2.imdecode(np.fromfile('./val/%d.bmp'  %i ,dtype=np.uint8), -1) #读取拍摄的左右双目照片
    # cv2.imshow("zuo", img1[300:1200, 500:2000])
# cv2.imshow("you", img2[300:1200, 500:2000])

# cv2.waitKey(0)

#设置左右照片的存储位置
cv2.imwrite("./val/zuo/reLeft%d.bmp"  %i  ,imgT[0:720, 0:1280] )#imgL的第一个参数是图片高度像素范围,第二个参数是图片宽度的像素范围
cv2.imwrite("./val/you/reRight%d.bmp" %i ,imgT[0:720, 1280:2560] )
print("Resize images%d Fnished!" %i)

print(“Fnished All!!!”)

注意:
1、运行resize.py前,注意设置左、右照片的分别的存储位置。
2、运行resize.py后,终端打印”Fnished All!!!”表示分割完成。
3、程序退出后,打开存储照片的文件夹查看照片,是否分割完成,左摄像头照片存放在zuo,右摄像头照片存放在you。(文件名可自己更改)

三、Matlab双目参数标定

1、打开Matlab后,点开app,找到Stereo Camera Calibrator。如下图所示:打开后如图所示:
2、导入双目的左右照片到Stereo Camera CalibratorAPP。具体操作,如下图所示:

image-20240530130249369

3、点击确认后就可以分析导入的左右的照片了。这个过程会自动剔除掉不合格(误差过大)的左右照片。

image-20240530131118874

4、导入照片后就可以进行双目定标了。
导入左右照片后,如图所示:

5、设置双目相机的定标参数,如图所示。

Radial Distortion: 3 Coefifcients

Compute: Skew

​ Trangential Distortion

image-20240530130432566

image-20240530132724870

6、进行双目定标,并导出双目参数矩阵到Matlab中,进行下一步的处理。

四、双目参数提取

1、左、右相机内参数获取,注意参数矩阵的转置:

>> stereoParams.CameraParameters1.IntrinsicMatrix'#左相机参数

ans =

  831.0889   -4.0855  659.4243
         0  831.8663  487.3259
         0         0    1.0000

>> stereoParams.CameraParameters2.IntrinsicMatrix'#右相机参数

ans =

  831.1982   -3.5773  632.5308
         0  832.1221  479.3084
         0         0    1.0000

Matlab如图所示:

2、获取左右相机畸变系数。

注意: 左右相机畸变系数:[k1, k2, p1, p2, k3] ,顺序要正确。

 # 左右相机畸变系数:[k1, k2, p1, p2, k3]

>> stereoParams.CameraParameters1.RadialDistortion

ans =

   -0.0806    0.3806   -0.5229 #k1  k2  k3

>> stereoParams.CameraParameters1.TangentialDistortion

ans =

   -0.0033    0.0005    #p1     p2

Matlab如图所示:

3、获取双目的旋转矩阵和平移矩阵,注意旋转矩阵的转置。

 # 旋转矩阵
>> stereoParams.RotationOfCamera2'

ans =

    1.0000    0.0017   -0.0093
   -0.0018    1.0000   -0.0019
    0.0093    0.0019    1.0000

 # 平移矩阵
>> stereoParams.TranslationOfCamera2

ans =

 -119.9578    0.1121   -0.2134

Matlab如图所示:

4、获取基线距离,单位:mm, 为平移向量的第一个参数(取绝对值)。

“`python
self.baseline = 119.9578 # 单位:mm, 为平移向量的第一个参数(取绝对值)
“`

至此,双目摄像头的参数就定标完了。

耐心一点,慢慢来总会成功的!!!
————————————————

                        版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/qq_40700822/article/details/124251201

我的双目输出标定:

            Standard Errors of Estimated Stereo Camera Parameters
            -----------------------------------------------------

Camera 1 Intrinsics
-------------------
Focal length (pixels):   [ 1557.0360 +/- 3.5529     1554.6651 +/- 3.6194  ]
Principal point (pixels):[  934.5229 +/- 2.4870      725.9970 +/- 1.8253  ]
Skew:                    [   -0.5642 +/- 0.2435  ]
Radial distortion:       [   -0.0521 +/- 0.0040        0.1695 +/- 0.0295       -0.1332 +/- 0.0685  ]
Tangential distortion:   [    0.0009 +/- 0.0003       -0.0025 +/- 0.0004  ]

Camera 1 Extrinsics
-------------------
Rotation vectors:
                         [    0.1404 +/- 0.0014       -0.2401 +/- 0.0017       -0.0245 +/- 0.0003  ]
                         [    0.0902 +/- 0.0015       -0.1148 +/- 0.0015        0.0815 +/- 0.0002  ]
                         [    0.1252 +/- 0.0016        0.1732 +/- 0.0018        0.0860 +/- 0.0002  ]
                         [    0.1272 +/- 0.0014       -0.3188 +/- 0.0016       -0.0038 +/- 0.0003  ]
                         [   -0.1238 +/- 0.0014        0.0813 +/- 0.0016        0.1334 +/- 0.0002  ]
                         [   -0.0096 +/- 0.0016       -0.1345 +/- 0.0018       -0.1244 +/- 0.0003  ]
                         [    0.0091 +/- 0.0016       -0.0719 +/- 0.0017       -0.0394 +/- 0.0003  ]
                         [   -0.0103 +/- 0.0015       -0.3273 +/- 0.0017        0.0368 +/- 0.0003  ]
                         [   -0.0094 +/- 0.0016       -0.0829 +/- 0.0016        0.0072 +/- 0.0003  ]
                         [   -0.4454 +/- 0.0014       -0.4731 +/- 0.0016       -0.0582 +/- 0.0004  ]
                         [   -0.3218 +/- 0.0019       -0.3877 +/- 0.0017       -0.0660 +/- 0.0005  ]
                         [    0.1001 +/- 0.0013       -0.2604 +/- 0.0017       -0.2103 +/- 0.0003  ]
                         [   -0.0395 +/- 0.0029       -0.1103 +/- 0.0022       -0.0281 +/- 0.0003  ]
                         [    0.2228 +/- 0.0017       -0.1775 +/- 0.0020       -1.6743 +/- 0.0003  ]
                         [   -0.2014 +/- 0.0013       -0.6541 +/- 0.0015        0.0063 +/- 0.0005  ]

Translation vectors (mm):
                         [   -1.2974 +/- 0.7708     -141.0844 +/- 0.5631      475.4112 +/- 1.1521  ]
                         [  -31.0645 +/- 0.6866      -82.1405 +/- 0.5028      425.7039 +/- 1.0093  ]
                         [   71.8146 +/- 0.7632     -162.2182 +/- 0.5620      470.0988 +/- 1.1743  ]
                         [  -57.8447 +/- 0.7099     -154.0450 +/- 0.5128      430.4074 +/- 1.0750  ]
                         [   36.6614 +/- 0.7358      -57.2701 +/- 0.5417      461.4980 +/- 1.0773  ]
                         [   28.1456 +/- 0.8723       45.5442 +/- 0.6426      544.3818 +/- 1.2791  ]
                         [   15.7305 +/- 0.7876      105.5578 +/- 0.5784      485.7754 +/- 1.1772  ]
                         [  -56.7221 +/- 0.8205        5.1884 +/- 0.6023      511.1243 +/- 1.2029  ]
                         [  -24.3024 +/- 0.7441      107.8938 +/- 0.5449      457.0560 +/- 1.1144  ]
                         [ -135.9453 +/- 1.1810       48.2589 +/- 0.8615      728.4183 +/- 1.6883  ]
                         [  -87.2899 +/- 1.0914      196.4729 +/- 0.7916      657.7164 +/- 1.6548  ]
                         [  -21.4200 +/- 0.7104     -119.9380 +/- 0.5180      438.2329 +/- 1.0558  ]
                         [ -139.4850 +/- 1.1350        7.5592 +/- 0.8295      702.7607 +/- 1.6362  ]
                         [   41.0943 +/- 0.9767      -76.5129 +/- 0.7219      611.1792 +/- 1.4357  ]
                         [ -111.3761 +/- 0.9381       21.4527 +/- 0.6818      576.3677 +/- 1.3849  ]

Camera 2 Intrinsics
-------------------
Focal length (pixels):   [ 1558.3205 +/- 3.7873     1556.2857 +/- 3.7325  ]
Principal point (pixels):[  886.4826 +/- 2.0573      711.1150 +/- 1.8086  ]
Skew:                    [   -0.1721 +/- 0.2234  ]
Radial distortion:       [   -0.0387 +/- 0.0034        0.1205 +/- 0.0222       -0.0840 +/- 0.0455  ]
Tangential distortion:   [   -0.0001 +/- 0.0003       -0.0020 +/- 0.0004  ]

Position And Orientation of Camera 2 Relative to Camera 1
---------------------------------------------------------
Rotation of camera 2:         [    0.9999 +/- 0.0009        0.0005 +/- 0.0009       -0.0125 +/- 0.0001  ]
Translation of camera 2 (mm): [ -120.3912 +/- 0.0487       -0.3378 +/- 0.0323       -0.6361 +/- 0.1601  ]

stereoParams = 

  stereoParameters with properties:

   Parameters of Two Cameras
        CameraParameters1: [1x1 cameraParameters]
        CameraParameters2: [1x1 cameraParameters]

   Inter-camera Geometry
        RotationOfCamera2: [3x3 double]
     TranslationOfCamera2: [-120.3912 -0.3378 -0.6361]
        FundamentalMatrix: [3x3 double]
          EssentialMatrix: [3x3 double]

   Accuracy of Estimation
    MeanReprojectionError: 0.2116

   Calibration Settings
              NumPatterns: 15
              WorldPoints: [35x2 double]
               WorldUnits: 'mm'


stereoParams = 

  stereoParameters with properties:

   Parameters of Two Cameras
        CameraParameters1: [1x1 cameraParameters]
        CameraParameters2: [1x1 cameraParameters]

   Inter-camera Geometry
        RotationOfCamera2: [3x3 double]
     TranslationOfCamera2: [-120.3912 -0.3378 -0.6361]
        FundamentalMatrix: [3x3 double]
          EssentialMatrix: [3x3 double]

   Accuracy of Estimation
    MeanReprojectionError: 0.2116

   Calibration Settings
              NumPatterns: 15
              WorldPoints: [35x2 double]
               WorldUnits: 'mm'

image-20240530161357706

camera1:

image-20240530161732114

camera2:

image-20240530161811968

以下是我的实际测试参数数据:

通过在MATLAB Command Window 输入命令获取

1、左、右相机内参数获取,注意参数矩阵的转置:

左相机参数:

>> stereoParams.CameraParameters1.IntrinsicMatrix

ans =

   1.0e+03 *

    1.5570         0         0
   -0.0006    1.5547         0
    0.9345    0.7260    0.0010

右相机参数:

>> stereoParams.CameraParameters2.IntrinsicMatrix

ans =

   1.0e+03 *

    1.5583         0         0
   -0.0002    1.5563         0
    0.8865    0.7111    0.0010

2、获取左右相机畸变系数。

注意: 左右相机畸变系数:[k1, k2, p1, p2, k3] ,顺序要正确。

>> stereoParams.CameraParameters1.RadialDistortion

ans =

   -0.0521    0.1695   -0.1332    #k1  k2  k3

>> stereoParams.CameraParameters1.TangentialDistortion
ans =

    0.0009   -0.0025    #p1     p2


>> stereoParams.CameraParameters2.RadialDistortion
ans =

   -0.0387    0.1205   -0.0840  #k1  k2  k3

>> stereoParams.CameraParameters2.TangentialDistortion

ans =

   -0.0001   -0.0020  #p1     p2

3、获取双目的旋转矩阵和平移矩阵,注意旋转矩阵的转置。

>> stereoParams.RotationOfCamera2

ans =

    0.9999   -0.0006    0.0125
    0.0005    1.0000    0.0089
   -0.0125   -0.0089    0.9999

>> stereoParams.TranslationOfCamera2

ans =

 -120.3912   -0.3378   -0.6361

4、获取基线距离,单位:mm, 为平移向量的第一个参数(取绝对值)。

self.baseline=120.3912 

至此,双目摄像头的参数就定标完了。

双目测试中配置文件使用

import numpy as np


####################仅仅是一个示例,是我的参数 2024.05.30 ##############################

# 双目相机参数
class stereoCamera(object):
    def __init__(self):
        # 左相机内参
        '''
        >> stereoParams.CameraParameters1.IntrinsicMatrix
            ans =

               1.0e+03 *

                1.5570         0         0
               -0.0006    1.5547         0
                0.9345    0.7260    0.0010
        '''

        self.cam_matrix_left = np.array([[1557.0, -0.6, 934.5],
                                         [0, 1554.7, 726.0],
                                         [0, 0, 1]
                                         ])
        # 右相机内参
        '''
        >> stereoParams.CameraParameters2.IntrinsicMatrix
            ans =

               1.0e+03 *

                1.5583         0         0
               -0.0002    1.5563         0
                0.8865    0.7111    0.0010
        '''
        self.cam_matrix_right = np.array([[1558.3, -0.2, 886.5],
                                          [0, 1556.3, 711.1],
                                          [0, 0, 1]
                                          ])

        # 左右相机畸变系数:[k1, k2, p1, p2, k3]
        '''
        **注意: 左右相机畸变系数:[k1, k2, p1, p2, k3] ,顺序要正确。**
            >> stereoParams.CameraParameters1.RadialDistortion
            ans =
               -0.0521    0.1695   -0.1332    #k1  k2  k3
            >> stereoParams.CameraParameters1.TangentialDistortion
            ans =
                0.0009   -0.0025    #p1     p2
            >> stereoParams.CameraParameters2.RadialDistortion
            ans =
               -0.0387    0.1205   -0.0840  #k1  k2  k3
            >> stereoParams.CameraParameters2.TangentialDistortion
            ans =
               -0.0001   -0.0020  #p1     p2
        '''
        self.distortion_l = np.array([[-0.0521, 0.1695, 0.0009, -0.0025, -0.1332]])
        self.distortion_r = np.array([[-0.0387, 0.1205, -0.0001, -0.0020, -0.0840]])

        # 旋转矩阵
        '''
        #注意旋转矩阵的转置。
        >> stereoParams.RotationOfCamera2
            ans =
                0.9999   -0.0006    0.0125
                0.0005    1.0000    0.0089
               -0.0125   -0.0089    0.9999
        '''
        self.R = np.array([[0.9999, 0.0005, -0.0125],
                           [-0.0006, 1.0000, -0.0089],
                           [0.0125, 0.0089, 1.9999]
                           ])

        # 平移矩阵
        '''
        >> stereoParams.TranslationOfCamera2
            ans =
             -120.3912   -0.3378   -0.6361
        '''
        self.T = np.array([[-120.3912], [-0.3378], [-0.6361]])

        # 焦距
        '''
            D:\AI\CV2\SHUANGMU>python dianyun.py
            config.cam_matrix_left:
            [[ 1.5570e+03 -6.0000e-01  9.3450e+02]
             [ 0.0000e+00  1.5547e+03  7.2600e+02]
             [ 0.0000e+00  0.0000e+00  1.0000e+00]]
            Q:
            [[ 1.00000000e+00  0.00000000e+00  0.00000000e+00 -9.03789375e+02]
             [ 0.00000000e+00  1.00000000e+00  0.00000000e+00 -7.19628532e+02]
             [ 0.00000000e+00  0.00000000e+00  0.00000000e+00  1.61991245e+03]
             [ 0.00000000e+00  0.00000000e+00  8.30610631e-03 -0.00000000e+00]]
            1557.0 1557.0 934.5 726.0
            x: 31758.486 y: 43296.887 z: -195026.7
            distance: 202283.55539983188
        '''
        # self.focal_length = 859.367  # 默认值,一般取立体校正后的重投影矩阵Q中的 Q[2,3]
        self.focal_length = 1619.91245  # 默认值,一般取立体校正后的重投影矩阵Q中的 Q[2,3]

        # 基线距离
        self.baseline = 120.3912   # 单位:mm, 为平移向量的第一个参数(取绝对值)

发表评论