摄像头标定实施
攝像頭標定實施
一.標定流程
在opencv中提供了一組函數用于實現相機的標定,標定返回的值包括:相機內參矩陣(fx fy xc yc)、相機外參矩陣(R t)以及畸變矩陣。
標定的步驟如下:
-
準備棋盤格,棋盤格圖片可以自行打印,以下使用107方格的棋盤格,交點則為96,棋盤格的大小1mm,即 gridsize=1
-
拍照,拍照的原則是多角度,根據理論至少要兩種角度的拍照,實際中通常會拍20張左右;
-
使用opencv提供的角點檢測函數findChessboardCorners找到棋盤格中的角點,并將每幅圖片的角點值存放到list中,同時將棋盤格的角點的三維坐標存放到另一個list。
-
使用calibrateCamera函數獲取內存矩陣、畸變矩陣、旋轉矩陣以及轉移矩陣。
5.使用undistort函數將畸變的圖像進行校正并查看校正后的圖片效果。
二.README.md:
calibrate camera 相機校正,使用opencv自帶的函數庫,計算出如下幾個參數。
內參矩陣: 3*3
In [22]: mtx
Out[22]:
array([[1.16022336e+03, 0.00000000e+00, 6.68285471e+02],
[0.00000000e+00, 1.15738493e+03, 3.89459697e+02],
[0.00000000e+00, 0.00000000e+00, 1.00000000e+00]])
畸變矩陣: 1*5
In [25]: dist
Out[25]: array([[-0.25129777, 0.02823272, -0.00053603, 0.00037274,
-0.08995589]])
旋轉矩陣:
In [27]: rvecs
Out[27]: 18*(3*1)
[array([[ 0.03558055], [-0.03112721], [-0.00755535]]),
array([[ 0.63788424], [-0.04903354], [ 0.01762295]]),
array([[-0.44908256], [-0.06512295], [-0.01916963]]),
array([[ 0.01780734], [ 0.0209126 ], [-0.00558506]]),
array([[0.02198169], [0.6367404 ], [0.00977959]]),
array([[ 0.03046199], [-0.7040381 ], [-0.01932221]]),
array([[-0.19237824], [-0.75952006], [ 0.1201012 ]]),
array([[ 0.51440228], [-0.2194547 ], [ 0.02910641]]),
array([[0.03761499], [0.45929723], [0.00663988]]),
array([[0.03691831], [0.64815823], [0.01041448]]),
array([[-0.3272451 ], [ 0.65900314], [-0.41478724]]),
array([[ 0.05770817], [-0.51997066], [-0.00538347]]),
array([[-0.01886995], [-0.48934854], [ 0.01885913]]),
array([[ 0.04012555], [-0.46639335], [-0.05743551]]),
array([[ 0.18608573], [-0.05068572], [-0.00117477]]),
array([[ 0.22181091], [-0.06412907], [ 0.0115335 ]]),
array([[0.0882598 ], [0.38487441], [0.05529661]]),
array([[-0.01748482], [ 0.38362373], [-0.00271536]])]
平移向量: 18*(3*1)
In [33]: tvecs
Out[33]:
[array([[-4.21904478], [-2.32362579], [ 8.49747635]]),
array([[-3.81963279], [-1.62195346], [ 7.98860175]]),
array([[-4.39150219], [-3.07999134], [10.75041784]]),
array([[-4.94259067], [-3.93663095], [30.57685167]]),
array([[-9.62311687], [-3.36509195], [32.2423649 ]]),
array([[ 0.73000489], [-2.96584094], [19.6837078 ]]),
array([[-0.85448692], [-4.63545431], [21.80683115]]),
array([[-2.09590548], [-0.77674132], [19.65246328]]),
array([[-16.99384696],[ -3.57759924],[ 32.14998811]]),
array([[-0.19382096], [-3.52948313], [21.95184873]]),
array([[-6.04109212], [-1.6349801 ], [26.75950346]]),
array([[ 5.40125149], [-4.50757377], [20.8880559 ]]),
array([[ 4.51225567], [-1.52138071], [20.08076553]]),
array([[ 4.91858056], [-5.1101675 ], [19.89170706]]),
array([[-3.63023758], [-4.17313449], [17.87154811]]),
array([[-3.96462648], [-1.36057071], [17.1048456 ]]),
array([[-13.02067108],[ -5.65276501],[ 23.81054552]]),
array([[-13.4309631 ],[ -0.55047404],[ 24.62701854]])]
通過計算后的參數生成未畸變的圖片image.jpg
三.calibrate.py
#!/usr/bin/env python3
-- coding: utf-8 --
“”"
Created on Wed Oct 16 08:45:25 2019
@author: hmeng
“”"
import numpy as np
import cv2
objp_dict = {
1: (9, 5),
2: (9, 6),
3: (9, 6),
4: (9, 6),
5: (9, 6),
6: (9, 6),
7: (9, 6),
8: (9, 6),
9: (9, 6),
10: (9, 6),
11: (9, 6),
12: (9, 6),
13: (9, 6),
14: (9, 6),
15: (9, 6),
16: (9, 6),
18: (9, 6),
17: (9, 6),
19: (9, 6),
20: (9, 6),
}
objp_list = []
corners_list = []
for k in objp_dict:
nx, ny = objp_dict[k]
Prepare object points, like (0,0,0), (1,0,0), (2,0,0) …,(6,5,0)
objp = np.zeros((nx*ny,3), np.float32)
objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1,2)
Make a list of calibration images
fname = ‘camera_cal/calibration%s.jpg’ % str(k)
img = cv2.imread(fname)
Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)
If found, save & draw corners
if ret == True:
Save object points and corresponding corners
objp_list.append(objp)
corners_list.append(corners)
Draw and display the corners
#cv2.drawChessboardCorners(img, (nx, ny), corners, ret)
#plt.imshow(img)
#plt.show()
#print(‘Found corners for %s’ % fname)
else:
print(‘Warning: ret = %s for %s’ % (ret, fname))
img = cv2.imread(‘camera_cal/calibration1.jpg’)
img_size = (img.shape[1], img.shape[0])
‘’’
mtx :
dist:
‘’’
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objp_list, corners_list,
img_size,None,None)
dst = cv2.undistort(img, mtx, dist, None, mtx)
com_img = np.hstack((img, dst))
cv2.namedWindow(‘image’, cv2.WINDOW_NORMAL)
cv2.imshow(‘image’, com_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
總結
- 上一篇: 相机畸变与标定
- 下一篇: 2020年Yann Lecun深度学习笔