Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 5
Line 23... Line 23...
23
#include <opencv2/highgui/highgui.hpp>
23
#include <opencv2/highgui/highgui.hpp>
24
#include <opencv2/features2d/features2d.hpp>
24
#include <opencv2/features2d/features2d.hpp>
25
25
26
#include "CameraCalibrator.h"
26
#include "CameraCalibrator.h"
27
27
-
 
28
int
28
int main()
29
main()
29
{
30
{
30
31
31
        cv::namedWindow("Image");
32
  cv::namedWindow("Image");
32
        cv::Mat image;
33
  cv::Mat image;
33
        std::vector<std::string> filelist;
34
  std::vector<std::string> filelist;
34
35
35
        // generate list of chessboard image filename
36
  // generate list of chessboard image filename
36
        for (int i=1; i<=20; i++) {
37
  for (int i = 1; i <= 20; i++)
-
 
38
    {
37
39
38
                std::stringstream str;
40
      std::stringstream str;
39
                str << "../chessboards/chessboard" << std::setw(2) << std::setfill('0') << i << ".jpg";
41
      str << "../chessboards/chessboard" << std::setw(2) << std::setfill('0')
-
 
42
          << i << ".jpg";
40
                std::cout << str.str() << std::endl;
43
      std::cout << str.str() << std::endl;
41
44
42
                filelist.push_back(str.str());
45
      filelist.push_back(str.str());
43
                image= cv::imread(str.str(),0);
46
      image = cv::imread(str.str(), 0);
44
                cv::imshow("Image",image);
47
      cv::imshow("Image", image);
Line 48... Line 51...
48
51
49
        // Create calibrator object
52
  // Create calibrator object
50
    CameraCalibrator cameraCalibrator;
53
  CameraCalibrator cameraCalibrator;
51
        // add the corners from the chessboard
54
  // add the corners from the chessboard
52
        cv::Size boardSize(6,4);
55
  cv::Size boardSize(6, 4);
53
        cameraCalibrator.addChessboardPoints(
-
 
54
                filelist,       // filenames of chessboard image
56
  cameraCalibrator.addChessboardPoints(filelist, // filenames of chessboard image
55
                boardSize);     // size of chessboard
57
      boardSize); // size of chessboard
56
                // calibrate the camera
58
  // calibrate the camera
57
    //  cameraCalibrator.setCalibrationFlag(true,true);
59
  //    cameraCalibrator.setCalibrationFlag(true,true);
58
        cameraCalibrator.calibrate(image.size());
60
  cameraCalibrator.calibrate(image.size());
59
61
Line 61... Line 63...
61
    image = cv::imread(filelist[6]);
63
  image = cv::imread(filelist[6]);
62
        cv::Mat uImage= cameraCalibrator.remap(image);
64
  cv::Mat uImage = cameraCalibrator.remap(image);
63
65
64
        // display camera matrix
66
  // display camera matrix
65
        cv::Mat cameraMatrix= cameraCalibrator.getCameraMatrix();
67
  cv::Mat cameraMatrix = cameraCalibrator.getCameraMatrix();
66
        std::cout << " Camera intrinsic: " << cameraMatrix.rows << "x" << cameraMatrix.cols << std::endl;
68
  std::cout << " Camera intrinsic: " << cameraMatrix.rows << "x"
-
 
69
      << cameraMatrix.cols << std::endl;
-
 
70
  std::cout << cameraMatrix.at<double>(0, 0) << " "
67
        std::cout << cameraMatrix.at<double>(0,0) << " " << cameraMatrix.at<double>(0,1) << " " << cameraMatrix.at<double>(0,2) << std::endl;
71
      << cameraMatrix.at<double>(0, 1) << " " << cameraMatrix.at<double>(0, 2)
-
 
72
      << std::endl;
-
 
73
  std::cout << cameraMatrix.at<double>(1, 0) << " "
68
        std::cout << cameraMatrix.at<double>(1,0) << " " << cameraMatrix.at<double>(1,1) << " " << cameraMatrix.at<double>(1,2) << std::endl;
74
      << cameraMatrix.at<double>(1, 1) << " " << cameraMatrix.at<double>(1, 2)
-
 
75
      << std::endl;
-
 
76
  std::cout << cameraMatrix.at<double>(2, 0) << " "
69
        std::cout << cameraMatrix.at<double>(2,0) << " " << cameraMatrix.at<double>(2,1) << " " << cameraMatrix.at<double>(2,2) << std::endl;
77
      << cameraMatrix.at<double>(2, 1) << " " << cameraMatrix.at<double>(2, 2)
-
 
78
      << std::endl;
70
79
71
    imshow("Original Image", image);
80
  imshow("Original Image", image);
72
    imshow("Undistorted Image", uImage);
81
  imshow("Undistorted Image", uImage);
73
82
74
        cv::waitKey();
83
  cv::waitKey();