opencv相机标定
#include<opencv2/opencv.hpp>
std::vector<cv::Point2f> myFindCorners(int rows, int cols, cv::Mat mat);
std::vector<std::vector<cv::Point2f>> getImagePoints(std::vector<cv::Mat> mat_vec);
std::vector<cv::Mat> getMatVector(std::string filePath = "");
std::vector<std::vector<cv::Point3f>> getObjectPoints(cv::Size board_size,cv::Size square_size) {
std::vector<std::vector<cv::Point3f>> objectPoints;
for (int t = 0; t < 4; t++)
{
std::vector<cv::Point3f> object_points;
for (int i = 0; i < board_size.height; i++)
{
for (int j = 0; j < board_size.width; j++)
{
cv::Point3f realPoint;
realPoint.x = i * square_size.width;
realPoint.y = j * square_size.height;
realPoint.z = 0;
object_points.push_back(realPoint);
}
}
objectPoints.push_back(object_points);
}
return objectPoints;
}
int main() {
cv::Size board_size(9, 6);
cv::Size square_size(30, 30);
cv::Mat mat = cv::imread("D://pic//calibration//cali_2.jpg",CV_LOAD_IMAGE_UNCHANGED);
cv::Mat dst(mat.size(), mat.type());
cv::Size imageSize(mat.cols, mat.rows);
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_32FC1, cv::Scalar::all(0));
cv::Mat distCoeffs = cv::Mat(1, 5, CV_32FC1, cv::Scalar::all(0));
std::vector<cv::Mat> rvecsMat;
std::vector<cv::Mat> tvecsMat;
double error = cv::calibrateCamera(getObjectPoints(board_size, square_size),
getImagePoints(getMatVector()),
imageSize, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, CV_CALIB_FIX_K3);
std::cout << "重投影误差:" << error << std::endl <<
"内参矩阵:" << std::endl << cameraMatrix << std::endl <<
"畸变系数:" << distCoeffs << std::endl;
cv::Mat mapx, mapy;
cv::Mat R = cv::Mat::eye(3, 3, CV_32F);
cv::initUndistortRectifyMap(cameraMatrix, distCoeffs,
R, cv::Mat(), mat.size(), CV_32FC1, mapx, mapy);
remap(mat,dst,mapx, mapy, cv::INTER_LINEAR);
return 0;
}
std::vector<cv::Mat> getMatVector(std::string filePath) {
std::vector<cv::Mat> mat_vec;
mat_vec.push_back(cv::imread("D://pic//calibration//cali_1.jpg", CV_LOAD_IMAGE_UNCHANGED));
mat_vec.push_back(cv::imread("D://pic//calibration//cali_2.jpg", CV_LOAD_IMAGE_UNCHANGED));
mat_vec.push_back(cv::imread("D://pic//calibration//cali_3.jpg", CV_LOAD_IMAGE_UNCHANGED));
mat_vec.push_back(cv::imread("D://pic//calibration//cali_4.jpg", CV_LOAD_IMAGE_UNCHANGED));
return mat_vec;
}
std::vector<std::vector<cv::Point2f>> getImagePoints(std::vector<cv::Mat> mat_vec) {
std::vector<std::vector<cv::Point2f>> imagePoints;
for (std::vector<cv::Mat>::iterator it = mat_vec.begin(); it != mat_vec.end(); it++) {
imagePoints.push_back(myFindCorners(9, 6, *it));
}
return imagePoints;
}
std::vector<cv::Point2f> myFindCorners(int rows, int cols, cv::Mat mat) {
cv::Mat mat_copy = mat;
cv::Mat gray;
cv::cvtColor(mat_copy, gray, CV_BGRA2GRAY);
int h = rows;
int w = cols;
std::vector<cv::Point2f> corners;
corners.resize(gray.cols * gray.rows);
memset(corners.data(), 0, corners.size() * sizeof(cv::Point2f));
bool patternWasFound = cv::findChessboardCorners(gray, cv::Size(h, w),
corners, cv::CALIB_CB_ADAPTIVE_THRESH);
if (!patternWasFound) {
std::cout << "获取角点失败" << std::endl;
return cv::Mat();
}
cv::TermCriteria termCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 20, 0.1);
cv::cornerSubPix(gray, corners, cv::Size(5, 5), cv::Size(-1, -1), termCriteria);
return corners;
}