使用OpenCV库快速求解相机内参
本文主要介紹如何使用OpenCV庫函數(shù)求解相機(jī)內(nèi)參。具體可查閱官網(wǎng):https://docs.opencv.org/master/dc/dbb/tutorial_py_calibration.html。
關(guān)于相機(jī)內(nèi)參的求解還有很多其它的工具,如使用MATLAB求解會(huì)更方便,直接調(diào)用MATLAB中的APP即可。
1.背景知識
關(guān)于相機(jī)標(biāo)定的詳細(xì)理論可以參考博客:《深入理解張正友相機(jī)標(biāo)定法:數(shù)學(xué)理論詳細(xì)推導(dǎo)》。
相機(jī)內(nèi)參形式如下,是一個(gè)3×33\times33×3的矩陣,其中(fx,fy)(f_x,f_y)(fx?,fy?)是相機(jī)焦距,(cx,cy)(c_x,c_y)(cx?,cy?)是光學(xué)中心。相機(jī)內(nèi)參對于相機(jī)而言是唯一的,因此只要計(jì)算出了相機(jī)內(nèi)參,就可以在同一相機(jī)拍攝的其它圖像上重復(fù)使用。
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=???fx?00?0fy?0?cx?cy?1????(1)
2.OpenCV庫求相機(jī)內(nèi)參
這里使用官方提供的圖片來演示如何求解相機(jī)內(nèi)參,目前已有某一相機(jī)拍攝的棋盤格圖片若干張(20-30張不等),部分圖片如下:
使用OpenCV庫求解相機(jī)內(nèi)參代碼如下,其中mtx為相機(jī)內(nèi)參, dist為畸變系數(shù),
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)標(biāo)定是根據(jù)查找棋盤格頂點(diǎn)來標(biāo)定的,如圖所示:
最后,根據(jù)求得的相機(jī)內(nèi)參和畸變系數(shù),可以對失真的圖片進(jìn)行還原,還原效果如下:
總結(jié)
以上是生活随笔為你收集整理的使用OpenCV库快速求解相机内参的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哪些行业属于周期性行业?值得投资的周期性
- 下一篇: 华人科学家量子计算机,华人科学家在美国研