#include <opencv2\>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
/********** The image used in this program is the first image at the time of camera calibration in code listing 10-10 **********/
/*************** parameters are obtained during calibration *****************/
// Input the internal reference matrix and distortion matrix calculated in the previous section
Mat cameraMatrix = (Mat_<float>(3, 3) << 532.016297, 0, 332.172519,
0, 531.565159, 233.388075,
0, 0, 1);
Mat distCoeffs = (Mat_<float>(1, 5) << -0.285188, 0.080097, 0.001274,
-0.002415, 0.106579);
Relationship between the camera coordinate system and the world coordinate system for the first image calculated in //Code Listing 10-10
Mat rvec = (Mat_<float>(1, 3) << -1.977853, -2.002220, 0.130029);
Mat tvec = (Mat_<float>(1, 3) << -26.88155, -42.79936, 159.19703);
// Generate the 3D world coordinates of the interior corner points in the first image
Size boardSize = Size(9, 6);Number of corner points (rows, columns) in the //square calibration board (counted from 1)
Size squareSize = Size(10, 10);//The true size of each square of the checkerboard grid
vector<Point3f> PointSets;
for (int j = 0; j < boardSize.height; j++)
{
for (int k = 0; k < boardSize.width; k++)
{
Point3f realPoint;
// Assume that the calibration plate is in the z-plane of the world coordinate system, i.e., z=0. The world coordinate system is user-defined and is set to the upper left corner of the plate.
realPoint.x = j*squareSize.width;
realPoint.y = k*squareSize.height;
realPoint.z = 0;
PointSets.push_back(realPoint);
}
}
// Estimation of pixel coordinates of interior corner points from 3D coordinates and camera time in relation to the world coordinate system
vector<Point2f> imagePoints;
projectPoints(PointSets, rvec, tvec, cameraMatrix, distCoeffs, imagePoints);
// In order: the spatial point coordinates, the correspondence between the two coordinate systems, the camera's internal reference matrix, the distortion matrix, and the coordinates mapped to the image
for (int i = 0; i < imagePoints.size(); i++)
{
cout << "No." << to_string(i) << "Coordinates of a point." << imagePoints[i] << endl;
}
waitKey(0);
return 0;
}