develop #21

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

View File

@ -1276,4 +1276,52 @@ Mat get_magic_wand_image(Mat src,int x,int y,float max,float min)
} }
} }
return matDst; return matDst;
}
bool camera_calibration(Mat gray,cv::Size patternSize,cv::Mat& cameraMatrix,cv::Mat& distCoeffs)
{
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 = cv::CALIB_ZERO_TANGENT_DIST + // 假设无切向畸变
cv::CALIB_FIX_K4 + // 固定第四个畸变参数通常为0
cv::CALIB_FIX_K5; // 固定第五个畸变参数通常为0
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);
return 1;
}
Mat distortion_correction(Mat image,cv::Mat cameraMatrix,cv::Mat distCoeffs)
{
cv::Mat undistortedImage;
cv::undistort(image, undistortedImage, cameraMatrix, distCoeffs);
return undistortedImage;
} }