Rev 3 | Details | Compare with Previous | 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: |
||
5 | PointedEar | 3 | Computer Vision Programming using the OpenCV Library. |
4 | by Robert Laganiere, Packt Publishing, 2011. |
||
3 | PointedEar | 5 | |
5 | PointedEar | 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. |
||
3 | PointedEar | 14 | |
5 | PointedEar | 15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name |
16 | \*------------------------------------------------------------------------------------------*/ |
||
3 | PointedEar | 17 | |
18 | #include <iostream> |
||
19 | #include <iomanip> |
||
20 | #include <vector> |
||
21 | #include <opencv2/core/core.hpp> |
||
22 | #include <opencv2/imgproc/imgproc.hpp> |
||
23 | #include <opencv2/highgui/highgui.hpp> |
||
24 | #include <opencv2/features2d/features2d.hpp> |
||
25 | |||
26 | #include "CameraCalibrator.h" |
||
27 | |||
5 | PointedEar | 28 | int |
29 | main() |
||
3 | PointedEar | 30 | { |
31 | |||
5 | PointedEar | 32 | cv::namedWindow("Image"); |
33 | cv::Mat image; |
||
34 | std::vector<std::string> filelist; |
||
3 | PointedEar | 35 | |
5 | PointedEar | 36 | // generate list of chessboard image filename |
37 | for (int i = 1; i <= 20; i++) |
||
38 | { |
||
3 | PointedEar | 39 | |
5 | PointedEar | 40 | std::stringstream str; |
41 | str << "../chessboards/chessboard" << std::setw(2) << std::setfill('0') |
||
42 | << i << ".jpg"; |
||
43 | std::cout << str.str() << std::endl; |
||
3 | PointedEar | 44 | |
5 | PointedEar | 45 | filelist.push_back(str.str()); |
46 | image = cv::imread(str.str(), 0); |
||
47 | cv::imshow("Image", image); |
||
3 | PointedEar | 48 | |
5 | PointedEar | 49 | cv::waitKey(100); |
50 | } |
||
3 | PointedEar | 51 | |
5 | PointedEar | 52 | // Create calibrator object |
53 | CameraCalibrator cameraCalibrator; |
||
54 | // add the corners from the chessboard |
||
55 | cv::Size boardSize(6, 4); |
||
56 | cameraCalibrator.addChessboardPoints(filelist, // filenames of chessboard image |
||
57 | boardSize); // size of chessboard |
||
58 | // calibrate the camera |
||
59 | // cameraCalibrator.setCalibrationFlag(true,true); |
||
60 | cameraCalibrator.calibrate(image.size()); |
||
3 | PointedEar | 61 | |
5 | PointedEar | 62 | // Image Undistortion |
63 | image = cv::imread(filelist[6]); |
||
64 | cv::Mat uImage = cameraCalibrator.remap(image); |
||
3 | PointedEar | 65 | |
5 | PointedEar | 66 | // display camera matrix |
67 | cv::Mat cameraMatrix = cameraCalibrator.getCameraMatrix(); |
||
68 | std::cout << " Camera intrinsic: " << cameraMatrix.rows << "x" |
||
69 | << cameraMatrix.cols << std::endl; |
||
70 | std::cout << cameraMatrix.at<double>(0, 0) << " " |
||
71 | << cameraMatrix.at<double>(0, 1) << " " << cameraMatrix.at<double>(0, 2) |
||
72 | << std::endl; |
||
73 | std::cout << cameraMatrix.at<double>(1, 0) << " " |
||
74 | << cameraMatrix.at<double>(1, 1) << " " << cameraMatrix.at<double>(1, 2) |
||
75 | << std::endl; |
||
76 | std::cout << cameraMatrix.at<double>(2, 0) << " " |
||
77 | << cameraMatrix.at<double>(2, 1) << " " << cameraMatrix.at<double>(2, 2) |
||
78 | << std::endl; |
||
3 | PointedEar | 79 | |
5 | PointedEar | 80 | imshow("Original Image", image); |
81 | imshow("Undistorted Image", uImage); |
||
82 | |||
83 | cv::waitKey(); |
||
84 | return 0; |
||
85 | } |