深度學(xué)習(xí)(全網(wǎng)最優(yōu)源碼學(xué)習(xí)中,一鍵三聯(lián))——相機(jī)標(biāo)定
import numpy as np import CV2
設(shè)置棋盤(pán)格尺寸及其內(nèi)角點(diǎn)個(gè)數(shù)
chessboard_size = (9, 6) objp = np.zeros((np.prod(chessboard_size), 3), dtype=np.float32) objp[:, :2] = np.indices(chessboard_size).T.reshape(-1, 2)
獲取棋盤(pán)格內(nèi)角點(diǎn)及其對(duì)應(yīng)的3D坐標(biāo)
img_points = [] obj_points = [] images = ['image1.jpg', 'image2.jpg', 'image3.jpg'] # 設(shè)置不同角度下的圖片路徑 for idx, fname in enumerate(images): img = CV2.imread(fname) gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY) ret, corners = CV2.findChessboardCorners(gray, chessboard_size, None) if ret: obj_points.append(objp) corners2 = CV2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria=(CV2.TERM_CRITERIA_EPS + CV2.TERM_CRITERIA_MAX_ITER, 30, 0.1)) img_points.append(corners)
標(biāo)定相機(jī)
ret, mtx, dist, rvecs, tvecs = CV2.calibrateCamera(obj_points, img_points, gray.shape[::-1], None, None)
打印相機(jī)內(nèi)參和畸變系數(shù)
print("Camera matrix : \n") print(mtx) print("\nDistortion coefficients : \n") print(dist)