摄像头标定实施

Posted wujianming-110117

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了摄像头标定实施相关的知识,希望对你有一定的参考价值。

摄像头标定实施

一.标定流程

在opencv中提供了一组函数用于实现相机的标定,标定返回的值包括:相机内参矩阵(fx fy xc yc)、相机外参矩阵(R t)以及畸变矩阵。

标定的步骤如下:

1. 准备棋盘格,棋盘格图片可以自行打印,以下使用10*7方格的棋盘格,交点则为9*6,棋盘格的大小1mm,即 gridsize=1

2. 拍照,拍照的原则是多角度,根据理论至少要两种角度的拍照,实际中通常会拍20张左右;

3. 使用opencv提供的角点检测函数findChessboardCorners找到棋盘格中的角点,并将每幅图片的角点值存放到list中,同时将棋盘格的角点的三维坐标存放到另一个list。

4. 使用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()

以上是关于摄像头标定实施的主要内容,如果未能解决你的问题,请参考以下文章

opencv实现摄像头内外参数标定

双目立体视觉摄像头的标定矫正世界坐标计算(opencv)

摄像机标定就是要获得摄像机的内外参数,但标定后获得的参数如何载入摄像机?谢谢

双目标定

图像视觉 --鱼眼摄像头标定和图像畸变矫正

ROS-单目摄像头标定