From 5e95c8bae2819ec3ce2039781e09214152ba82fc Mon Sep 17 00:00:00 2001 From: ackimixs Date: Tue, 16 Apr 2024 17:06:31 +0200 Subject: [PATCH] normally work --- calibration.cpp | 182 ++++++++++++++++++++++-------------------------- 1 file changed, 84 insertions(+), 98 deletions(-) diff --git a/calibration.cpp b/calibration.cpp index c427fe8..e2699a0 100644 --- a/calibration.cpp +++ b/calibration.cpp @@ -1,112 +1,98 @@ -#include -#include -#include #include -#include +#include +#include +#include +#include +#include -int main(int argc, char *argv[]) +// Defining the dimensions of checkerboard +int CHECKERBOARD[2]{6,9}; + +int main() { + // Creating vector to store vectors of 3D points for each checkerboard image + std::vector > objpoints; - for (int i = 0; i < argc; i++) { - if (std::string(argv[i]) == "--help") - { - std::cout << "Usage: " << argv[0] << " " << std::endl; - std::cout << "directory: The directory containing the calibration images." << std::endl; - return 0; - } + // Creating vector to store vectors of 2D points for each checkerboard image + std::vector > imgpoints; + + // Defining the world coordinates for 3D points + std::vector objp; + for(int i{0}; i images; + // Path of the folder containing checkerboard images + std::string path = "./images/*.jpg"; + + cv::glob(path, images); + + cv::Mat frame, gray; + // vector to store the pixel coordinates of detected checker board corners + std::vector corner_pts; + bool success; + + // Looping over all the images in the directory + for(int i{0}; i" << std::endl; - return 1; - } + cv::imshow("Image",frame); + cv::waitKey(0); + } - // Set the chessboard size (number of inner corners in width and height) - cv::Size chessboardSize(9, 6); + cv::destroyAllWindows(); - cv::Size imgSize; + cv::Mat cameraMatrix,distCoeffs,R,T; - // Create vectors to store the detected chessboard corners and corresponding image points - std::vector> objectPoints; // 3D world points - std::vector> imagePoints; // 2D image points + /* + * Performing camera calibration by + * passing the value of known 3D points (objpoints) + * and corresponding pixel coordinates of the + * detected corners (imgpoints) + */ + cv::calibrateCamera(objpoints, imgpoints, cv::Size(gray.rows,gray.cols), cameraMatrix, distCoeffs, R, T); - // Generate the 3D world points for the chessboard corners - std::vector worldPoints; - for (int i = 0; i < chessboardSize.height; ++i) { - for (int j = 0; j < chessboardSize.width; ++j) { - worldPoints.emplace_back(j, i, 0); // Assuming the chessboard lies in the XY plane (Z=0) - } - } + std::cout << "cameraMatrix : " << cameraMatrix << std::endl; + std::cout << "distCoeffs : " << distCoeffs << std::endl; + std::cout << "Rotation vector : " << R << std::endl; + std::cout << "Translation vector : " << T << std::endl; - lccv::PiCamera* cam = new lccv::PiCamera; - cam->options->video_width=1920; - cam->options->video_height=1080; - cam->options->framerate=5; - cam->options->verbose=true; - cv::namedWindow("Video",cv::WINDOW_NORMAL); - cam->startVideo(); + cv::FileStorage fs("./calibration_results.yaml", cv::FileStorage::WRITE); + fs << "cameraMatrix" << cameraMatrix; + fs << "distCoeffs" << distCoeffs; + fs.release(); // Release the file + cv::destroyAllWindows(); - char key; - - while(key != 27) { - cv::Mat image, imageCopy, imgNotRotated; - if(!cam->getVideoFrame(imgNotRotated,1000)){ - std::cout<<"Timeout error"< corners; - - // if (findChessboardCorners(gray, chessboardSize, corners)) { - // // Refine corner locations - // cv::cornerSubPix(gray, corners, cv::Size(11, 11), cv::Size(-1, -1), - // cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 30, 0.001)); - // - // // Store object and image points - // objectPoints.push_back(worldPoints); - // imagePoints.push_back(corners); - // } - - putText(gray, "Press 'c' to add current frame. 'ESC' to finish and calibrate", - cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255, 0, 0), 2); - - imshow("Video", gray); - key = (char)cv::waitKey(500); - if(key == 'c' && findChessboardCorners(gray, chessboardSize, corners)) { - // Refine corner locations - cv::cornerSubPix(gray, corners, cv::Size(11, 11), cv::Size(-1, -1), - cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 30, 0.001)); - - // Store object and image points - objectPoints.push_back(worldPoints); - imagePoints.push_back(corners); - - imgSize = image.size(); - } - } - - cv::Mat cameraMatrix, distCoeffs; - std::vector rvecs, tvecs; - - double repErr = calibrateCamera(objectPoints, imagePoints, imgSize, - cameraMatrix, distCoeffs, rvecs, tvecs); - - std::cout << repErr << std::endl; - - cv::FileStorage fs("./calibration_results.yaml", cv::FileStorage::WRITE); - fs << "cameraMatrix" << cameraMatrix; - fs << "distCoeffs" << distCoeffs; - fs.release(); // Release the file - - cv::destroyAllWindows(); - - return 0; + return 0; } \ No newline at end of file