Calibration example (Patch #802)


Added by David Doria about 14 years ago. Updated almost 11 years ago.


Status:Open Start date:
Priority:Low Due date:
Assignee:- % Done:

0%

Category:samples
Target version:-
Affected version:branch '2.4' (2.4-dev) Operating System:Any
Difficulty:Easy HW Platform:Any
Pull request:

Description

The included samples/cpp/calibration.cpp is very large as it covers many different input options. It may be nice to include a very simple "one camera, one chessboard" example like the following:

 1#include "opencv2/core/core.hpp" 
 2#include "opencv2/imgproc/imgproc.hpp" 
 3#include "opencv2/calib3d/calib3d.hpp" 
 4#include "opencv2/highgui/highgui.hpp" 
 5
 6#include <iostream>
 7#include <string>
 8
 9using namespace cv;
10
11std::vector<Point3f> Create3DChessboardCorners(Size boardSize, float squareSize);
12
13int main( int argc, char** argv )
14{
15    Size boardSize(7,7); // the number of "inside corners" (where a black square meets a white square). This board is actually 8x8 squares
16
17    float squareSize = 1.f; // This is "1 arbitrary unit" 
18
19    //std::string imageFileName = argvr1;
20    std::string imageFileName = "board.jpg";
21
22    Mat view = imread(imageFileName, 1);
23
24    namedWindow( "Image View", 1 );
25
26    Size imageSize = view.size();
27
28    // Find the chessboard corners
29    vector<vector<Point2f> > imagePoints(1);
30    bool found = findChessboardCorners(view, boardSize, imagePointsr0);
31    if(!found)
32      {
33      std::cerr << "Could not find chess board!" << std::endl;
34      exit(-1);
35      }
36
37    drawChessboardCorners( view, boardSize, Mat(imagePointsr0), found );
38
39    std::vector<std::vector<Point3f> > objectPoints(1);
40    objectPointsr0 = Create3DChessboardCorners(boardSize, squareSize);
41
42    std::vector<Mat> rotationVectors;
43    std::vector<Mat> translationVectors;
44
45    Mat distortionCoefficients = Mat::zeros(8, 1, CV_64F); // There are 8 distortion coefficients
46    Mat cameraMatrix = Mat::eye(3, 3, CV_64F);
47
48    int flags = 0;
49    double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
50                    distortionCoefficients, rotationVectors, translationVectors, flags|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
51
52    std::cout << "RMS: " << rms << std::endl;
53
54    std::cout << "Ccamera matrix: " << cameraMatrix << std::endl;
55    std::cout << "Distortion _coefficients: " << distortionCoefficients << std::endl;
56
57    imshow("Image View", view);
58    waitKey(0);
59
60    return 0;
61}
62
63std::vector<Point3f> Create3DChessboardCorners(Size boardSize, float squareSize)
64{
65  // This function creates the 3D points of your chessboard in its own coordinate system
66
67  std::vector<Point3f> corners;
68
69  for( int i = 0; i < boardSize.height; i++ )
70  {
71    for( int j = 0; j < boardSize.width; j++ )
72    {
73      corners.push_back(Point3f(float(j*squareSize),
74                                float(i*squareSize), 0));
75    }
76  }
77
78  return corners;
79}


Associated revisions

Revision abe2ea59
Added by Andrey Kamaev almost 12 years ago

Merge pull request #802 from SpecLad:shebang

History

Updated by Alexander Shishkov almost 13 years ago

  • Tracker changed from Feature to Patch
  • Target version deleted ()
  • Description changed from The included samples/cpp/calibration.cpp is very large as it covers many diff... to The included samples/cpp/calibration.cpp is very large as it covers many diff... More

Updated by Alexander Shishkov almost 13 years ago

  • Priority changed from Normal to Low
  • Target version deleted ()

Updated by Alexander Smorkalov almost 11 years ago

  • HW Platform set to Any
  • Operating System set to Any
  • Difficulty set to Easy
  • Affected version set to branch '2.4' (2.4-dev)

Also available in: Atom PDF