Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/CameraCalibration/cameraCalibrationWithUndistortion.cpp
3118 views
1
#include <opencv2/opencv.hpp>
2
#include <opencv2/calib3d/calib3d.hpp>
3
#include <opencv2/highgui/highgui.hpp>
4
#include <opencv2/imgproc/imgproc.hpp>
5
#include <stdio.h>
6
#include <iostream>
7
8
// Defining the dimensions of checkerboard
9
int CHECKERBOARD[2]{6,9};
10
11
int main()
12
{
13
// Creating vector to store vectors of 3D points for each checkerboard image
14
std::vector<std::vector<cv::Point3f> > objpoints;
15
16
// Creating vector to store vectors of 2D points for each checkerboard image
17
std::vector<std::vector<cv::Point2f> > imgpoints;
18
19
// Defining the world coordinates for 3D points
20
std::vector<cv::Point3f> objp;
21
for(int i{0}; i<CHECKERBOARD[1]; i++)
22
{
23
for(int j{0}; j<CHECKERBOARD[0]; j++)
24
objp.push_back(cv::Point3f(j,i,0));
25
}
26
27
28
// Extracting path of individual image stored in a given directory
29
std::vector<cv::String> images;
30
// Path of the folder containing checkerboard images
31
std::string path = "./images/*.jpg";
32
33
cv::glob(path, images);
34
35
cv::Mat frame, gray;
36
// vector to store the pixel coordinates of detected checker board corners
37
std::vector<cv::Point2f> corner_pts;
38
bool success;
39
40
// Looping over all the images in the directory
41
for(int i{0}; i<images.size(); i++)
42
{
43
frame = cv::imread(images[i]);
44
cv::cvtColor(frame,gray,cv::COLOR_BGR2GRAY);
45
46
// Finding checker board corners
47
// If desired number of corners are found in the image then success = true
48
success = cv::findChessboardCorners(gray,cv::Size(CHECKERBOARD[0],CHECKERBOARD[1]), corner_pts, cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FAST_CHECK | cv::CALIB_CB_NORMALIZE_IMAGE);
49
50
/*
51
* If desired number of corner are detected,
52
* we refine the pixel coordinates and display
53
* them on the images of checker board
54
*/
55
if(success)
56
{
57
cv::TermCriteria criteria(cv::TermCriteria::EPS | cv::TermCriteria::MAX_ITER, 30, 0.001);
58
59
// refining pixel coordinates for given 2d points.
60
cv::cornerSubPix(gray,corner_pts,cv::Size(11,11), cv::Size(-1,-1),criteria);
61
62
// Displaying the detected corner points on the checker board
63
cv::drawChessboardCorners(frame, cv::Size(CHECKERBOARD[0],CHECKERBOARD[1]), corner_pts,success);
64
65
objpoints.push_back(objp);
66
imgpoints.push_back(corner_pts);
67
}
68
69
cv::imshow("Image",frame);
70
cv::waitKey(0);
71
}
72
73
cv::destroyAllWindows();
74
75
cv::Mat cameraMatrix,distCoeffs,R,T;
76
77
/*
78
* Performing camera calibration by
79
* passing the value of known 3D points (objpoints)
80
* and corresponding pixel coordinates of the
81
* detected corners (imgpoints)
82
*/
83
cv::calibrateCamera(objpoints, imgpoints,cv::Size(gray.rows,gray.cols),cameraMatrix,distCoeffs,R,T);
84
85
std::cout << "cameraMatrix : " << cameraMatrix << std::endl;
86
std::cout << "distCoeffs : " << distCoeffs << std::endl;
87
std::cout << "Rotation vector : " << R << std::endl;
88
std::cout << "Translation vector : " << T << std::endl;
89
90
91
// Trying to undistort the image using the camera parameters obtained from calibration
92
93
cv::Mat image;
94
image = cv::imread(images[0]);
95
cv::Mat dst, map1, map2,new_camera_matrix;
96
cv::Size imageSize(cv::Size(image.cols,image.rows));
97
98
// Refining the camera matrix using parameters obtained by calibration
99
new_camera_matrix = cv::getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0);
100
101
// Method 1 to undistort the image
102
cv::undistort( frame, dst, new_camera_matrix, distCoeffs, new_camera_matrix );
103
104
// Method 2 to undistort the image
105
cv::initUndistortRectifyMap(cameraMatrix, distCoeffs, cv::Mat(),cv::getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),imageSize, CV_16SC2, map1, map2);
106
107
cv::remap(frame, dst, map1, map2, cv::INTER_LINEAR);
108
109
//Displaying the undistorted image
110
cv::imshow("undistorted image",dst);
111
cv::waitKey(0);
112
113
return 0;
114
}
115
116