1. 程式人生 > >魚眼相機與針孔相機的標定與校正基於MATLAB和Opencv

魚眼相機與針孔相機的標定與校正基於MATLAB和Opencv

魚眼相機的標定比針孔相機的標定的原理與方法類似,這裡貼上兩種自己親測可用的方案吧。

首先是張正友的MATLAB工具箱,下載。從這個超連結可以下載工具箱,也有使用方法的介紹。

普通針孔相機的標定過程:
1. Run the main matlab calibration function calib_gui_normal/calib_gui.m/calib_gui_no_read/calib_gui_fisheye
2. Reading the images(20): Click on the Image names button, Enter the basename of the calibration images (Image) and the image format (tif).這裡當然需要事先把標定照片放在該目錄下


3. Extract the grid corners: Click on the Extract grid corners button.Press “enter” (with an empty argument) to select all the images
(otherwise, you would enter a list of image indices like [2 5 8 10 12] to extract corners of a subset of images).
Then, select the default window size of the corner finder: wintx=winty=5 by pressing “enter” with empty arguments to the wintx and winty question.
(WARNING: try to click accurately on the four corners(LU,RU,RD,LD), at most 5 pixels away from the corners. Otherwise some of the corners might be missed by the detector).
dX=dY=30mm=default values; 手動拾取角點並輸入三維網格邊長
如果是魚眼相機,那麼這裡需要手動標定一下,需要在MATLAB的窗口裡輸入“run manual_corner_extraction.m”進入手動標定的方式

4. Main Calibration step: click on the button Calibration.Calibration is done in two steps: first initialization, and then nonlinear optimization.
5. Recomp. corners button. 自動標出grid corners
6. Run then another calibration optimization by clicking on Calibration:
7. click on Save to save the calibration results (intrinsic and extrinsic) in the matlab file Calib_Results.mat
click on Reproject on images to reproject the grids onto the original calibration images.
Click on Analyse error to view the new reprojection error.
click on Show Extrinsic to show the new 3D positions of the grids with respect to the camera.

我用這種方法標定出魚眼的結果是
Focal Length: fc = [ 497.36165 494.92849 ] +- [ 4.08794 3.82063 ]
Principal point: cc = [ 958.99209 454.68095 ] +- [ 2.38658 1.82875 ]
Skew: alpha_c = [ 0.00000 ] +- [ 0.00000 ] => angle of pixel axes = 90.00000 ? 0.00000 degrees
Fisheye Distortion: kc = [ -0.02813 0.00601 -0.00648 0.00076 ] ? [ 0.02079 0.03954 0.02987 0.00764 ]
Pixel error: err = [ 1.19736 1.50271 ]

更新一下,2016版的matlab的APP裡有直接的camera calibrator的應用

但是MATLAB工具箱這種方法不夠方便,比如需要手工標定角點,特別考驗眼力。所以後來我使用了Opencv的方法。

在《學習Opencv》一書中有針對針孔相機的標定的原始碼。魚眼相機的標定與其流程相似,只是要修改相關專門用於fisheye的函式,這些函式在Opencv裡也有實現,見魚眼函式的官方文件

我把用於針孔相機的標定程式碼,使用線上方式標定魚眼相機和離線方式標定魚眼相機的程式碼都上傳到了GitHub,點選移步GitHub

用這種方法標定的結果是
畫素

相機內參數矩陣:
[483.2704156971429, 0, 960.4974562203088;
0, 486.1219309294918, 459.4646834483839;
0, 0, 1]
畸變係數:
[-0.0258665, 0.0110641, -0.00669646, -2.38718e-05]
總體平均誤差:0.14081畫素