参考资料:
import numpy as np
import cv2 as cv
import glob
# termination criteria
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*8,3), np.float32)
objp[:,:2] = np.mgrid[0:8,0:6].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = glob.glob('C:\\Users\\26909\Desktop\camera_data\*.jpg')
for fname in images:
img = cv.imread(fname)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv.findChessboardCorners(gray, (8,6), None)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners)
# Draw and display the corners
cv.drawChessboardCorners(img, (8,6), corners2, ret)
cv.imshow('img', img)
cv.waitKey(500)
cv.destroyAllWindows()
#参数
ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
print("ret:", ret)
print("mtx:\n", mtx) # 内参数矩阵
print("dist:\n", dist) # 畸变系数 distortion cofficients = (k_1,k_2,p_1,p_2,k_3)
print("rvecs:\n", rvecs) # 旋转向量 # 外参数
print("tvecs:\n", tvecs ) # 平移向量 # 外参数
#畸变矫正
img = cv.imread('C:\\Users\\26909\Desktop\camera_data\IMG12.jpg')
h, w = img.shape[:2]
newcameramtx, roi = cv.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))
# undistort
dst = cv.undistort(img, mtx, dist, None, newcameramtx)
# crop the image
x, y, w, h = roi
dst = dst[y:y+h, x:x+w]
cv.imwrite('calibresult.png', dst)
实验结果
3.1 相机参数标定及可视化
标定结果:
ret: 0.7404995966474621
mtx:
[[274.4022963 0. 309.41661229]
[ 0. 274.81034452 228.80419383]
[ 0. 0. 1. ]]
dist:
[[-0.3454201 0.1457639 -0.00195636 0.00171827 -0.02320787]]
rvecs:
(array([[ 0.14050152],
[-0.08172329],
[ 1.55213876]]), array([[-0.50002852],
[-0.20014157],
[-1.38796715]]), array([[-0.20899323],
[-0.3015702 ],
[ 1.17631626]]), array([[ 0.14426414],
[-0.48615287],
[ 1.25333586]]), array([[-0.11196678],
[ 0.13929802],
[-1.51166718]]), array([[-0.20041373],
[ 0.21822841],
[-1.48091619]]), array([[-0.23701475],
[ 0.18275228],
[-0.84192608]]), array([[-0.1877632 ],
[ 0.08558443],
[-0.38675306]]), array([[-0.12538803],
[ 0.23143798],
[-1.36803536]]), array([[-0.25324129],
[ 0.26484299],
[-1.0249051 ]]), array([[-0.15181587],
[ 0.17507901],
[-1.19285926]]), array([[ 0.16808607],
[ 0.09056166],
[-0.01890093]]), array([[0.00670207],
[0.36090502],
[1.46210405]]), array([[0.575493 ],
[0.1586262 ],
[1.02133379]]), array([[-0.06530318],
[ 0.2759119 ],
[-0.76715368]]), array([[-0.17849515],
[ 0.1838762 ],
[ 0.00634664]]), array([[-0.2013951 ],
[ 0.09738451],
[ 0.72377849]]), array([[-0.44136116],
[ 0.37411479],
[-0.53144601]]), array([[-0.5093139 ],
[ 0.11895948],
[-0.65299492]]), array([[-0.57225479],
[-0.06450065],
[-1.01460071]]))
tvecs:
(array([[ 2.07154433],
[-2.30660832],
[ 7.4506053 ]]), array([[-3.95294077],
[ 3.55032646],
[ 8.64371851]]), array([[ 2.99558578],
[-3.57085102],
[11.73926593]]), array([[ 2.3984248 ],
[-3.01994723],
[10.69421613]]), array([[-0.31440216],
[ 4.03730762],
[11.24204555]]), array([[-0.96673367],
[ 3.831028 ],
[ 9.009737 ]]), array([[-3.63737237],
[ 1.72964791],
[10.5699932 ]]), array([[-4.74312794],
[-0.28685914],
[10.31412678]]), array([[-3.13384807],
[ 3.48362812],
[12.11070046]]), array([[-6.74105261],
[ 1.96433573],
[12.56616204]]), array([[-5.97698749],
[ 2.40248059],
[ 8.73901348]]), array([[-3.365379 ],
[-2.08857099],
[ 7.58537102]]), array([[ 4.60034986],
[-1.63283684],
[10.16924106]]), array([[ 0.96341843],
[-2.83724946],
[ 7.42342456]]), array([[-3.96962978],
[ 1.8225874 ],
[11.23708158]]), array([[-3.97587032],
[-1.26762813],
[11.19197982]]), array([[-2.10782797],
[-4.64916982],
[10.41076044]]), array([[-6.00656584],
[-0.11113525],
[11.82626777]]), array([[-8.59365244],
[-1.02351119],
[11.98704289]]), array([[-6.2890475 ],
[ 1.47511978],
[ 9.99562781]]))
使用MATLAB的相机标定工具cameraCalibrator进行可视化:
3.2 根据标定参数进行畸变校正
校正前: 校正后: