OpenCV | 双目相机标定之OpenCV获取左右相机图像+MATLAB单目标定+双目标定
博主github:https://github.com/MichaelBeechan
博主CSDN:https://blog.csdn.net/u011344545
原本網上可以搜到很多關于雙目相機標定的文章,但是一直沒得搜到一個完整的從圖像獲取,再到單目標定,最后到雙目標定的全過程。為此,博主寫下了這篇博客,盡微薄之力,為大家呈上Help。多言不虛了,上代碼》》》》》》》》》》》
OpenCV雙目連續圖像幀采集(代碼)可參照博客:https://blog.csdn.net/u011344545/article/details/89005247
1. OpenCV代碼之左右圖像獲取:OpenCV3.0 + VS2010 + 64位操作系統
?
#include "stdafx.h" #include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv;int main() { int i=0; VideoCapture cap_left(1);VideoCapture cap_right(0);if (cap_left.isOpened() && cap_right.isOpened())cout << "camera is opened" << endl;elsecout << "camera is not opened" << endl;bool stop = false;Mat frame;cap_left.set(CAP_PROP_FRAME_WIDTH, 480);cap_left.set(CAP_PROP_FRAME_HEIGHT, 640);cap_right.set(CAP_PROP_FRAME_WIDTH, 480);cap_right.set(CAP_PROP_FRAME_HEIGHT, 640);VideoWriter writer0("two_test0.avi",VideoWriter::fourcc('M', 'J', 'P', 'G'),30, Size(640,480),true); VideoWriter writer1("two_test1.avi",VideoWriter::fourcc('M', 'J', 'P', 'G'),30, Size(640,480),true); Mat img0 , img1; namedWindow("camera_left"); namedWindow("camera_right"); int count = 130;while (count) { cap_left >> img0; // if(!cap_left.read(img0)) // 讀取下一幀圖像 break; writer0 << img0; imshow("camera_left", img0); cap_right >> img1; if(!cap_right.read(img1)) // 讀取下一幀圖像 break; writer1 << img1; imshow("camera_right", img1); waitKey(30); count --; } //讀取獲取到的視頻,進行圖像采集 VideoCapture cap00("two_test0.avi"); VideoCapture cap11("two_test1.avi"); if(cap00.isOpened() && cap11.isOpened()) cout << "ok ,have a video" << endl; else cout << "no video" << endl; Mat frame00, frame11; int num0 = 1, num1 = 1; string filename0; char temp_file0[100]; string path0 = "C:\\Users\\Administrator\\Desktop\\標定圖像獲取\\left\\left";//can change the path for (int i = 1; i < 131; i++) { cap00 >> frame00; if(frame00.empty()) break; if(i % 5 == 0) { _itoa_s(num0, temp_file0, 4, 10); filename0 = temp_file0; filename0 = path0 + filename0 + ".jpg"; num0 ++; imwrite (filename0,frame00); } } cap00.release(); //重復上一步驟 string filename1; char temp_file1[100]; string path1 = "C:\\Users\\Administrator\\Desktop\\標定圖像獲取\\right\\right";//can change the path for (int i = 1; i < 131;i++) { cap11 >> frame11; if(frame11.empty()) break; if(i % 5 == 0) { _itoa_s(num1, temp_file1, 4, 10); filename1 = temp_file1; filename1 = path1 + filename1 + ".jpg"; num1 ++; imwrite (filename1,frame11); } } cap11.release(); return 0; }?
?
Camera Calibration and 3D Reconstruction? ? ?相機標定的函數及公式
?
MATLAB相機標定工具箱下載
?
MATLAB自帶單目標定圖像
?
MATLAB自帶雙目圖像及標定
?
2. Matlab單目標定:MATLAB2014a
?
Left圖像:
Image names—>Read images—> Extract grid corners?
?
Calibration按鈕
Initialization of the intrinsic parameters - Number of images: 20
Calibration parameters after initialization:
?
Focal Length: ?????????fc = [ 1031.50439 ??1031.50439 ]
Principal point: ??????cc = [ 319.50000 ??239.50000 ]
Skew: ????????????alpha_c = [ 0.00000 ] ??=> angle of pixel = 90.00000 degrees
Distortion: ???????????kc = [ 0.00000 ??0.00000 ??0.00000 ??0.00000 ??0.00000 ]
?
Main calibration optimization procedure - Number of images: 20
Gradient descent iterations: 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...done
Estimation of uncertainties...done
?
Calibration results after optimization (with uncertainties):
?
Focal Length: ??????fc = [ 997.33556 ??1001.73912 ] +/- [ 55.05614 ??55.49485 ]
Principal point: ????cc = [ 298.01423 ??249.24570 ] +/- [ 13.54904 ??7.56469 ]
Skew: ???????????alpha_c = [ 0.00000 ] +/- [ 0.00000 ?] ??=> angle of pixel axes = 90.00000 +/- 0.00000 degrees
Distortion: ???????kc = [ 0.01808 ??1.16917 ??0.00352 ??-0.00714 ?0.00000 ] +/- [ 0.04498 ??0.74714 ??0.00278 ??0.00346 ?0.00000 ]
Pixel error: ?????????err = [ 0.18524 ??0.15456 ]
?
Note: The numerical errors are approximately three times the standard deviations (for reference).
?
Recommendation: Some distortion coefficients are found equal to zero (within their uncertainties).
????????????????To reject them from the optimization set est_dist=[0;1;1;1;0] and run Calibration
?
Show Extrinsic
?
Analyse error
?
Recomp.corners ——> ?Calibration
There is now a total of 20 active images for calibration:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 and 20.
?
You may now run 'Calibration' to recalibrate based on this new set of images.
?
Aspect ratio optimized (est_aspect_ratio = 1) -> both components of fc are estimated (DEFAULT).
Principal point optimized (center_optim=1) - (DEFAULT). To reject principal point, set center_optim=0
Skew not optimized (est_alpha=0) - (DEFAULT)
Distortion not fully estimated (defined by the variable est_dist):
?????Sixth order distortion not estimated (est_dist(5)=0) - (DEFAULT) .
?
Main calibration optimization procedure - Number of images: 20
Gradient descent iterations: 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...done
Estimation of uncertainties...done
?
Calibration results after optimization (with uncertainties):
?
Focal Length: ?????????fc = [ 997.53863 ??1001.94304 ] +/- [ 54.94370 ??55.38183 ]
Principal point: ??????cc = [ 298.03007 ??249.25204 ] +/- [ 13.51600 ??7.55436 ]
Skew: ????????????alpha_c = [ 0.00000 ] +/- [ 0.00000 ?] ??=> angle of pixel axes = 90.00000 +/- 0.00000 degrees
Distortion: ???????????kc = [ 0.01788 ??1.17241 ??0.00353 ??-0.00713 ?0.00000 ] +/- [ 0.04494 ??0.74699 ??0.00278 ??0.00345 ?0.00000 ]
Pixel error: ?????????err = [ 0.18501 ??0.15435 ]
?
Note: The numerical errors are approximately three times the standard deviations (for reference).
?
Recommendation: Some distortion coefficients are found equal to zero (within their uncertainties).
????????????????To reject them from the optimization set est_dist=[0;1;1;1;0] and run Calibration
?
visualize_distortions
?
現在,就像練習(在實踐中沒有真正建議的那樣),讓我們在沒有鏡頭扭曲模型的情況下進行優化(通過強制kc =[0;0;0;0;0;0])并且強制fc為0,為此,在matlab提示符中設置二元變量est_dist為[0;0;0;0]和est_aspect_ratio為0:
>> est_dist = [0;0;0;0;0];
>> est_aspect_ratio = 0;
Aspect ratio not optimized (est_aspect_ratio = 0) -> fc(1)=fc(2). Set est_aspect_ratio to 1 for estimating aspect ratio.
Principal point optimized (center_optim=1) - (DEFAULT). To reject principal point, set center_optim=0
Skew not optimized (est_alpha=0) - (DEFAULT)
Distortion not fully estimated (defined by the variable est_dist):
?????Second order distortion not estimated (est_dist(1)=0).
?????Fourth order distortion not estimated (est_dist(2)=0).
?????Sixth order distortion not estimated (est_dist(5)=0) - (DEFAULT) .
?????Tangential distortion not estimated (est_dist(3:4)~=[1;1]).
?
Main calibration optimization procedure - Number of images: 20
Gradient descent iterations: 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...done
Estimation of uncertainties...done
?
Calibration results after optimization (with uncertainties):
?
Focal Length: ?????????fc = [ 859.95431 ??859.95431 ] +/- [ 84.85733 ??84.85733 ]
Principal point: ??????cc = [ 362.68078 ??225.03187 ] +/- [ 12.32947 ??5.63310 ]
Skew: ????????????alpha_c = [ 0.00000 ] +/- [ 0.00000 ?] ??=> angle of pixel axes = 90.00000 +/- 0.00000 degrees
Distortion: ???????????kc = [ 0.00000 ??0.00000 ??0.00000 ??-0.00000 ?0.00000 ] +/- [ 0.00000 ??0.00000 ??0.00000 ??0.00000 ?0.00000 ]
Pixel error: ?????????err = [ 0.21106 ??0.17413 ]
?
Note: The numerical errors are approximately three times the standard deviations (for reference).
?
>> est_aspect_ratio = 0;
>> center_option = 0;
>> center_optim = 0;
>> est_dist = [0;0;0;0;0];
>> eat_alpha = 0;
>> cc = [(nx - 1)/2; (ny - 1)/2];
Aspect ratio not optimized (est_aspect_ratio = 0) -> fc(1)=fc(2). Set est_aspect_ratio to 1 for estimating aspect ratio.
Principal point not optimized (center_optim=0). Note: to set it in the middle of the image, clear variable cc, and run calibration again.
Skew not optimized (est_alpha=0) - (DEFAULT)
Distortion not fully estimated (defined by the variable est_dist):
?????Second order distortion not estimated (est_dist(1)=0).
?????Fourth order distortion not estimated (est_dist(2)=0).
?????Sixth order distortion not estimated (est_dist(5)=0) - (DEFAULT) .
?????Tangential distortion not estimated (est_dist(3:4)~=[1;1]).
?
Main calibration optimization procedure - Number of images: 20
Gradient descent iterations: 1...2...3...4...5...6...7...8...9...10...11...12...13...14...15...16...17...18...19...20...21...22...23...done
Estimation of uncertainties...done
?
Calibration results after optimization (with uncertainties):
?
Focal Length: ?????????fc = [ 1108.24978 ??1108.24978 ] +/- [ 27.98441 ??27.98441 ]
Principal point: ??????cc = [ 319.50000 ??239.50000 ] +/- [ 0.00000 ??0.00000 ]
Skew: ????????????alpha_c = [ 0.00000 ] +/- [ 0.00000 ?] ??=> angle of pixel axes = 90.00000 +/- 0.00000 degrees
Distortion: ???????????kc = [ 0.00000 ??0.00000 ??0.00000 ??-0.00000 ?0.00000 ] +/- [ 0.00000 ??0.00000 ??0.00000 ??0.00000 ?0.00000 ]
Pixel error: ?????????err = [ 0.22425 ??0.19920 ]
?
Note: The numerical errors are approximately three times the standard deviations (for reference).
獲取得到:
Calib_Results_left.mat
Right標定同上:
獲取得到:
Calib_Results_right.mat
3. MATLAB之雙目標定
?
>> stereo_gui
Loading of the individual left and right camera calibration files
Name of the left camera calibration file ([]=Calib_Results_left.mat): Calib_Results_left.mat
Name of the right camera calibration file ([]=Calib_Results_right.mat): Calib_Results_right.mat
Loading the left camera calibration result file Calib_Results_left.mat...
Loading the right camera calibration result file Calib_Results_right.mat...
?
Stereo calibration parameters after loading the individual calibration files:
?
Intrinsic parameters of left camera:
?
這樣整個標定就完成了。。。。。。。。。。。
?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的OpenCV | 双目相机标定之OpenCV获取左右相机图像+MATLAB单目标定+双目标定的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法 | RASAC 入门
- 下一篇: 视觉里程计 | 关于Stereo DSO