Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 5
1
/*------------------------------------------------------------------------------------------*\
1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 9 of the cookbook:  
2
   This file contains material supporting chapter 9 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
3
 Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
4
 by Robert Laganiere, Packt Publishing, 2011.
5

5

6
   This program is free software; permission is hereby granted to use, copy, modify,
6
 This program is free software; permission is hereby granted to use, copy, modify,
7
   and distribute this source code, or portions thereof, for any purpose, without fee,
7
 and distribute this source code, or portions thereof, for any purpose, without fee,
8
   subject to the restriction that the copyright notice may not be removed
8
 subject to the restriction that the copyright notice may not be removed
9
   or altered from any source or altered source distribution.
9
 or altered from any source or altered source distribution.
10
   The software is released on an as-is basis and without any warranties of any kind.
10
 The software is released on an as-is basis and without any warranties of any kind.
11
   In particular, the software is not guaranteed to be fault-tolerant or free from failure.
11
 In particular, the software is not guaranteed to be fault-tolerant or free from failure.
12
   The author disclaims all warranties with regard to this software, any use,
12
 The author disclaims all warranties with regard to this software, any use,
13
   and any consequent failure, is purely the responsibility of the user.
13
 and any consequent failure, is purely the responsibility of the user.
14
 
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
15
 Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
16
 \*------------------------------------------------------------------------------------------*/
17
17
18
#include <iostream>
18
#include <iostream>
19
#include <iomanip>
19
#include <iomanip>
20
#include <vector>
20
#include <vector>
21
#include <opencv2/core/core.hpp>
21
#include <opencv2/core/core.hpp>
22
#include <opencv2/imgproc/imgproc.hpp>
22
#include <opencv2/imgproc/imgproc.hpp>
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);
45
       
48
46
                 cv::waitKey(100);
49
      cv::waitKey(100);
47
        }
50
    }
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
60
    // Image Undistortion
62
  // Image Undistortion
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();
75
        return 0;
84
  return 0;
76
}
85
}
-
 
86