calib2
This commit is contained in:
parent
55939f7f9c
commit
ee764361f5
12
README.md
12
README.md
|
|
@ -1 +1,11 @@
|
||||||
This is the README for the EU_Robot of Group D.
|
This is the README for the EU_Robot of Group D.
|
||||||
|
|
||||||
|
// ...existing content...
|
||||||
|
|
||||||
|
## Calibration Instructions
|
||||||
|
|
||||||
|
To calibrate the webcam using a chessboard pattern:
|
||||||
|
|
||||||
|
1. Ensure you have a printed chessboard pattern with 9x6 inner corners.
|
||||||
|
2. Run the `calibrationWebcam` node:
|
||||||
|
|
||||||
|
|
@ -1,73 +1,78 @@
|
||||||
|
#include <ros/ros.h>
|
||||||
#include <opencv2/opencv.hpp>
|
#include <opencv2/opencv.hpp>
|
||||||
#include <opencv2/calib3d.hpp>
|
#include <opencv2/highgui/highgui.hpp>
|
||||||
#include <opencv2/highgui.hpp>
|
#include <opencv2/calib3d/calib3d.hpp>
|
||||||
#include <opencv2/imgproc.hpp>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
int main()
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
// Define the number of inner corners per chessboard row and column
|
ros::init(argc, argv, "calibration_webcam");
|
||||||
int boardWidth = 9;
|
ros::NodeHandle nh;
|
||||||
int boardHeight = 6;
|
|
||||||
cv::Size boardSize(boardWidth, boardHeight);
|
|
||||||
|
|
||||||
// Prepare object points (0,0,0), (1,0,0), (2,0,0), ..., (boardWidth-1, boardHeight-1, 0)
|
cv::Size boardSize(9, 6); // Chessboard size
|
||||||
|
std::vector<std::vector<cv::Point2f>> imagePoints;
|
||||||
|
std::vector<std::vector<cv::Point3f>> objectPoints;
|
||||||
std::vector<cv::Point3f> obj;
|
std::vector<cv::Point3f> obj;
|
||||||
for (int i = 0; i < boardHeight; ++i)
|
for (int i = 0; i < boardSize.height; ++i)
|
||||||
for (int j = 0; j < boardWidth; ++j)
|
for (int j = 0; j < boardSize.width; ++j)
|
||||||
obj.push_back(cv::Point3f(j, i, 0));
|
obj.push_back(cv::Point3f(j, i, 0));
|
||||||
|
|
||||||
std::vector<std::vector<cv::Point3f>> objectPoints;
|
cv::VideoCapture cap(0);
|
||||||
std::vector<std::vector<cv::Point2f>> imagePoints;
|
|
||||||
std::vector<cv::Point2f> corners;
|
|
||||||
|
|
||||||
cv::Mat frame, gray;
|
|
||||||
cv::VideoCapture cap(0); // Open the default camera
|
|
||||||
|
|
||||||
if (!cap.isOpened())
|
if (!cap.isOpened())
|
||||||
{
|
{
|
||||||
std::cerr << "Unable to open camera" << std::endl;
|
ROS_ERROR("Unable to open camera");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int numFrames = 0;
|
cv::Mat frame, gray;
|
||||||
while (numFrames < 20) // Capture 20 frames
|
int frameCount = 0;
|
||||||
|
while (ros::ok())
|
||||||
{
|
{
|
||||||
cap >> frame;
|
cap >> frame;
|
||||||
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
|
if (frame.empty())
|
||||||
|
{
|
||||||
|
ROS_ERROR("Captured empty frame");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
|
||||||
|
std::vector<cv::Point2f> corners;
|
||||||
bool found = cv::findChessboardCorners(gray, boardSize, corners,
|
bool found = cv::findChessboardCorners(gray, boardSize, corners,
|
||||||
cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FAST_CHECK | cv::CALIB_CB_NORMALIZE_IMAGE);
|
cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FAST_CHECK | cv::CALIB_CB_NORMALIZE_IMAGE);
|
||||||
|
|
||||||
if (found)
|
if (found)
|
||||||
{
|
{
|
||||||
cv::cornerSubPix(gray, corners, cv::Size(11, 11), cv::Size(-1, -1),
|
cv::cornerSubPix(gray, corners, cv::Size(11, 11), cv::Size(-1, -1),
|
||||||
cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 30, 0.1));
|
cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 30, 0.1));
|
||||||
cv::drawChessboardCorners(frame, boardSize, corners, found);
|
cv::drawChessboardCorners(frame, boardSize, corners, found);
|
||||||
imagePoints.push_back(corners);
|
imagePoints.push_back(corners);
|
||||||
objectPoints.push_back(obj);
|
objectPoints.push_back(obj);
|
||||||
numFrames++;
|
frameCount++;
|
||||||
|
ROS_INFO("Frame %d captured", frameCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
cv::imshow("Chessboard", frame);
|
cv::imshow("Calibration", frame);
|
||||||
if (cv::waitKey(30) == 27) // Exit on ESC key
|
if (cv::waitKey(30) == 27) break; // Exit on ESC key
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cv::destroyAllWindows();
|
if (imagePoints.size() < 10)
|
||||||
|
{
|
||||||
|
ROS_ERROR("Not enough frames captured for calibration");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Calibrate the camera
|
cv::Mat cameraMatrix, distCoeffs;
|
||||||
cv::Mat cameraMatrix, distCoeffs, rvecs, tvecs;
|
std::vector<cv::Mat> rvecs, tvecs;
|
||||||
cv::calibrateCamera(objectPoints, imagePoints, gray.size(), cameraMatrix, distCoeffs, rvecs, tvecs);
|
cv::calibrateCamera(objectPoints, imagePoints, gray.size(), cameraMatrix, distCoeffs, rvecs, tvecs);
|
||||||
|
|
||||||
// Save the camera parameters
|
ROS_INFO("Calibration successful");
|
||||||
|
ROS_INFO_STREAM("Camera Matrix: " << cameraMatrix);
|
||||||
|
ROS_INFO_STREAM("Distortion Coefficients: " << distCoeffs);
|
||||||
|
|
||||||
cv::FileStorage fs("camera_parameters.yml", cv::FileStorage::WRITE);
|
cv::FileStorage fs("camera_parameters.yml", cv::FileStorage::WRITE);
|
||||||
fs << "camera_matrix" << cameraMatrix;
|
fs << "camera_matrix" << cameraMatrix;
|
||||||
fs << "distortion_coefficients" << distCoeffs;
|
fs << "distortion_coefficients" << distCoeffs;
|
||||||
fs.release();
|
fs.release();
|
||||||
|
|
||||||
std::cout << "Camera calibration completed and parameters saved to camera_parameters.yml" << std::endl;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue