develop #21

Closed
maxbang wants to merge 4 commits from (deleted):develop into develop
Showing only changes of commit 3fad215f7a - Show all commits

View File

@ -1278,7 +1278,7 @@ Mat get_magic_wand_image(Mat src,int x,int y,float max,float min)
return matDst;
}
bool camera_calibration(Mat gray,cv::Size patternSize,cv::Mat& cameraMatrix,cv::Mat& distCoeffs)
bool camera_calibration(Mat gray,cv::Size patternSize,float grid_size,cv::Mat& cameraMatrix,cv::Mat& distCoeffs,float& pixel_size)
{
if(gray.type() != CV_8UC1)
{
@ -1310,12 +1310,46 @@ bool camera_calibration(Mat gray,cv::Size patternSize,cv::Mat& cameraMatrix,cv::
cameraMatrix = cv::Mat::eye(3, 3, CV_64F); // 相机矩阵初始化
distCoeffs = cv::Mat::zeros(8, 1, CV_64F); // 畸变系数初始化
std::vector<cv::Mat> rvecs, tvecs;
int calibrationFlags = cv::CALIB_ZERO_TANGENT_DIST + // 假设无切向畸变
cv::CALIB_FIX_K4 + // 固定第四个畸变参数通常为0
cv::CALIB_FIX_K5; // 固定第五个畸变参数通常为0
calibrationFlags = 0;
int calibrationFlags = 0;
TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 30, DBL_EPSILON);
cv::calibrateCamera(objectPoints, imagePoints, gray.size(), cameraMatrix, distCoeffs, rvecs, tvecs,calibrationFlags,criteria);
vector<Point2f> undistortedCorners;
undistortPoints(corners, undistortedCorners, cameraMatrix, distCoeffs);
std::vector<cv::Point3f> homogeneousPoints;
for (const auto& p : undistortedCorners) {
homogeneousPoints.push_back(cv::Point3f(p.x, p.y, 1.0f)); // 将 (x, y) 转换为 (x, y, 1)
}
std::vector<cv::Point2f> pixelPoints;
for (const auto& p : homogeneousPoints) {
cv::Mat pointMat = (cv::Mat_<double>(3, 1) << p.x, p.y, p.z);
cv::Mat transformedPoint = cameraMatrix * pointMat;
float x = transformedPoint.at<double>(0, 0) / transformedPoint.at<double>(2, 0);
float y = transformedPoint.at<double>(1, 0) / transformedPoint.at<double>(2, 0);
pixelPoints.push_back(cv::Point2f(x, y));
}
int numCols = patternSize.width;
int numRows = patternSize.height;
float meanSideLength = 0;
int num = 0;
for (int i = 0; i < (patternSize.height - 1); ++i) {
for (int j = 0; j < (patternSize.width - 1); ++j) {
int index = i * patternSize.width + j;
if (j + 1 < numCols) {
float width = static_cast<float>(norm(pixelPoints[index] - pixelPoints[index + 1]));
meanSideLength+=(width);
num++;
}
if (i + 1 < patternSize.height) {
float height = static_cast<float>(norm(pixelPoints[index] - pixelPoints[index + numCols]));
meanSideLength+=(height);
num++;
}
}
}
meanSideLength /= num;
pixel_size = grid_size/meanSideLength;
return 1;
}