精华内容
下载资源
问答
  • opencv3.1 相机矫正

    2018-06-02 16:41:29
    opencv单目相机的图像矫正opencv单目相机的图像矫正
  • opencv 相机标定与矫正

    2017-08-07 14:25:37
    opencv

    由摄像机拍取并进行标定与矫正

    步骤:
    1.确定基础设置

        //设置1 图像的尺寸
        const int nImageW = 2592;
        const int nImageH = 1944;
    
        //设置2 棋盘X方向上角点数量
        const int nChessBoardWidthCount = 7;
    
        //设置3 棋盘Y方向上角点数量
        const int nChessBoardHeightCount = 5;
    
        CvSize szChessBoardSize = cvSize(nChessBoardWidthCount, nChessBoardHeightCount);
        const int nOneChessBoardCornerCount = nChessBoardWidthCount *nChessBoardHeightCount;
    
        //设置4 读取的图像名称
        char chFileName[] = "01.bmp";
    
        //设置5 图像数量
        const int nImageCount = 23;
    
        //设置6 棋盘格子的边长
        float fSquareSize = 25.6;

    2.获取多角度的照片,进行棋盘格角点检测
    运用cvFindChessboardCorners()以及cvFindCornerSubPix()检测角点
    运用cvDrawChessboardCorners()绘制角点

    3.将图像二维坐标转换成世界坐标
    cvProjectPoints2()计算三维点投影到二维图像平面上的坐标
    cvNorm()计算每幅图片的误差

    4.使用cvCalibrateCamera2()获取

    Mat intrinsic;                                          //相机内参数  
    Mat distortion_coeff;                                   //相机畸变参数  
    vector<mat> rvecs;                                        //旋转向量  
    vector<mat> tvecs;                                        //平移向量  

    5.使用cvUndistort2()矫正畸变

    展开全文
  • 史上最简单Opencv相机畸变矫正教学

    万次阅读 2018-10-12 11:38:49
    利用Opencv库中的接口,可以很方便地对一款固定型号的摄像头进行矫正,一般地我们将这个过程分成两步:生成参数文件和矫正。 生成参数文件 这里使用的是OpenCV的例程(非常方便非常好用~),例程可以在你的opencv...

    最近因为项目需要研究了一下摄像头的畸变矫正,我打算通过写这篇博客记录一下相关流程。其实关于摄像头畸变矫正的原理,网络上已经有非常多的博客可以参考了,我在博客里也就不再赘述了。利用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

     

     

     

     

    展开全文
  • opencv自带相机矫正算法,自动找角点抓拍后矫正,并显示原始图和矫正后效果图,我用的棋盘格是7*6,块数是42,可以根据自己的情况修改
  • 径向畸变矫正: 以图像中心点为圆心,相同半径的点补充量相同,不同半径的点补偿量服从二次函数。 二、相机成像过程 世界坐标系 —> 相机坐标系 点的转换:求解外参 [ 旋转、平移 ] 相机坐标系 —> 图像...


    一、相机畸变

    畸变: 指在世界坐标系中的直线转换到其他坐标系不再是直线,从而导致失真。

    1. 径向畸变:(枕形、桶形)相机的光学镜头厚度不均匀,光线在远离透镜中心的地方比靠近中心的地方更加弯曲。

    2. 切向畸变: 透镜不完全平行于图像平面,即 sensor 装配时与镜头间的角度不准。

    径向畸变矫正: 以图像中心点为圆心,相同半径的点补充量相同,不同半径的点补偿量服从二次函数。
    在这里插入图片描述

    二、相机成像过程

    1. 世界坐标系 —> 相机坐标系
      点的转换:求解外参 [ 旋转、平移 ]

    2. 相机坐标系 —> 图像物理坐标系
      投影到成像平面:求解内参 [ 相机矩阵、畸变系数 ]

    3. 图像物理坐标系 —> 图像像素坐标系
      将平面上的数据转换到图像平面:求解像素转换矩阵

    相机内参:

    1. 相机矩阵:焦距、光学中心
    2. 畸变系数:畸变模型的 5 个参数 D = { k 1 k_1 k1, k 2 k_2 k2, k 3 k_3 k3, p 1 p_1 p1, p 2 p_2 p2}

    相机外参:

    1. 通过旋转和平移将实际场景 3D 映射到相机的 2D 坐标过程中的旋转和平移就是外参;
    2. 描述的是 世界坐标 转换到 相机坐标 的过程。

    三、相机标定

    1. 相机标定的目的

    获得相机的内参和外参矩阵(同时也会得到每一幅标定图像的旋转和平移矩阵),内参和外参系数可以对之后相机拍摄的图像进行矫正,得到畸变很小的图像。

    2. 相机标定的输入

    标定图像上所有内角点的图像坐标,标定板图像上所有内角点的空间三维坐标(默认 Z = 0 Z=0 Z=0 平面上)

    3. 相机标定的输出

    内参矩阵、外参矩阵、畸变系数

    四、棋盘格标定实验

    1. 实验步骤

    (1) 打印一张棋盘格 A4 纸(黑白间距已知),并贴在一个平板上;
    (2) 针对棋盘格拍摄若干张图片(10 ~ 20张);
    (3) 在图片中检测角点;
    (4) 根据角点位置信息和图像中的坐标,求解内参矩阵;
    (5) 利用解析解估计方法计算出 5 个内参,以及 6 个外参;
    (6) 根据极大似然估计策略,设计优化目标并实现参数的 refinement。

    2. 数据集采集

    • 打印棋盘格并贴在一个平板上,针对棋盘格拍摄若干张图片。
    • 棋盘格的规格为 9(行) * 7(列),每一方格大小为 28mm。

    3. 实验代码

    思路:

    1. 完成标定板图像的采集(至少3张)
    2. 利用 findChessboardCorners() 函数检测标定板角点,并利用 cornerSubPix() 函数完成亚像素级校准
    3. 利用 calibrateCamera() 函数进行相机标定,得到内参矩阵和畸变系数
    # coding=utf-8
    """
    张正友棋盘标定法:相机畸变矫正
    @author: libo-coder
    """
    import cv2
    import numpy as np
    import glob
    import os
    import yaml
    
    def get_K_and_D(CheckerboardSize, Nx_cor, Ny_cor, imgPath='./', saveFile=True, saveImages=True):
        """
        单目(普通+广角/鱼眼)摄像头标定
        :param CheckerboardSize: 标定的棋盘格尺寸,必须为整数.(单位:mm或0.1mm)
        :param Nx_cor: 棋盘格横向内角数
        :param Ny_cor: 棋盘格纵向内角数
        :param saveFile: 是否保存标定结果,默认不保存.
        :param saveImages: 是否保存图片,默认不保存.
        :return mtx: 内参数矩阵.{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}
        :return dist: 畸变系数.(k_1,k_2,p_1,p_2,k_3)
        """
        # 找棋盘格角点(角点精准化迭代过程的终止条件)
        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, CheckerboardSize, 1e-6)  # 循环中断 (3,27,1e-6)
        flags = cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE  # 11
        flags_fisheye = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC + cv2.fisheye.CALIB_CHECK_COND + cv2.fisheye.CALIB_FIX_SKEW  # 14
    
        # 获取标定板角点的位置,世界坐标系中的棋盘格点,例如(0,0,0), (1,0,0), (2,0,0) ....,(8,5,0)
        objp = np.zeros((1, Nx_cor * Ny_cor, 3), np.float32)
        objp[0, :, :2] = np.mgrid[0:Nx_cor, 0:Ny_cor].T.reshape(-1, 2)
    
        # 储存棋盘格角点的世界坐标和图像坐标对
        objpoints = []  # 在世界坐标系中的三维点
        imgpoints = []  # 在图像平面的二维点
    
        count = 0  # 用来标志成功检测到的棋盘格画面数量
        _img_shape = None
        images = glob.glob(imgPath + '/*.jpg')
        for img_path in images:
            img = cv2.imread(img_path)
            if not _img_shape:
                _img_shape = img.shape[:2]
            else:
                assert _img_shape == img.shape[:2], "All images must share the same size."
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # 寻找棋盘格模板的角点
            ret, corners = cv2.findChessboardCorners(gray, (Nx_cor, Ny_cor), flags)
            if ret:     # 如果找到,添加目标点,图像点
                objpoints.append(objp)
                # cv2.cornerSubPix(gray, corners, (5, 5), (-1, -1), criteria)  # 获取更精确的角点位置
                cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)  # 获取更精确的角点位置
                imgpoints.append(corners)
    
                # 将角点在图像上显示
                cv2.drawChessboardCorners(img, (Nx_cor, Ny_cor), corners, ret)
                count += 1
                if saveImages:
                    cv2.imwrite(dataroot + 'result/' + str(count) + '.jpg', img)
                print('NO.' + str(count))
    
        global mtx, dist
        # 标定. rvec 和 tvec 是在获取了相机内参 mtx,dist 之后通过内部调用 solvePnPRansac() 函数获得的
        # ret为标定结果,mtx为内参数矩阵,dist为畸变系数,rvecs为旋转矩阵,tvecs为位移向量
        ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
            objpoints, imgpoints, gray.shape[:2][::-1], None, criteria
        )
        print('mtx = np.array( ' + str(mtx.tolist()) + " )")  # 摄像头内参 mtx = [[f_x,0,c_x][0,f_y,c_y][0,0,1]]
        print('dist = np.array( ' + str(dist.tolist()) + " )")  # 畸变系数dist = (k1,k2,p1,p2,k3)
    
        # 鱼眼/大广角镜头的单目标定
        K = np.zeros((3, 3))
        D = np.zeros((4, 1))
        RR = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(len(objpoints))]
        TT = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(len(objpoints))]
        rms, _, _, _, _ = cv2.fisheye.calibrate(
            objpoints, imgpoints, gray.shape[:2][::-1], K, D, RR, TT, flags_fisheye, criteria
        )
    
        print("K = np.array( " + str(K.tolist()) + " )")  # 摄像头内参,此结果与mtx相比更为稳定和精确
        print("D = np.array( " + str(D.tolist()) + " )")  # 畸变系数D = (k1,k2,k3,k4)
    
        # 计算反投影误差,反应找到的参数的精确程度
        mean_error = 0
        for i in range(len(objpoints)):
            imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
            error = cv2.norm(imgpoints[i], imgpoints2, cv2.NORM_L2) / len(imgpoints2)
            mean_error += error
        print("total error: ", mean_error / len(objpoints))
    
        if saveFile:
            # np.savez("./debug/calibrate.npz", mtx=mtx, dist=dist, K=K, D=D)
            fs = cv2.FileStorage('parameters.yml', cv2.FileStorage_WRITE)
            fs.write('K', K)
            fs.write('D', D)
            fs.release()
    
        return mtx, dist, K, D
    
    
    dataroot = '/root/share175/Boris/dataset/undistort_data/206/'
    
    if __name__ == '__main__':
        tmeplatePath = dataroot + 'template2'
        mtx, dist, K, D = get_K_and_D(10, 11, 8, imgPath=tmeplatePath)
        print('K = ', K)
        print('D = ', D)
    
        ################ 单张图片进行矫正测试 ###############
        # # 利用已获得的内参进行畸变矫正
        # img = cv2.imread('./test/test02.jpg')
        # height, width = img.shape[:2]
        #
        # # 优化内参和畸变系数
        # p = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(K, D, (width, height), None)
        #
        # # initUndistortRectifyMap 用来计算畸变映射, mapx2、mapy2分别代表 X 坐标和 Y 坐标的映射
        # mapx2, mapy2 = cv2.fisheye.initUndistortRectifyMap(K, D, None, p, (width, height), cv2.CV_32F)
        #
        # # remap 用来把求得的映射应用到图像上
        # img_rectified = cv2.remap(img,    # 畸变的原始图像
        #                           mapx2, mapy2,   # X 坐标和 Y 坐标的映射
        #                           interpolation=cv2.INTER_LINEAR,     # 图像的插值方式
        #                           borderMode=cv2.BORDER_CONSTANT)     # 边界的填充方式
        #
        # cv2.imwrite('./debug/test/img_rectified.jpg', img_rectified)
        ######################################################
    
        ################### 批量进行矫正测试 #################
        datadir = dataroot + 'test'
        path = os.path.join(datadir)
        img_list = os.listdir(path)
        # print(img_list)
        for i, name in enumerate(img_list):
            # bfn, ext = os.path.splitext(name)
            # print(bfn)
            img = cv2.imread(os.path.join(path, name))
            # cv2.imwrite('./img.jpg', img)
            height, width = img.shape[:2]
            p = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(K, D, (width, height), None)
            mapx2, mapy2 = cv2.fisheye.initUndistortRectifyMap(K, D, None, p, (width, height), cv2.CV_32F)
            img_rectified = cv2.remap(img,  # 畸变的原始图像
                                      mapx2, mapy2,   # X 坐标和 Y 坐标的映射
                                      interpolation=cv2.INTER_LINEAR,     # 图像的插值方式
                                      borderMode=cv2.BORDER_CONSTANT)     # 边界的填充方式
            cv2.imwrite(dataroot + 'output/' + name, img_rectified)
        ######################################################
    

    4. 实验结果

    内部参数计算结果:
    在这里插入图片描述
    外部参数计算结果:

    五、实验小结

    注意点: 实验时需要关注自己打印出来的棋盘格行列数以及每个小方格的边长,代码中与之相关的参数要注意保持一致。

    参考链接

    1. https://www.codenong.com/cs105362491/
    2. https://www.cnblogs.com/wildbloom/p/8320351.html
    3. https://zhuanlan.zhihu.com/p/94244568
    展开全文
  • 本文分为两部分,一部分是介绍鱼眼相机畸变校正的原理,一部分是手撕OpenCV相机矫正代码。 文章主要结构如下图所示: 介绍鱼眼相机的原理 什么是鱼眼相机 相机内参标定 相机内外参标定 畸变矫正 径向畸变 枕型畸变 ...

    首先展示一下实现的效果:
    校正前:
    在这里插入图片描述
    校正后:
    在这里插入图片描述

    本文分为两部分,一部分是介绍鱼眼相机畸变校正的原理,一部分是手撕OpenCV相机矫正代码。
    文章主要结构如下图所示:

    在这里插入图片描述

    一、介绍鱼眼相机的原理

    1、什么是鱼眼镜头

    鱼眼镜头是具有超广视角

    展开全文
  • opencv 相机标定获取去畸变图像 private void calcBoardCornerPositions(Mat corners,int mCornersSize, Size mPatternSize,double mSquareSize) { final int cn = 3; float positions[] = new float...
  • 本文主要记录相机标定的代码实现,关于相机标定的原理可以...本文相机标定的照片采用OpenCV提供的图片,位置:...\opencv\opencv\sources\samples\data中left01~left14.jpg 相机标定在OpenCV中实现的主要步骤为: ...
  • 文章目录相机矫正与显示说明Code运行效果参考 相机矫正与显示 1、注意事项; 2、Code; 3、效果; 4、参考; 说明 1、相机标定的棋盘格习惯width方向棋盘少点,height方向棋盘多点,棋盘格角点识别只寻找内角点(都...
  • opencv2\opencv.hpp> using namespace cv; void correct_photo(const char * jpg) { Mat src = imread(jpg); Mat distortion = src.clone(); Mat camera_matrix = Mat(3, 3, CV_32FC1); Mat ...
  • 搞了个摄像头,拍摄有畸变,拍摄出的直线是弧线形状,类似于鱼眼相机,需要正常输出,遂使用OpenCV进行相机标定 1、畸变参数的提取 大哥用MATLAB提取的畸变内参和畸变系数,用来矫正,我不会,此博客主要说参数的...
  • OpenCV相机畸变矫正

    2020-09-24 15:36:26
    opencv 4.4 vs2017 代码是借用人家的,具体哪篇也忘了~ 程序运行后按Y键持续检测角点直到输出 角点找不到的原因分析 这里设置标定板的角点数错误,行列应该设置为你标定板最大格数-1,我的标定板是7行10列这里就...
  • 使用OpenCV自带的标定标定程序进行标定,图像矫正结果往往会变形严重,使用Matlab工具箱进行双目相机标定,用OpenCV生成矫正矩形,这样矫正后的图像比直接使用OpenCV要好很多
  • opencv 相机标定代码

    2016-07-20 14:26:52
    opencv进行相机标定,并且进行畸变矫正,每一步代码都有详细的注释,且有标定图片,可以直接运行
  • opencv相机标定

    千次阅读 2017-01-20 17:18:12
    opencv相机标定
  • 目录一、鱼眼矫正原理讲解1. 像素坐标转化为相机坐标2. 无畸变相机坐标 与 畸变后相机坐标 的 对应关系 根据前面两篇文章,我们已经知道鱼眼矫正最重要的函数是fisheye::initUndistortRectifyMap(),它能得到map1...
  • opencv相机标定与畸变矫正介绍;以及自动驾驶学习资料 涵盖感知,规划和控制,ADAS,传感器; 1. apollo相关的技术教程和文档; 2.adas(高级辅助驾驶)算法设计(例如AEB,ACC,LKA等) 3.自动驾驶鼻祖mobileye的论文...
  • OpenCV相机标定

    2019-05-07 14:28:34
    OpenCV相机标定 1.关键函数介绍 棋盘格角点检测函数 cv::findChessboardCorners() bool findChessboardCorners( InputArray image, Size patternSize, ...
  • 利用MatLab+OpenCV进行相机畸变矫正

    千次阅读 2019-11-26 18:31:20
    关于矫正的数学原理这里不再赘述,可以参考openCV官方文档和https://github.com/Nocami/PythonComputerVision-6-CameraCalibration 1.准备数据 step1:去openCV下载pattern.jpg 显示在屏幕上即可。 step2:用...
  • OpenCV进行鱼眼相机矫正的代码很多,大家可以上网搜索,也可以下载博主的代码。 但是,不管参考哪家程序,用到的主要API总是这三个: - fisheye::calibrate(); //标定相机,获取内参、外参、畸变系数 - fisheye::...
  • OpenCV相机标定程序

    热门讨论 2013-04-30 17:38:10
    本程序是基于VC6和OpenCV1.0的摄像机标定程序,并采用MFC做界面显示,可以计算出摄像机的内参数和畸变系数。非常的好用。适合初学者。
  • 采用vs2010+opencv2.4.9
  • 通过要标定相机拍摄的不同方位的棋盘图,获取相机的内参矩阵,畸变系数,以及每幅图的相应旋转,平移矩阵 函数定义: def calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs=...
  • 主要介绍了Python opencv相机标定实现原理及步骤详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,280
精华内容 912
关键字:

opencv相机矫正