史上最简单Opencv相机畸变矫正教学

2023-05-16

最近因为项目需要研究了一下摄像头的畸变矫正,我打算通过写这篇博客记录一下相关流程。其实关于摄像头畸变矫正的原理,网络上已经有非常多的博客可以参考了,我在博客里也就不再赘述了。利用Opencv库中的接口,可以很方便地对一款固定型号的摄像头进行矫正,一般地我们将这个过程分成两步:生成参数文件和矫正。

生成参数文件

这里使用的是OpenCV的例程(非常方便非常好用~),例程可以在你的opencv源码目录下找到,具体位置在sources\samples\cpp\tutorial_code\calib3d\camera_calibration

我们可以用例程的源码在IDE上建立专门用来做摄像头标定和矫正的工程,虽然我们这篇博客讲的只有矫正,其实整个例程是可以同时把标定过程也完成的,在运行程序之前我们需要保证在工程的构建目录中有以下几个文件:

  1. default.xml(后附,保存标定及矫正程序过程中的各项参数)
  2. VID5.xml(后附,保存棋盘格图片的文件路径)
  3. 摄像头所拍摄的棋盘格的图片10~20张:棋盘格标定板有很多规格的(其实都是自己打印出来的),一般都是9*6或者10*7的,方格的边长一般是30mm也并没有固定的标准,其实这些参数在defalut.xml中都是可以根据实际情况进行设置的,所以不同的规格并不影响最终矫正的效果。拍摄的图片要保证整个棋盘格都在成像区内,尽量保证多张图片之间棋盘格在图像中的成像角度以及所在位置都各不相同,棋盘格图片如下所示:

尽量能达到下面的这种效果,即在各个位置(红色方框)都有棋盘格的图片:

做矫正之前的所有准备都已经说清楚了,下面附上Opencv中的例程和关键文件default.xml和VID5.xml,这两个文件一定要放在工程的构建目录下。因为default.xml和VID5.xml的配置比较重要,所以就先上default.xml和VID5.xml了:

default.xml

<?xml version="1.0"?>
<opencv_storage>
<Settings>
  <!-- Number of inner corners per a item row and column. (square, circle) -->
  <BoardSize_Width> 9</BoardSize_Width>
  <BoardSize_Height>6</BoardSize_Height>
  
  <!-- The size of a square in some user defined metric system (pixel, millimeter)-->
  <Square_Size>30</Square_Size>
  
  <!-- The type of input used for camera calibration. One of: CHESSBOARD CIRCLES_GRID ASYMMETRIC_CIRCLES_GRID -->
  <Calibrate_Pattern>"CHESSBOARD"</Calibrate_Pattern>
  
  <!-- The input to use for calibration. 
		To use an input camera -> give the ID of the camera, like "1"
		To use an input video  -> give the path of the input video, like "/tmp/x.avi"
		To use an image list   -> give the path to the XML or YAML file containing the list of the images, like "/tmp/circles_list.xml"
		-->
  <Input>"H:\\Distortion\\build\\VID5.xml"</Input>
  <!--  If true (non-zero) we flip the input images around the horizontal axis.-->
  <Input_FlipAroundHorizontalAxis>0</Input_FlipAroundHorizontalAxis>
  
  <!-- Time delay between frames in case of camera. -->
  <Input_Delay>100</Input_Delay>	
  
  <!-- How many frames to use, for calibration. -->
  <Calibrate_NrOfFrameToUse>25</Calibrate_NrOfFrameToUse>
  <!-- Consider only fy as a free parameter, the ratio fx/fy stays the same as in the input cameraMatrix. 
	   Use or not setting. 0 - False Non-Zero - True-->
  <Calibrate_FixAspectRatio> 1 </Calibrate_FixAspectRatio>
  <!-- If true (non-zero) tangential distortion coefficients  are set to zeros and stay zero.-->
  <Calibrate_AssumeZeroTangentialDistortion>1</Calibrate_AssumeZeroTangentialDistortion>
  <!-- If true (non-zero) the principal point is not changed during the global optimization.-->
  <Calibrate_FixPrincipalPointAtTheCenter> 1 </Calibrate_FixPrincipalPointAtTheCenter>
  
  <!-- The name of the output log file. -->
  <Write_outputFileName>"out_camera_data.xml"</Write_outputFileName>
  <!-- If true (non-zero) we write to the output file the feature points.-->
  <Write_DetectedFeaturePoints>1</Write_DetectedFeaturePoints>
  <!-- If true (non-zero) we write to the output file the extrinsic camera parameters.-->
  <Write_extrinsicParameters>1</Write_extrinsicParameters>
  <!-- If true (non-zero) we show after calibration the undistorted images.-->
  <Show_UndistortedImage>1</Show_UndistortedImage>
 
</Settings>
</opencv_storage>

这就是非常关键的default.xml文件,其中有几个参数是需要根据具体情况进行对应设置的:

  • BoardSize_Width表示的是棋盘格角点矩阵的宽度,BoardSize_Height表示的是棋盘格角点矩阵的高度(其实很简单,就是看棋盘格宽和高分别有多少个黑白格,然后分别减1,从上面的图片可以看到我是用的是10*7的标定板,那么这里的参数应该就是9*6)。
  • Square_Size表示的是标定板每个黑白格的边长(30mm)
  • Input表示的输入文件VID5.xml所在位置,我这里是直接放在了工程的构建目录中"H:\\Distortion\\build\\VID5.xml",前面也说到了VID5.xml存放的就是棋盘格图片的文件路径。

VID5.xml

<?xml version="1.0"?>
<opencv_storage>
<images>
H:/Distortion/build/pic/PICT0022.jpg
H:/Distortion/build/pic/PICT0025.jpg
H:/Distortion/build/pic/PICT0026.jpg
H:/Distortion/build/pic/PICT0027.jpg
H:/Distortion/build/pic/PICT0028.jpg
H:/Distortion/build/pic/PICT0029.jpg
H:/Distortion/build/pic/PICT0030.jpg
H:/Distortion/build/pic/PICT0031.jpg
H:/Distortion/build/pic/PICT0032.jpg
H:/Distortion/build/pic/PICT0033.jpg
H:/Distortion/build/pic/PICT0034.jpg
H:/Distortion/build/pic/PICT0037.jpg

</images>
</opencv_storage>

image中的每一个条目都对应这一张用来做畸变矫正的棋盘格图片,VID5.xml文件还是非常好配置的。

最后就是Opencv中标定和矫正的例程了,我们只需要把源码放进配置好Opencv环境的工程中就可以了,因为例程的代码有些长,我们就把代码放在最后了。程序运行完成之后会在构建目录下生成一个参数文件out_camera_data.xml,这个文件十分重要,它内部包含着标定和矫正的所有结果参数,也就是说同一款摄像头我们只需要做一次标定和矫正,之后的应用都只需要参数文件就行了。

矫正

其实运用参数文件进行矫正的过程非常简单,只需要将out_camera_data.xml中的参数读入,然后调用undistort函数就行了。

关键代码如下:

        Mat src = imread("PICT0039.jpg");
        Mat distortion = src.clone();
        Mat camera_matrix = Mat(3, 3, CV_32FC1);
        Mat distortion_coefficients;

//导入相机内参和畸变系数矩阵
        FileStorage file_storage("out_camera_data.xml", FileStorage::READ);
        file_storage["Camera_Matrix"] >> camera_matrix;
        file_storage["Distortion_Coefficients"] >> distortion_coefficients;
        file_storage.release();

        //矫正
        undistort(src, distortion, camera_matrix, distortion_coefficients);

camera_matrix和distortion_coefficients都是存储在cv::Mat中的参数,src和distortion分别是原图像和矫正过后的图像。效果如下:

矫正前
矫正后

最后附上Opencv中的例程:

#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>

#ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
#endif

using namespace cv;
using namespace std;

static void help()
{
    cout <<  "This is a camera calibration sample." << endl
         <<  "Usage: calibration configurationFile"  << endl
         <<  "Near the sample file you'll find the configuration file, which has detailed help of "
             "how to edit it.  It may be any OpenCV supported file format XML/YAML." << endl;
}
class Settings
{
public:
    Settings() : goodInput(false) {}
    enum Pattern { NOT_EXISTING, CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID };
    enum InputType {INVALID, CAMERA, VIDEO_FILE, IMAGE_LIST};

    void write(FileStorage& fs) const                        //Write serialization for this class
    {
        fs << "{" << "BoardSize_Width"  << boardSize.width
                  << "BoardSize_Height" << boardSize.height
                  << "Square_Size"         << squareSize
                  << "Calibrate_Pattern" << patternToUse
                  << "Calibrate_NrOfFrameToUse" << nrFrames
                  << "Calibrate_FixAspectRatio" << aspectRatio
                  << "Calibrate_AssumeZeroTangentialDistortion" << calibZeroTangentDist
                  << "Calibrate_FixPrincipalPointAtTheCenter" << calibFixPrincipalPoint

                  << "Write_DetectedFeaturePoints" << bwritePoints
                  << "Write_extrinsicParameters"   << bwriteExtrinsics
                  << "Write_outputFileName"  << outputFileName

                  << "Show_UndistortedImage" << showUndistorsed

                  << "Input_FlipAroundHorizontalAxis" << flipVertical
                  << "Input_Delay" << delay
                  << "Input" << input
           << "}";
    }
    void read(const FileNode& node)                          //Read serialization for this class
    {
        node["BoardSize_Width" ] >> boardSize.width;
        node["BoardSize_Height"] >> boardSize.height;
        node["Calibrate_Pattern"] >> patternToUse;
        node["Square_Size"]  >> squareSize;
        node["Calibrate_NrOfFrameToUse"] >> nrFrames;
        node["Calibrate_FixAspectRatio"] >> aspectRatio;
        node["Write_DetectedFeaturePoints"] >> bwritePoints;
        node["Write_extrinsicParameters"] >> bwriteExtrinsics;
        node["Write_outputFileName"] >> outputFileName;
        node["Calibrate_AssumeZeroTangentialDistortion"] >> calibZeroTangentDist;
        node["Calibrate_FixPrincipalPointAtTheCenter"] >> calibFixPrincipalPoint;
        node["Input_FlipAroundHorizontalAxis"] >> flipVertical;
        node["Show_UndistortedImage"] >> showUndistorsed;
        node["Input"] >> input;
        node["Input_Delay"] >> delay;
        interprate();
    }
    void interprate()
    {
        goodInput = true;
        if (boardSize.width <= 0 || boardSize.height <= 0)
        {
            cerr << "Invalid Board size: " << boardSize.width << " " << boardSize.height << endl;
            goodInput = false;
        }
        if (squareSize <= 10e-6)
        {
            cerr << "Invalid square size " << squareSize << endl;
            goodInput = false;
        }
        if (nrFrames <= 0)
        {
            cerr << "Invalid number of frames " << nrFrames << endl;
            goodInput = false;
        }

        if (input.empty())      // Check for valid input
                inputType = INVALID;
        else
        {
            if (input[0] >= '0' && input[0] <= '9')
            {
                stringstream ss(input);
                ss >> cameraID;
                inputType = CAMERA;
            }
            else
            {
                if (readStringList(input, imageList))
                    {
                        inputType = IMAGE_LIST;
                        nrFrames = (nrFrames < (int)imageList.size()) ? nrFrames : (int)imageList.size();
                    }
                else
                    inputType = VIDEO_FILE;
            }
            if (inputType == CAMERA)
                inputCapture.open(cameraID);
            if (inputType == VIDEO_FILE)
                inputCapture.open(input);
            if (inputType != IMAGE_LIST && !inputCapture.isOpened())
                    inputType = INVALID;
        }
        if (inputType == INVALID)
        {
            cerr << " Inexistent input: " << input;
            goodInput = false;
        }

        flag = 0;
        if(calibFixPrincipalPoint) flag |= CV_CALIB_FIX_PRINCIPAL_POINT;
        if(calibZeroTangentDist)   flag |= CV_CALIB_ZERO_TANGENT_DIST;
        if(aspectRatio)            flag |= CV_CALIB_FIX_ASPECT_RATIO;


        calibrationPattern = NOT_EXISTING;
        if (!patternToUse.compare("CHESSBOARD")) calibrationPattern = CHESSBOARD;
        if (!patternToUse.compare("CIRCLES_GRID")) calibrationPattern = CIRCLES_GRID;
        if (!patternToUse.compare("ASYMMETRIC_CIRCLES_GRID")) calibrationPattern = ASYMMETRIC_CIRCLES_GRID;
        if (calibrationPattern == NOT_EXISTING)
            {
                cerr << " Inexistent camera calibration mode: " << patternToUse << endl;
                goodInput = false;
            }
        atImageList = 0;

    }
    Mat nextImage()
    {
        Mat result;
        if( inputCapture.isOpened() )
        {
            Mat view0;
            inputCapture >> view0;
            view0.copyTo(result);
        }
        else if( atImageList < (int)imageList.size() )
            result = imread(imageList[atImageList++], CV_LOAD_IMAGE_COLOR);

        return result;
    }

    static bool readStringList( const string& filename, vector<string>& l )
    {
        l.clear();
        FileStorage fs(filename, FileStorage::READ);
        if( !fs.isOpened() )
            return false;
        FileNode n = fs.getFirstTopLevelNode();
        if( n.type() != FileNode::SEQ )
            return false;
        FileNodeIterator it = n.begin(), it_end = n.end();
        for( ; it != it_end; ++it )
            l.push_back((string)*it);
        return true;
    }
public:
    Size boardSize;            // The size of the board -> Number of items by width and height
    Pattern calibrationPattern;// One of the Chessboard, circles, or asymmetric circle pattern
    float squareSize;          // The size of a square in your defined unit (point, millimeter,etc).
    int nrFrames;              // The number of frames to use from the input for calibration
    float aspectRatio;         // The aspect ratio
    int delay;                 // In case of a video input
    bool bwritePoints;         //  Write detected feature points
    bool bwriteExtrinsics;     // Write extrinsic parameters
    bool calibZeroTangentDist; // Assume zero tangential distortion
    bool calibFixPrincipalPoint;// Fix the principal point at the center
    bool flipVertical;          // Flip the captured images around the horizontal axis
    string outputFileName;      // The name of the file where to write
    bool showUndistorsed;       // Show undistorted images after calibration
    string input;               // The input ->



    int cameraID;
    vector<string> imageList;
    int atImageList;
    VideoCapture inputCapture;
    InputType inputType;
    bool goodInput;
    int flag;

private:
    string patternToUse;


};

static void read(const FileNode& node, Settings& x, const Settings& default_value = Settings())
{
    if(node.empty())
        x = default_value;
    else
        x.read(node);
}

enum { DETECTION = 0, CAPTURING = 1, CALIBRATED = 2 };

bool runCalibrationAndSave(Settings& s, Size imageSize, Mat&  cameraMatrix, Mat& distCoeffs,
                           vector<vector<Point2f> > imagePoints );

int main(int argc, char* argv[])
{
    help();
    Settings s;
    const string inputSettingsFile = argc > 1 ? argv[1] : "default.xml";
    FileStorage fs(inputSettingsFile, FileStorage::READ); // Read the settings
    if (!fs.isOpened())
    {
        cout << "Could not open the configuration file: \"" << inputSettingsFile << "\"" << endl;
        return -1;
    }
    fs["Settings"] >> s;
    fs.release();                                         // close Settings file

    if (!s.goodInput)
    {
        cout << "Invalid input detected. Application stopping. " << endl;
        return -1;
    }

    vector<vector<Point2f> > imagePoints;
    Mat cameraMatrix, distCoeffs;
    Size imageSize;
    int mode = s.inputType == Settings::IMAGE_LIST ? CAPTURING : DETECTION;
    clock_t prevTimestamp = 0;
    const Scalar RED(0,0,255), GREEN(0,255,0);
    const char ESC_KEY = 27;

    for(int i = 0;;++i)
    {
      Mat view;
      bool blinkOutput = false;

      view = s.nextImage();

      //-----  If no more image, or got enough, then stop calibration and show result -------------
      if( mode == CAPTURING && imagePoints.size() >= (unsigned)s.nrFrames )
      {
          if( runCalibrationAndSave(s, imageSize,  cameraMatrix, distCoeffs, imagePoints))
              mode = CALIBRATED;
          else
              mode = DETECTION;
      }
      if(view.empty())          // If no more images then run calibration, save and stop loop.
      {
            if( imagePoints.size() > 0 )
                runCalibrationAndSave(s, imageSize,  cameraMatrix, distCoeffs, imagePoints);
            break;
      }


        imageSize = view.size();  // Format input image.
        if( s.flipVertical )    flip( view, view, 0 );

        vector<Point2f> pointBuf;

        bool found;
        switch( s.calibrationPattern ) // Find feature points on the input format
        {
        case Settings::CHESSBOARD:
            found = findChessboardCorners( view, s.boardSize, pointBuf,
                CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
            break;
        case Settings::CIRCLES_GRID:
            found = findCirclesGrid( view, s.boardSize, pointBuf );
            break;
        case Settings::ASYMMETRIC_CIRCLES_GRID:
            found = findCirclesGrid( view, s.boardSize, pointBuf, CALIB_CB_ASYMMETRIC_GRID );
            break;
        default:
            found = false;
            break;
        }

        if (found)                // If done with success,
        {
              // improve the found corners' coordinate accuracy for chessboard
                if( s.calibrationPattern == Settings::CHESSBOARD)
                {
                    Mat viewGray;
                    cvtColor(view, viewGray, COLOR_BGR2GRAY);
                    cornerSubPix( viewGray, pointBuf, Size(11,11),
                        Size(-1,-1), TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
                }

                if( mode == CAPTURING &&  // For camera only take new samples after delay time
                    (!s.inputCapture.isOpened() || clock() - prevTimestamp > s.delay*1e-3*CLOCKS_PER_SEC) )
                {
                    imagePoints.push_back(pointBuf);
                    prevTimestamp = clock();
                    blinkOutput = s.inputCapture.isOpened();
                }

                // Draw the corners.
                drawChessboardCorners( view, s.boardSize, Mat(pointBuf), found );
        }

        //----------------------------- Output Text ------------------------------------------------
        string msg = (mode == CAPTURING) ? "100/100" :
                      mode == CALIBRATED ? "Calibrated" : "Press 'g' to start";
        int baseLine = 0;
        Size textSize = getTextSize(msg, 1, 1, 1, &baseLine);
        Point textOrigin(view.cols - 2*textSize.width - 10, view.rows - 2*baseLine - 10);

        if( mode == CAPTURING )
        {
            if(s.showUndistorsed)
                msg = format( "%d/%d Undist", (int)imagePoints.size(), s.nrFrames );
            else
                msg = format( "%d/%d", (int)imagePoints.size(), s.nrFrames );
        }

        putText( view, msg, textOrigin, 1, 1, mode == CALIBRATED ?  GREEN : RED);

        if( blinkOutput )
            bitwise_not(view, view);

        //------------------------- Video capture  output  undistorted ------------------------------
        if( mode == CALIBRATED && s.showUndistorsed )
        {
            Mat temp = view.clone();
            undistort(temp, view, cameraMatrix, distCoeffs);
        }

        //------------------------------ Show image and check for input commands -------------------
        imshow("Image View", view);
        char key = (char)waitKey(s.inputCapture.isOpened() ? 50 : s.delay);

        if( key  == ESC_KEY )
            break;

        if( key == 'u' && mode == CALIBRATED )
           s.showUndistorsed = !s.showUndistorsed;

        if( s.inputCapture.isOpened() && key == 'g' )
        {
            mode = CAPTURING;
            imagePoints.clear();
        }
    }

    // -----------------------Show and save the undistorted image for the image list ------------------------
    if( s.inputType == Settings::IMAGE_LIST && s.showUndistorsed )
    {
        Mat view, rview, map1, map2;
        initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
            getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),
            imageSize, CV_16SC2, map1, map2);

        for(int i = 0; i < (int)s.imageList.size(); i++ )
        {
            view = imread(s.imageList[i], 1);
            if(view.empty())
                continue;
            remap(view, rview, map1, map2, INTER_LINEAR);
            imshow("Image View", rview);

            string imageName = format( "undistorted_%d.jpg", i);
            imwrite(imageName,rview);

            char c = (char)waitKey();
            if( c  == ESC_KEY || c == 'q' || c == 'Q' )
                break;
        }
    }


    return 0;
}

static double computeReprojectionErrors( const vector<vector<Point3f> >& objectPoints,
                                         const vector<vector<Point2f> >& imagePoints,
                                         const vector<Mat>& rvecs, const vector<Mat>& tvecs,
                                         const Mat& cameraMatrix , const Mat& distCoeffs,
                                         vector<float>& perViewErrors)
{
    vector<Point2f> imagePoints2;
    int i, totalPoints = 0;
    double totalErr = 0, err;
    perViewErrors.resize(objectPoints.size());

    for( i = 0; i < (int)objectPoints.size(); ++i )
    {
        projectPoints( Mat(objectPoints[i]), rvecs[i], tvecs[i], cameraMatrix,
                       distCoeffs, imagePoints2);
        err = norm(Mat(imagePoints[i]), Mat(imagePoints2), CV_L2);

        int n = (int)objectPoints[i].size();
        perViewErrors[i] = (float) std::sqrt(err*err/n);
        totalErr        += err*err;
        totalPoints     += n;
    }

    return std::sqrt(totalErr/totalPoints);
}

static void calcBoardCornerPositions(Size boardSize, float squareSize, vector<Point3f>& corners,
                                     Settings::Pattern patternType /*= Settings::CHESSBOARD*/)
{
    corners.clear();

    switch(patternType)
    {
    case Settings::CHESSBOARD:
    case Settings::CIRCLES_GRID:
        for( int i = 0; i < boardSize.height; ++i )
            for( int j = 0; j < boardSize.width; ++j )
                corners.push_back(Point3f(float( j*squareSize ), float( i*squareSize ), 0));
        break;

    case Settings::ASYMMETRIC_CIRCLES_GRID:
        for( int i = 0; i < boardSize.height; i++ )
            for( int j = 0; j < boardSize.width; j++ )
                corners.push_back(Point3f(float((2*j + i % 2)*squareSize), float(i*squareSize), 0));
        break;
    default:
        break;
    }
}

static bool runCalibration( Settings& s, Size& imageSize, Mat& cameraMatrix, Mat& distCoeffs,
                            vector<vector<Point2f> > imagePoints, vector<Mat>& rvecs, vector<Mat>& tvecs,
                            vector<float>& reprojErrs,  double& totalAvgErr)
{

    cameraMatrix = Mat::eye(3, 3, CV_64F);
    if( s.flag & CV_CALIB_FIX_ASPECT_RATIO )
        cameraMatrix.at<double>(0,0) = 1.0;

    distCoeffs = Mat::zeros(8, 1, CV_64F);

    vector<vector<Point3f> > objectPoints(1);
    calcBoardCornerPositions(s.boardSize, s.squareSize, objectPoints[0], s.calibrationPattern);

    objectPoints.resize(imagePoints.size(),objectPoints[0]);

    //Find intrinsic and extrinsic camera parameters
    double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
                                 distCoeffs, rvecs, tvecs, s.flag|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);

    cout << "Re-projection error reported by calibrateCamera: "<< rms << endl;

    bool ok = checkRange(cameraMatrix) && checkRange(distCoeffs);

    totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints,
                                             rvecs, tvecs, cameraMatrix, distCoeffs, reprojErrs);

    return ok;
}

// Print camera parameters to the output file
static void saveCameraParams( Settings& s, Size& imageSize, Mat& cameraMatrix, Mat& distCoeffs,
                              const vector<Mat>& rvecs, const vector<Mat>& tvecs,
                              const vector<float>& reprojErrs, const vector<vector<Point2f> >& imagePoints,
                              double totalAvgErr )
{
    FileStorage fs( s.outputFileName, FileStorage::WRITE );

    time_t tm;
    time( &tm );
    struct tm *t2 = localtime( &tm );
    char buf[1024];
    strftime( buf, sizeof(buf)-1, "%c", t2 );

    fs << "calibration_Time" << buf;

    if( !rvecs.empty() || !reprojErrs.empty() )
        fs << "nrOfFrames" << (int)std::max(rvecs.size(), reprojErrs.size());
    fs << "image_Width" << imageSize.width;
    fs << "image_Height" << imageSize.height;
    fs << "board_Width" << s.boardSize.width;
    fs << "board_Height" << s.boardSize.height;
    fs << "square_Size" << s.squareSize;

    if( s.flag & CV_CALIB_FIX_ASPECT_RATIO )
        fs << "FixAspectRatio" << s.aspectRatio;

    if( s.flag )
    {
        sprintf( buf, "flags: %s%s%s%s",
            s.flag & CV_CALIB_USE_INTRINSIC_GUESS ? " +use_intrinsic_guess" : "",
            s.flag & CV_CALIB_FIX_ASPECT_RATIO ? " +fix_aspectRatio" : "",
            s.flag & CV_CALIB_FIX_PRINCIPAL_POINT ? " +fix_principal_point" : "",
            s.flag & CV_CALIB_ZERO_TANGENT_DIST ? " +zero_tangent_dist" : "" );
        cvWriteComment( *fs, buf, 0 );

    }

    fs << "flagValue" << s.flag;

    fs << "Camera_Matrix" << cameraMatrix;
    fs << "Distortion_Coefficients" << distCoeffs;

    fs << "Avg_Reprojection_Error" << totalAvgErr;
    if( !reprojErrs.empty() )
        fs << "Per_View_Reprojection_Errors" << Mat(reprojErrs);

    if( !rvecs.empty() && !tvecs.empty() )
    {
        CV_Assert(rvecs[0].type() == tvecs[0].type());
        Mat bigmat((int)rvecs.size(), 6, rvecs[0].type());
        for( int i = 0; i < (int)rvecs.size(); i++ )
        {
            Mat r = bigmat(Range(i, i+1), Range(0,3));
            Mat t = bigmat(Range(i, i+1), Range(3,6));

            CV_Assert(rvecs[i].rows == 3 && rvecs[i].cols == 1);
            CV_Assert(tvecs[i].rows == 3 && tvecs[i].cols == 1);
            //*.t() is MatExpr (not Mat) so we can use assignment operator
            r = rvecs[i].t();
            t = tvecs[i].t();
        }
        cvWriteComment( *fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0 );
        fs << "Extrinsic_Parameters" << bigmat;
    }

    if( !imagePoints.empty() )
    {
        Mat imagePtMat((int)imagePoints.size(), (int)imagePoints[0].size(), CV_32FC2);
        for( int i = 0; i < (int)imagePoints.size(); i++ )
        {
            Mat r = imagePtMat.row(i).reshape(2, imagePtMat.cols);
            Mat imgpti(imagePoints[i]);
            imgpti.copyTo(r);
        }
        fs << "Image_points" << imagePtMat;
    }
}

bool runCalibrationAndSave(Settings& s, Size imageSize, Mat&  cameraMatrix, Mat& distCoeffs,vector<vector<Point2f> > imagePoints )
{
    vector<Mat> rvecs, tvecs;
    vector<float> reprojErrs;
    double totalAvgErr = 0;

    bool ok = runCalibration(s,imageSize, cameraMatrix, distCoeffs, imagePoints, rvecs, tvecs,
                             reprojErrs, totalAvgErr);
    cout << (ok ? "Calibration succeeded" : "Calibration failed")
        << ". avg re projection error = "  << totalAvgErr ;

    if( ok )
        saveCameraParams( s, imageSize, cameraMatrix, distCoeffs, rvecs ,tvecs, reprojErrs,
                            imagePoints, totalAvgErr);
    return ok;
}

参考博客:https://blog.csdn.net/u013498583/article/details/71404323

 

 

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

史上最简单Opencv相机畸变矫正教学 的相关文章

  • ssh修改连接端口,以及修改端口之后连接不上的问题

    SSh服务配置文件路径一般都是在 etc ssh这个目录下面 sshd config 这个文件 使用VI vim编辑器 xff0c 打开sshd config这个文件 xff0c 搜索找到 port字段 去掉 xff0c 修改port 后面
  • FreeRTOS原理剖析:任务的创建

    1 任务创建API函数 任务的最基本功能是任务管理 xff0c 任务管理中最基本操作是任务的创建和删除 对于任务的创建和删除 xff0c 由于篇幅有点长 xff0c 分两篇分别讲解 在FreeRTOS中任务的创建函数如下 xff1a 函数描
  • @xmlAttribute等注解它的用处?

    用的是jdk自带的javax xml bind JAXBContext将对象和xml字符串进行相互转换 如果对要生成的 xml 格式有点些许的限制 xff0c 就会对生成xml的对象就需要进行些许控制 xff0c 控制对象的一个最可行的办法
  • C/C++ 分支预测(likely unlikely)

    看一些代码时 xff0c 会遇到likely unlikely 查了查网上的资料 xff0c 结合自己的理解记录一下 1 一些概念 指令周期 是指执行一条指令所需要的时间 xff0c 一般由若干个机器周期组成 xff0c 是从取指令 分析指
  • Vnc viewer与windows之间的复制粘贴

    用VNC连接到Linux之后 xff0c 最纠结的问题就是无法复制粘贴 其实很简单 xff0c 在Linux里面 xff0c 打开一个终端 xff0c 然后输入命令 xff1a vncconfig 之后 xff0c 会弹出一个窗口 不要关闭
  • Android studio 添加多语言支持

    环境 xff1a Android studio 3 2 执行步骤 xff1a 一 生成对应语言文件夹 选中你的工程 gt res gt 右键点击new gt 选中Android resource directory Available qu
  • VNC 远程环境搭建教程

    最近因项目需要使用到 VNC 远程工具 xff0c 因此记录使用过程 一 在 VNC 官网下载 VNC 服务端和客户端安装包 进入下载页面 二 注册 VNC 官网账号 三 在本地安装 VNC 客户端 xff0c 被远程电脑安装 VNC 服务
  • Ubuntu桌面出现Accept clipboard from viewers,Send clipboard to viewers,Send primary selection to vi等三行错误时

    如上图的错误时 1 输入以下神秘代码 sudo apt get install gnome core2 重启vnc服务3 若还不行 xff0c 则修改xstartup脚本 方法见下链接第五部分 修改xstartup
  • Python+ADB实现Android手机QQ自动点赞

    1 前言 前段时间看了些爬虫的知识 xff0c 然后又看到selenium xff0c Appium xff0c 在Appium环境设置过程中 xff0c 意外地看到这个帖子adb命令模拟按键事件 KeyCode xff0c 然后结合相关搜
  • Go语言汇编入门

    虽然在前面的文章中 xff0c 分析代码已经接触了一些Go语言的汇编代码的注解 xff0c 比如在slice和Go语言笔记以及以后的文章中都会使用到Go汇编 本章主要讲解Go汇编大致流程的框架 xff0c 对于刚接触Go汇编理解Go函数栈是
  • Holistic++ Scene Understanding论文翻译解析笔记

    Holistic 43 43 Scene Understanding 摘要 我们提出了一个新的3D整体场景理解问题 xff0c 它l共同解决了单视角图片的两个问题 xff08 1 xff09 整体场景的语义分析和重建 xff08 2 3D人
  • windows server 2012R2制作qcow2镜像

    一 环境准备 xff1a 1 windows server 2012R2的iso镜像 2 物理机一台 xff1a 要求支持硬件虚拟化 xff0c 并且装好了centos系统 xff0c 在windows上安装vmware 然后在vmware
  • VNC无法连接,出现“TOO MANY SECURITY FAILURES”(安全故障太多)

    通过VNC VIEWER远程管理 xff0c 连接的时候报错 too many security failures 这是因为VNC的黑名单机制 xff0c 用来保护你的服务器 如果有人暴力破解 xff0c 将会触发VNC的黑名单机制 处理方
  • java.sql.SQLException: #HY000

    勾选自动递增 将 type类型改成int xff0c binyint是boolean xff0c 类型是1 xff0c 2 xff0c 3 xff0c 4 xff0c 不是true false 就好了
  • jqgrid表格高度宽度设置

    jqgrid表格高度宽度设置 问题说明 gt 页面上使用上面搜索框 xff0c 下面是jqgrid表格形式 xff0c 总是出现 xff0c grid表格加载宽度 高度问题 本文通过主要解决表格高度宽度变化适应的问题 grid宽度 1 修改
  • 梯度下降法及matlab实现

    个人博客文章链接 xff1a http www huqj top article id 61 162 梯度下降法 xff08 gradient descent xff09 xff0c 是机器学习中最常用的参数调优算法 xff0c 所谓梯度下
  • 命令执行判断($?:命令回传值、&&、||)

    1 概述 当在linux中输入命令时 xff0c 命令可能成功也可能失败 xff0c 此时可以通过命令回传值来判断 xff08 符号 xff1a xff09 xff0c 命令回传值可以和 amp amp 与 一起使用 2 符号 amp am
  • LXC/KVM虚拟化基本概念

    1 LXC 其名称来自Linux软件容器 xff08 Linux Containers xff09 的缩写 一种操作系统层虚拟化 xff08 Operating system level virtualization xff09 技术 xf
  • 解决VNC连接安了Ubuntu MATE系统的树莓派3b时出现灰屏的问题

    1 xff09 首先安装vncserver服务 xff08 这一步有没有用我也不知道 xff0c 一般人都是装的tightvncserver 当然 xff0c 我也装了 xff09 sudo apt get install vnc4serv
  • Ubuntu18.04安装CUDA10、CUDNN

    上篇记录了Ubuntu下安装INVIDIA显卡驱动的方法 xff0c 尽管可以选择CUDA自带的驱动 xff0c 但为了避免不必要的问题 xff0c 尽量单独安装 如果没有单独安装驱动 xff0c 建议多找几篇博客 xff0c 对比来看 x

随机推荐

  • IDEA mavne项目转gradle项目

    文章目录 前言一 配置gradle二 将mavne项目转换为gradle1 找到项目根目录2 执行命令转换3 重启项目 配置IDEA 的 gradle4 转换后的样子 总结 前言 不知道小伙伴有没有遇到过这个问题 就是由于项目是用maven
  • 2016,再见 2017,还请多多指教

    先来一个象征意义上的序 今天是2017 01 01 新年的第一天 昨天适合总结 今天适合制作新年计划 昨天没做总结 于是今天总结和新年计划一起来吧 充满回忆的2016 昨天在驾校练车练了一天 倒库终于能倒进去了 回到住处已经下午5点 买了路
  • 3. 云计算的落地实践(下)

    本章讲解知识点 云计算如何落地实践 ISO镜像文件 创建虚拟机入门 创建数据节点 配置 VMWare创建虚拟机三种网络模式 1 云计算的落地实践 上一章我们讲了云计算的业界实践 即 搭建IaaS后 用于创建虚拟机 在虚拟机上部署PaaS 用
  • deepin15.8配置深度显卡驱动

    以deepin15 8为例 xff0c 电脑为联想的y7000 刚开始以网上下载 run文件的方式进行安装显卡驱动 xff0c 后来在下载cmake等一下工具的时候 xff0c 总会提示与显卡驱动某个模块版本冲突 xff0c 所以索性放弃了
  • 关于调用第三方接口时传递参数是File类型的解决方式

    最近在做一个项目 xff0c 需要频繁的调用第三方的接口 xff0c 本以为都是基本的数据类型 xff0c 没想到需要传一个文件类型的参数 xff0c 我想着调用接口的时候直接用文件流把文件写到connection不就行了 xff0c 这就
  • C#中?、?.、? ?、? ?=的用法和说明

    一 可空类型修饰符 xff1f 引用类型能用空引用来表示一个不存在的值 xff0c 但是值类型不能 例如 xff1a string str 61 null int i 61 null 编译报错 为了使值类型也能使用可空类型 xff0c 就可
  • TortorliseGit(小乌龟)创建删除(远程和本地)分支

    以下两篇文章分别为删除和创建 1 使用TortorliseGit 小乌龟 删除本地分支 xff0c 远程分支 2 使用TortoiseGit操作分支的创建与合并
  • UML类图的几种关系浅析

    类图中的主要关系有如下几种 关联关系 聚合关系 组合关系依赖关系泛化关系细化关系 注 xff1a 以下图片均来自网络 xff0c 侵删 1 关联关系 关联关系是类与类之间的连接 xff0c 表示一类对象与另一类对象之间有联系 xff0c 通
  • 关于c#创建界面的几种方式

    c 创建界面有很多种方式 xff0c 下面列举5中创建界面的方式 1 windows窗体 xff0c 这种窗体设计界面是最简单的一种 这种可以直接从工具箱拿出组件进行使用 xff0c 能够很好的设计界面 2 用户控件类 3 组件类 4 窗口
  • .ova文件转换成.ovf和.vmdk格式

    一 准备工具 xff1a 下载软件 xff1a OVFTool x64 下载地址 xff1a https pan baidu com s 1YDtHh0 OnK0Lm5C4KoF4 w 二 安装后 xff0c 去安装路径下 xff0c 按住
  • 【Word】如何在数学公式同一行末尾填写编号

    使用word插入公式框后 xff0c 在公式框中打完公式的末尾处 xff08 依旧在框内 xff09 加上 xff08 编号 xff09 xff0c 然后回车即可 xff01 xff01 超神器 xff01 再也不用手动空格啦 输入公式序号
  • 使用Xmanager软件远程调用图形化(可视化)安装Oracle数据库

    安装Oracle xff0c 使用调用图形化界面进行安装 xff0c 此次不能使用VNC远程到服务器本地进行图形化安装 xff0c 只能远程调用图形化进行本地安装 xff0c 方法如下 xff1a 一 Linux系统安装所需要的依赖组 xf
  • js中怎么删除对象的某个key值?js 遍历数组,有用!!

    参考 xff1a https blog csdn net denghaolinzy article details 87913561 formThead cate false id true out trade no true produc
  • UDP数据包的延迟及丢包检测(C++)

    摘要 本文记录通过数据报套接字来检测UDP数据包的延迟和丢包的思路和简单的代码实现 思路 UDP协议及用户数据报协议在传输层提供了无连接 不可靠的传输服务 xff0c 端到端的延迟以及丢包率是反应当前网络环境好坏的重要评价标准 Ping检测
  • 二叉树前中后序遍历非递归实现C++

    前几天面试过程中面试官让手写一下二叉树后序遍历的非递归写法 xff0c 当时没有写出来 xff0c 本想着可能是因为面试太紧张的原因 xff0c 才这么简单的题都没写出来 xff0c 后来特地去研究了一下 xff0c 发现二叉树的后序遍历非
  • Arcgis(AE)二次开发问题解决 创建组件”ToolbarControl”失败等

    本文提供 xff08 Arcgis二次开发 xff08 AE xff09 xff09 遇到的以下问题参考解决办法 xff1a 1 必须有许可证才能使用此ActiveX 控件 xff1b 2 命名空间 ESRI ArcGIS 中不存在类型或命
  • C++11右值引用和移动语义

    C 43 43 11中加入了很多新特性 xff0c 其中非常有用的一个就是右值引用和移动语义 xff0c 移动语义主要体现在移动构造函数和移动赋值函数
  • MacOS安装Minikube踩坑记录

    安装minikube macos可以通过两种方式安装minikube xff0c 第一种通过下载二进制文件的方式 xff0c 第二种通过brew直接安装 这里推荐直接通过brew安装 xff0c 更加简单方便 brew install mi
  • 线性回归原理及实现(一):最小二乘法

    线性回归到底要干什么 xff0c 顾名思义很简单 xff0c 即在已有数据集上通过构建一个线性的模型来拟合该数据集特征向量的各个分量之间的关系 xff0c 对于需要预测结果的新数据 xff0c 我们利用已经拟合好的线性模型来预测其结果 关于
  • 史上最简单Opencv相机畸变矫正教学

    最近因为项目需要研究了一下摄像头的畸变矫正 xff0c 我打算通过写这篇博客记录一下相关流程 其实关于摄像头畸变矫正的原理 xff0c 网络上已经有非常多的博客可以参考了 xff0c 我在博客里也就不再赘述了 利用Opencv库中的接口 x