网站后台别人制作,做个app软件多少钱,广告传媒公司注册费用,成都私人网站制作公司本文主要介绍如何使用OpenCV库函数求解相机内参。具体可查阅官网#xff1a;https://docs.opencv.org/master/dc/dbb/tutorial_py_calibration.html。 关于相机内参的求解还有很多其它的工具#xff0c;如使用MATLAB求解会更方便#xff0c;直接调用MATLAB中的APP即可。 1.背… 本文主要介绍如何使用OpenCV库函数求解相机内参。具体可查阅官网https://docs.opencv.org/master/dc/dbb/tutorial_py_calibration.html。 关于相机内参的求解还有很多其它的工具如使用MATLAB求解会更方便直接调用MATLAB中的APP即可。 1.背景知识
关于相机标定的详细理论可以参考博客《深入理解张正友相机标定法数学理论详细推导》。 相机内参形式如下是一个3×33\times33×3的矩阵其中(fx,fy)(f_x,f_y)(fx,fy)是相机焦距(cx,cy)(c_x,c_y)(cx,cy)是光学中心。相机内参对于相机而言是唯一的因此只要计算出了相机内参就可以在同一相机拍摄的其它图像上重复使用。 K[fx0cx0fycy001](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张不等部分图片如下
image1image2使用OpenCV库求解相机内参代码如下其中mtx为相机内参, dist为畸变系数
import 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 cornersret, corners cv2.findChessboardCorners(gray, (8, 6), None) if ret True:objpoints.append(objp)imgpoints.append(corners)# Draw and display the cornerscv2.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)标定是根据查找棋盘格顶点来标定的如图所示
image1image2最后根据求得的相机内参和畸变系数可以对失真的图片进行还原还原效果如下
undist_imagedist_image