develop #19

Closed
maxbang wants to merge 2 commits from (deleted):develop into develop
2 changed files with 58 additions and 1 deletions

View File

@ -78,3 +78,12 @@ Mat get_photon_image(Mat src, float sec, float Wcm, float Hcm, float sr);
//魔术棒功能src是处理成8bit的图x,y是点击位置的坐标max和min是设定的大小,max和min需要注意除以256使用0-255数据 //魔术棒功能src是处理成8bit的图x,y是点击位置的坐标max和min是设定的大小,max和min需要注意除以256使用0-255数据
//点击位置的像素差在[min,max]范围内的连在一起的像素,都会被框选 //点击位置的像素差在[min,max]范围内的连在一起的像素,都会被框选
Mat get_magic_wand_image(Mat src,int x,int y,float max,float min); Mat get_magic_wand_image(Mat src,int x,int y,float max,float min);
//相机标定功能gray是包含完整棋盘格灰度图像patternSize是棋盘格内角点的数量假如棋盘格的尺寸是9*7那内角点就是8*6
//cameraMatrix和distCoeffs是后续图像畸变矫正需要的参数标定一次后镜头无变动情况下后续图像直接用此参数矫正就可以
//返回值是0表示标定错误返回值是1表示标定正确
//注:相机标定功能得到的结果参数只能用于矫正和计算得到结果的输入图像分辨率一致的图
bool camera_calibration(Mat gray,cv::Size patternSize,cv::Mat& cameraMatrix,cv::Mat& distCoeffs);
//图像畸变矫正功能image是输入图像无要求cameraMatrix和distCoeffs是图像畸变矫正的参数
//返回值是矫正后的图像,输入图像是什么格式,输出图像就是什么格式
//注:相机标定功能得到的结果参数只能用于矫正和计算得到结果的输入图像分辨率一致的图
Mat distortion_correction(Mat image,cv::Mat cameraMatrix,cv::Mat distCoeffs);

View File

@ -1277,3 +1277,51 @@ 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;
}