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   

#include       
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;
	else
		cout << "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相机标定工具箱下载

OpenCV | 双目相机标定之OpenCV获取左右相机图像+MATLAB单目标定+双目标定_第1张图片

OpenCV | 双目相机标定之OpenCV获取左右相机图像+MATLAB单目标定+双目标定_第2张图片

 

MATLAB自带单目标定图像

OpenCV | 双目相机标定之OpenCV获取左右相机图像+MATLAB单目标定+双目标定_第3张图片

 

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:

 

这样整个标定就完成了。。。。。。。。。。。

 

你可能感兴趣的:(相机标定,OpenCV)