web123456

OpenCV (58) Single-eye camera calibration Zhang Zhengyou camera calibration

#include <opencv2\> #include <fstream> #include <iostream> #include <vector> using namespace std; using namespace cv; int main() { //Read all images vector<Mat> imgs; string imageName; ifstream fin("");// Text stream reads txt text while (getline(fin, imageName))//Read each line of data in the text, that is, the name of each image, and store the imageName as a string; { Mat img = imread(imageName); imgs.push_back(img); } Size board_size = Size(9, 6); //The number of corner points (height, width) and rows of rows should be different to determine the direction of the checkerboard. vector<vector<Point2f>> imgsPoints; for (int i = 0; i < imgs.size(); i++) { Mat img1 = imgs[i]; Mat gray1; cvtColor(img1, gray1, COLOR_BGR2GRAY); vector<Point2f> img1_points; findChessboardCorners(gray1, board_size, img1_points); //Calculate the corner points of the grid calibration plate find4QuadCornerSubpix(gray1, img1_points, Size(5, 5)); //Refine the coordinates of the corner points of the square calibrating plate bool pattern = true; drawChessboardCorners(img1, board_size, img1_points, pattern); imshow("img1", img1); waitKey(0); imgsPoints.push_back(img1_points); } // Generate the three-dimensional coordinates of each inner corner point of the checkerboard grid Size squareSize = Size(10, 10); //The real size of each square of the checkerboard grid vector<vector<Point3f>> objectPoints; for (int i = 0; i < imgsPoints.size(); i++)//The two-dimensional coordinate points under the pixel coordinate system of N pictures { vector<Point3f> tempPointSet; for (int j = 0; j < board_size.height; j++) { for (int k = 0; k < board_size.width; k++) { Point3f realPoint; // Assuming that the calibration plate is the z-plane of the world coordinate system, that is, z=0 realPoint.x = j * squareSize.width; realPoint.y = k * squareSize.height; realPoint.z = 0; tempPointSet.push_back(realPoint); } } objectPoints.push_back(tempPointSet); } ///* Initialize the number of corner points in each image, assuming that a complete calibration board can be seen in each image */ //vector<int> point_number; //for (int i = 0; i<(); i++) //{ // point_number.push_back(board_size.width*board_size.height); //} //Image size Size imageSize; imageSize.width = imgs[0].cols; imageSize.height = imgs[0].rows; Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); //Camera parameter matrix Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); //The 5 distortion coefficients of the camera: k1, k2, p1, p2, k3 vector<Mat> rvecs; //The rotation vector of each image vector<Mat> tvecs; //Plant amount of each image calibrateCamera(objectPoints, imgsPoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, 0); cout << "Camera's internal parameter matrix=" << endl << cameraMatrix << endl; cout << "Camera Distortion Coefficient" << distCoeffs << endl; waitKey(0); return 0; }