pythonカメラ内パラメータ

13946 ワード

この兄貴のpython+opencvカメラの標定を写して、彼の10行目のobjp[:, :2] = np.mgrid[0:9, 0:6].T.reshape(-1, 2)、6と9は逆に書いたようです.
import cv2
import numpy as np
import glob

def cal(h, w, image_path_list):
    # h, w:        

    #             ,              30       0.001
    criteria = (cv2.TERM_CRITERIA_MAX_ITER | cv2.TERM_CRITERIA_EPS, 30, 0.001)

    #                 ,             ,    Z     0,       x y
    '''
    objp:
    [[0. 0. 0.]
    [1. 0. 0.]
    [2. 0. 0.]
    ...
    [4. 4. 0.]
    [5. 4. 0.]
    [6. 4. 0.]]
    '''
    objp = np.zeros((h * w, 3), np.float32)
    objp[:, :2] = np.mgrid[0:h, 0:w].T.reshape(-1, 2)

    obj_points = []
    img_points = []
  
    for image_path in image_path_list:
        img = cv2.imread(image_path)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        size = gray.shape[::-1]
        ret, corners = cv2.findChessboardCorners(gray, (h, w), None)
        print(ret)

        if ret:
            obj_points.append(objp)

            #                    ,                 
            corners2 = cv2.cornerSubPix(gray, corners, (5, 5), (-1, -1), criteria) 
            
            if [corners2]:
                img_points.append(corners2)
            else:
                img_points.append(corners)

            cv2.drawChessboardCorners(img, (h, w), corners, ret)
            cv2.imshow('img', img)
            cv2.waitKey(1000)

    print(len(img_points))
    cv2.destroyAllWindows()

    # calibration
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, size, None, None)

    np.set_printoptions(suppress=True)
    print("ret:", ret)
    print("mtx:
"
, mtx.reshape(1,-1).tolist()) # print("dist:
"
, dist.tolist()) # distortion cofficients = (k_1,k_2,p_1,p_2,k_3) # print("rvecs:
", len(rvecs)) # #
# print("tvecs:
", tvecs ) # #
print("-----------------------------------------------------") # total_error = 0 for i in range(len(obj_points)): img_points2, _ = cv2.projectPoints(obj_points[i], rvecs[i], tvecs[i], mtx, dist) error = cv2.norm(img_points[i],img_points2, cv2.NORM_L2)/len(img_points2) total_error += error # print("average error: ", total_error/len(img_points2)) def main(): image_path_list = glob.glob("calibrationdata/*.jpg") cal(7, 5, image_path_list) if __name__ == '__main__': main()

私の標定板はこの様子です.
次の2つのリンクは、本明細書と同様のpythonソースコードを持つ比較的詳細な標定原理の説明である可能性があります.https://blog.csdn.net/hehedadaq/article/details/105763241?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-6 https://github.com/1368069096/Calibration_ZhangZhengyou_Method