使用OpenCV库快速求解相机内参
本文主要介紹如何使用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=???fx?00?0fy?0?cx?cy?1????(1)
2.OpenCV庫求相機內參
這里使用官方提供的圖片來演示如何求解相機內參,目前已有某一相機拍攝的棋盤格圖片若干張(20-30張不等),部分圖片如下:
使用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)標定是根據查找棋盤格頂點來標定的,如圖所示:
最后,根據求得的相機內參和畸變系數,可以對失真的圖片進行還原,還原效果如下:
總結
以上是生活随笔為你收集整理的使用OpenCV库快速求解相机内参的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哪些行业属于周期性行业?值得投资的周期性
- 下一篇: 第三条穿越塔克拉玛干的沙漠公路:尉且沙漠