Subversion Repositories OpenCV2-Cookbook

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 9 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
5
 
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,
8
   subject to the restriction that the copyright notice may not be removed
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.
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,
13
   and any consequent failure, is purely the responsibility of the user.
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
17
 
18
#include "CameraCalibrator.h"
19
 
20
// Open chessboard images and extract corner points
21
int CameraCalibrator::addChessboardPoints(
22
         const std::vector<std::string>& filelist,
23
         cv::Size & boardSize) {
24
 
25
        // the points on the chessboard
26
    std::vector<cv::Point2f> imageCorners;
27
    std::vector<cv::Point3f> objectCorners;
28
 
29
    // 3D Scene Points:
30
    // Initialize the chessboard corners 
31
    // in the chessboard reference frame
32
        // The corners are at 3D location (X,Y,Z)= (i,j,0)
33
        for (int i=0; i<boardSize.height; i++) {
34
                for (int j=0; j<boardSize.width; j++) {
35
 
36
                        objectCorners.push_back(cv::Point3f(i, j, 0.0f));
37
                }
38
    }
39
 
40
    // 2D Image points:
41
    cv::Mat image; // to contain chessboard image
42
    int successes = 0;
43
    // for all viewpoints
44
    for (int i=0; i<filelist.size(); i++) {
45
 
46
        // Open the image
47
        image = cv::imread(filelist[i],0);
48
 
49
        // Get the chessboard corners
50
        bool found = cv::findChessboardCorners(
51
                        image, boardSize, imageCorners);
52
 
53
        // Get subpixel accuracy on the corners
54
        cv::cornerSubPix(image, imageCorners,
55
                  cv::Size(5,5),
56
                  cv::Size(-1,-1),
57
                        cv::TermCriteria(cv::TermCriteria::MAX_ITER +
58
                          cv::TermCriteria::EPS,
59
             30,                // max number of iterations 
60
             0.1));     // min accuracy
61
 
62
          // If we have a good board, add it to our data
63
                  if (imageCorners.size() == boardSize.area()) {
64
 
65
                        // Add image and scene points from one view
66
            addPoints(imageCorners, objectCorners);
67
            successes++;
68
          }
69
 
70
        //Draw the corners
71
        cv::drawChessboardCorners(image, boardSize, imageCorners, found);
72
        cv::imshow("Corners on Chessboard", image);
73
        cv::waitKey(100);
74
    }
75
 
76
        return successes;
77
}
78
 
79
// Add scene points and corresponding image points
80
void CameraCalibrator::addPoints(const std::vector<cv::Point2f>& imageCorners, const std::vector<cv::Point3f>& objectCorners) {
81
 
82
        // 2D image points from one view
83
        imagePoints.push_back(imageCorners);          
84
        // corresponding 3D scene points
85
        objectPoints.push_back(objectCorners);
86
}
87
 
88
// Calibrate the camera
89
// returns the re-projection error
90
double CameraCalibrator::calibrate(cv::Size &imageSize)
91
{
92
        // undistorter must be reinitialized
93
        mustInitUndistort= true;
94
 
95
        //Output rotations and translations
96
    std::vector<cv::Mat> rvecs, tvecs;
97
 
98
        // start calibration
99
        return
100
     calibrateCamera(objectPoints, // the 3D points
101
                            imagePoints,  // the image points
102
                                        imageSize,    // image size
103
                                        cameraMatrix, // output camera matrix
104
                                        distCoeffs,   // output distortion matrix
105
                                        rvecs, tvecs, // Rs, Ts 
106
                                        flag);        // set options
107
//                                      ,CV_CALIB_USE_INTRINSIC_GUESS);
108
 
109
}
110
 
111
// remove distortion in an image (after calibration)
112
cv::Mat CameraCalibrator::remap(const cv::Mat &image) {
113
 
114
        cv::Mat undistorted;
115
 
116
        if (mustInitUndistort) { // called once per calibration
117
 
118
                cv::initUndistortRectifyMap(
119
                        cameraMatrix,  // computed camera matrix
120
            distCoeffs,    // computed distortion matrix
121
            cv::Mat(),     // optional rectification (none) 
122
                        cv::Mat(),     // camera matrix to generate undistorted
123
                        cv::Size(640,480),
124
//            image.size(),  // size of undistorted
125
            CV_32FC1,      // type of output map
126
            map1, map2);   // the x and y mapping functions
127
 
128
                mustInitUndistort= false;
129
        }
130
 
131
        // Apply mapping functions
132
    cv::remap(image, undistorted, map1, map2,
133
                cv::INTER_LINEAR); // interpolation type
134
 
135
        return undistorted;
136
}
137
 
138
 
139
// Set the calibration options
140
// 8radialCoeffEnabled should be true if 8 radial coefficients are required (5 is default)
141
// tangentialParamEnabled should be true if tangeantial distortion is present
142
void CameraCalibrator::setCalibrationFlag(bool radial8CoeffEnabled, bool tangentialParamEnabled) {
143
 
144
    // Set the flag used in cv::calibrateCamera()
145
    flag = 0;
146
    if (!tangentialParamEnabled) flag += CV_CALIB_ZERO_TANGENT_DIST;
147
        if (radial8CoeffEnabled) flag += CV_CALIB_RATIONAL_MODEL;
148
}
149