本文主要介绍如何使用
OpenCV
库函数求解相机内参。具体可查阅官网:https://docs.opencv.org/master/dc/dbb/tutorial_py_calibration.html。
关于相机内参的求解还有很多其它的工具,如使用MATLAB求解会更方便,直接调用MATLAB
中的APP
即可。
1.背景知识
关于相机标定的详细理论可以参考博客:《深入理解张正友相机标定法:数学理论详细推导》。
相机内参形式如下,是一个
3
×
3
3times3
3×3的矩阵,其中
(
f
x
,
f
y
)
(f_x,f_y)
(fx,fy)是相机焦距,
(
c
x
,
c
y
)
(c_x,c_y)
(cx,cy)是光学中心。相机内参对于相机而言是唯一的,因此只要计算出了相机内参,就可以在同一相机拍摄的其它图像上重复使用。
K
=
[
f
x
0
c
x
0
f
y
c
y
0
0
1
]
(1)
K = left[ begin{matrix} f_x & 0 & c_x \ 0 & f_y & c_y \ 0 & 0 & 1 end{matrix} right] tag{1}
K=⎣⎡fx000fy0cxcy1⎦⎤(1)
2.OpenCV库求相机内参
这里使用官方提供的图片来演示如何求解相机内参,目前已有某一相机拍摄的棋盘格图片若干张(20-30张不等),部分图片如下:
image1 | image2 |
---|---|
![]() | ![]() |
使用OpenCV 库求解相机内参代码如下,其中mtx 为相机内参, dist 为畸变系数, |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40import numpy as np import cv2 import glob # extract object points and image points 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 = [] imgpoints = [] images = glob.glob('calibration_wide/GO*.jpg') # Step through the list and search for chessboard corners for idx, fname in enumerate(images): img = cv2.imread(fname) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Find the chessboard corners ret, corners = cv2.findChessboardCorners(gray, (8, 6), None) if ret == True: objpoints.append(objp) imgpoints.append(corners) # Draw and display the corners cv2.drawChessboardCorners(img, (8, 6), corners, ret) cv2.imshow('img', img) cv2.waitKey(500) # calibrate img = cv2.imread('test_image.jpg') img_size = (img.shape[1], img.shape[0]) # Do camera calibration given objects points and image points ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size, None, None) dst = cv2.undistort(img, mtx, dist, None, mtx) cv2.imwrite('test_undist.jpg', dst)
标定是根据查找棋盘格顶点来标定的,如图所示:
image1 | image2 |
---|---|
![]() | ![]() |
最后,根据求得的相机内参和畸变系数,可以对失真的图片进行还原,还原效果如下: |
undist_image | dist_image |
---|---|
![]() | ![]() |
最后
以上就是大胆月光最近收集整理的关于使用OpenCV库快速求解相机内参的全部内容,更多相关使用OpenCV库快速求解相机内参内容请搜索靠谱客的其他文章。
发表评论 取消回复