“78d7c0e0d81c40e0d9541e4ef0dd15df30d457e3”上不存在“app/views/projects/git@gitcode.net:lshemail/gitlab-foss.git”
camera_calibration_square_chess.rst 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
.. _CameraCalibrationSquareChessBoardTutorial:

Camera calibration with square chessboard
*****************************************

.. highlight:: cpp

The goal of this tutorial is to learn how to calibrate a camera given a set of chessboard images.

10
*Test data*: use images in your data/chess folder.
11 12

#.
13
    Compile opencv with samples by setting ``BUILD_EXAMPLES`` to ``ON`` in cmake configuration.
14 15 16

#.
    Go to ``bin`` folder and use ``imagelist_creator`` to create an ``XML/YAML`` list of your images.
17

18
#.
19
    Then, run ``calibration`` sample to get camera parameters. Use square size equal to 3cm.
20 21 22 23 24 25 26 27 28 29 30

Pose estimation
===============

Now, let us write a code that detects a chessboard in a new image and finds its distance from the camera. You can apply the same method to any object with known 3D geometry that you can detect in an image.

*Test data*: use chess_test*.jpg images from your data folder.

#.
    Create an empty console project. Load a test image: ::

31
        Mat img = imread(argv[1], IMREAD_GRAYSCALE);
32 33 34 35

#.
    Detect a chessboard in this image using findChessboard function. ::

36
        bool found = findChessboardCorners( img, boardSize, ptvec, CALIB_CB_ADAPTIVE_THRESH );
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

#.
    Now, write a function that generates a ``vector<Point3f>`` array of 3d coordinates of a chessboard in any coordinate system. For simplicity, let us choose a system such that one of the chessboard corners is in the origin and the board is in the plane *z = 0*.

#.
    Read camera parameters from XML/YAML file: ::

        FileStorage fs(filename, FileStorage::READ);
        Mat intrinsics, distortion;
        fs["camera_matrix"] >> intrinsics;
        fs["distortion_coefficients"] >> distortion;

#.
    Now we are ready to find chessboard pose by running ``solvePnP``: ::

        vector<Point3f> boardPoints;
        // fill the array
        ...

        solvePnP(Mat(boardPoints), Mat(foundBoardCorners), cameraMatrix,
                             distCoeffs, rvec, tvec, false);

#.
60
    Calculate reprojection error like it is done in ``calibration`` sample (see ``opencv/samples/cpp/calibration.cpp``, function ``computeReprojectionErrors``).
61

R
Roman Donchenko 已提交
62
Question: how to calculate the distance from the camera origin to any of the corners?