增加图像畸变矫正和pixel对应物理大小计算

This commit is contained in:
马学浜 xuebang_ma 2024-12-29 21:40:35 +08:00
parent cc31b91bf7
commit 5f06ddc447
2 changed files with 94 additions and 4 deletions

View File

@ -47,4 +47,13 @@ Mat get_magic_wand_image(Mat src,int x,int y,float max,float min);
// 锐化 // 锐化
Mat SetSharpen(Mat src); Mat SetSharpen(Mat src);
//相机标定功能gray是包含完整棋盘格灰度图像patternSize是棋盘格内角点的数量假如棋盘格的尺寸是9*7那内角点就是8*6grid_size是每个格子的物理大小
//cameraMatrix和distCoeffs是后续图像畸变矫正需要的参数标定一次后镜头无变动情况下后续图像直接用此参数矫正就可以pixel_size是矫正后每个pixel的物理大小
//返回值是0表示标定错误返回值是1表示标定正确
//注:相机标定功能得到的结果参数只能用于矫正和计算得到结果的输入图像分辨率一致的图
bool camera_calibration(Mat gray,cv::Size patternSize,float grid_size,cv::Mat& cameraMatrix,cv::Mat& distCoeffs,float& pixel_size);
//图像畸变矫正功能image是输入图像无要求cameraMatrix和distCoeffs是图像畸变矫正的参数
//返回值是矫正后的图像,输入图像是什么格式,输出图像就是什么格式
//注:相机标定功能得到的结果参数只能用于矫正和计算得到结果的输入图像分辨率一致的图
Mat distortion_correction(Mat image,cv::Mat cameraMatrix,cv::Mat distCoeffs);

View File

@ -1110,7 +1110,8 @@ Mat bgr_scale_image(Mat src, float maxVal, float minVal, int scientific_flag)
float rotia = 255.0 / (maxVal - minVal); float rotia = 255.0 / (maxVal - minVal);
Mat image = Mat(h + 2 * start, 3 * w, CV_8UC3); Mat image = Mat(h + 2 * start, 3 * w, CV_8UC3);
image.setTo(255); image.setTo(255);
src.copyTo(image(Rect(0, start, w, h))); src.copyTo(image(Rect(0,start,w,h)));
cv::rectangle(image, Rect(0,start,w,h), cv::Scalar(0,0,0), 2);
float diff = maxVal - minVal; float diff = maxVal - minVal;
int exponent = static_cast<int>(std::log10(diff)); int exponent = static_cast<int>(std::log10(diff));
@ -1188,8 +1189,7 @@ Mat bgr_scale_image(Mat src, float maxVal, float minVal, int scientific_flag)
std::ostringstream oss; std::ostringstream oss;
if (tickData[i] == int(tickData[i])) { if (tickData[i] == int(tickData[i])) {
oss << static_cast<int>(tickData[i]); oss << static_cast<int>(tickData[i]);
} } else {
else {
oss.precision(1); oss.precision(1);
oss << std::fixed << tickData[i]; oss << std::fixed << tickData[i];
} }
@ -1287,4 +1287,85 @@ Mat SetSharpen(Mat src)
usm = src + 0.5 * (src - blur_img); usm = src + 0.5 * (src - blur_img);
return usm; return usm;
}
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)
{
std::cerr << "Error: gray not CV_8UC1!" << std::endl;
return 0;
}
std::vector<cv::Point2f> corners;
bool found = cv::findChessboardCorners(gray, patternSize, corners,
cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_NORMALIZE_IMAGE);
if (found) {
cv::cornerSubPix(gray, corners, cv::Size(3, 3), cv::Size(-1, -1),
cv::TermCriteria(cv::TermCriteria::EPS | cv::TermCriteria::MAX_ITER, 30, 0.1));
} else {
std::cerr << "Error: Chessboard corners not found!" << std::endl;
return 0;
}
std::vector<std::vector<cv::Point3f>> objectPoints;
std::vector<std::vector<cv::Point2f>> imagePoints;
std::vector<cv::Point3f> objPts;
for (int i = 0; i < patternSize.height; ++i) {
for (int j = 0; j < patternSize.width; ++j) {
objPts.push_back(cv::Point3f(j, i, 0.0f));
}
}
objectPoints.push_back(objPts);
imagePoints.push_back(corners);
cameraMatrix = cv::Mat::eye(3, 3, CV_64F); // 相机矩阵初始化
distCoeffs = cv::Mat::zeros(8, 1, CV_64F); // 畸变系数初始化
std::vector<cv::Mat> rvecs, tvecs;
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;
}
Mat distortion_correction(Mat image,cv::Mat cameraMatrix,cv::Mat distCoeffs)
{
cv::Mat undistortedImage;
cv::undistort(image, undistortedImage, cameraMatrix, distCoeffs);
return undistortedImage;
} }