下面展示一些 内联代码片
。
// 内参标定
var foo = 'bar';
//内参标定
#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <cstdio>
#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
//此处省略各种头文件
using namespace cv;
using namespace std;
//此处省略help()函数
enum {
DETECTION = 0, CAPTURING = 1, CALIBRATED = 2
};
enum Pattern {
CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID
};
//计算重投影误差函数
static double computeReprojectionErrors(const vector<vector<Point3f> >& objectPoints,const vector<vector<Point2f> >& imagePoints,
const vector<Mat>& rvecs, const vector<Mat>& tvecs,const Mat& cameraMatrix, const Mat& distCoeffs,vector<float>& perViewErrors)
{
vector<Point2f> imagePoints2;
size_t totalPoints = 0;
double totalErr = 0, err;
perViewErrors.resize(objectPoints.size());
return std::sqrt(totalErr / totalPoints);
//此处省略...
}
static void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners)
{
corners.clear();
for (int i = 0; i < boardSize.height; ++i)
for (int j = 0; j < boardSize.width; ++j)
corners.push_back(Point3f(j*squareSize, i*squareSize, 0));
//省略...
//本文中用到的标定板,在该函数中的参数为:boardSize.width=7,boardSize.height=7,squareSize=0.025(此处单位为米)
}
//执行标定,包括计算重投影误差
static bool runCalibration(vector<vector<Point2f> > imagePoints,
Size imageSize, Size boardSize,
float squareSize, float aspectRatio,
int flags, Mat& cameraMatrix, Mat& distCoeffs,
vector<Mat>& rvecs, vector<Mat>& tvecs,
vector<float>& reprojErrs,
double& totalAvgErr)
{
vector<Point3f> newObjPoints;
//! [fixed_aspect]
cameraMatrix = Mat::eye(3, 3, CV_64F);
if (CALIB_FIX_ASPECT_RATIO)
cameraMatrix.at<double>(0, 0) = aspectRatio;
//! [fixed_aspect]
distCoeffs = Mat::zeros(8, 1, CV_64F);
cout << "New board corners: " << endl;
cout << newObjPoints[0] << endl;
cout << newObjPoints[boardSize.width - 1] << endl;
cout << newObjPoints[boardSize.width * (boardSize.height - 1)] << endl;
cout << newObjPoints.back() << endl;
cout << "Re-projection error reported by calibrateCamera: " << rms << endl;
bool ok = checkRange(cameraMatrix) && checkRange(distCoeffs);
bool isw = false;
objectPoints.clear();
objectPoints.resize(imagePoints.size(), newObjPoints);
totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints, rvecs, tvecs, cameraMatrix,
distCoeffs, reprojErrs);
//省略...
return true;
}
//读取字符串
static bool readStringList(const string& filename, vector<string>& l)
{
l.resize(0);
FileStorage fs(filename, FileStorage::READ);
if (!fs.isOpened())
return false;
FileNode n = fs["images"];
if (n.type() != FileNode::SEQ)
return false;
FileNodeIterator it = n.begin(), it_end = n.end();
for (; it != it_end; ++it)
l.push_back((string)*it);
return true;
}
//运行并保存
static bool runAndSave(const string& outputFilename,
const vector<vector<Point2f> >& imagePoints,
Size imageSize, Size boardSize, Pattern patternType, float squareSize,
float aspectRatio, int flags, Mat& cameraMatrix,
Mat& distCoeffs, bool writeExtrinsics, bool writePoints)
{
vector<Mat> rvecs, tvecs;
vector<float> reprojErrs;
double totalAvgErr = 0;
vector<Point3f> newObjPoints;
bool ok = runCalibration(imagePoints, imageSize, boardSize, squareSize, aspectRatio, flags, cameraMatrix, distCoeffs, rvecs, tvecs, reprojErrs,totalAvgErr);
cout << (ok ? "Calibration succeeded" : "Calibration failed")
<< ". avg re projection error = " << totalAvgErr << endl;
FileStorage fs(outputFilename, FileStorage::WRITE);
time_t tm;
time(&tm);
struct tm *t2 = localtime(&tm);
char buf[1024];
strftime(buf, sizeof(buf), "%c", t2);
fs << "calibration_time" << buf;
if (!rvecs.empty() || !reprojErrs.empty())
fs << "nr_of_frames" << (int)std::max(rvecs.size(), reprojErrs.size());
fs << "image_width" << imageSize.width;
fs << "image_height" << imageSize.height;
fs << "board_width" << boardSize.width;
fs << "board_height" << boardSize.height;
fs << "square_size" << squareSize;
std::stringstream flagsStringStream;
bool flag = true;
flagsStringStream << "flags:"
<< (flag & CALIB_USE_INTRINSIC_GUESS ? " +use_intrinsic_guess" : "")
<< (flag & CALIB_FIX_ASPECT_RATIO ? " +fix_aspectRatio" : "")
<< (flag & CALIB_FIX_PRINCIPAL_POINT ? " +fix_principal_point" : "")
<< (flag & CALIB_ZERO_TANGENT_DIST ? " +zero_tangent_dist" : "")
<< (flag & CALIB_FIX_K1 ? " +fix_k1" : "")
<< (flag & CALIB_FIX_K2 ? " +fix_k2" : "")
<< (flag & CALIB_FIX_K3 ? " +fix_k3" : "")
<< (flag & CALIB_FIX_K4 ? " +fix_k4" : "")
<< (flag & CALIB_FIX_K5 ? " +fix_k5" : "");
fs.writeComment(flagsStringStream.str());
fs << "camera_matrix" << cameraMatrix;
fs << "distortion_coefficients" << distCoeffs;
fs << "avg_reprojection_error" << totalAvgErr;
if ( !reprojErrs.empty())
fs << "per_view_reprojection_errors" << Mat(reprojErrs);
if (!rvecs.empty() && !tvecs.empty())
{
CV_Assert(rvecs[0].type() == tvecs[0].type());
Mat bigmat((int)rvecs.size(), 6, CV_MAKETYPE(rvecs[0].type(), 1));
bool needReshapeR = rvecs[0].depth() != 1 ? true : false;
bool needReshapeT = tvecs[0].depth() != 1 ? true : false;
for (size_t i = 0; i < rvecs.size(); i++)
{
Mat r = bigmat(Range(int(i), int(i + 1)), Range(0, 3));
Mat t = bigmat(Range(int(i), int(i + 1)), Range(3, 6));
if (needReshapeR)
rvecs[i].reshape(1, 1).copyTo(r);
else
{
//*.t() is MatExpr (not Mat) so we can use assignment operator
CV_Assert(rvecs[i].rows == 3 && rvecs[i].cols == 1);
r = rvecs[i].t();
}
if (needReshapeT)
tvecs[i].reshape(1, 1).copyTo(t);
else
{
CV_Assert(tvecs[i].rows == 3 && tvecs[i].cols == 1);
t = tvecs[i].t();
}
}
fs.writeComment("a set of 6-tuples (rotation vector + translation vector) for each view");
fs << "extrinsic_parameters" << bigmat;
}
if (writePoints && !imagePoints.empty())
{
Mat imagePtMat((int)imagePoints.size(), (int)imagePoints[0].size(), CV_32FC2);
for (size_t i = 0; i < imagePoints.size(); i++)
{
Mat r = imagePtMat.row(int(i)).reshape(2, imagePtMat.cols);
Mat imgpti(imagePoints[i]);
imgpti.copyTo(r);
}
fs << "image_points" << imagePtMat;
}
if (!newObjPoints.empty())
{
fs << "grid_points" << newObjPoints;
}
return true;
}
int main(int argc, char** argv)
{
cout << argc << endl;
for (size_t i = 0; i < argc; i++)
{
cout << argv[i] << endl;
}
Size boardSize, imageSize;
float squareSize, aspectRatio;
Mat cameraMatrix = Mat::eye(3, 3, CV_64F), distCoeffs;
string outputFilename;
string inputFilename = "";
int i, nframes;
bool writeExtrinsics, writePoints;
bool undistortImage = false;
int flags = 0;
VideoCapture capture;
bool flipVertical;
bool showUndistorted;
bool videofile;
int delay;
clock_t prevTimestamp = 0;
int mode = DETECTION;
int cameraId = 0;
vector<vector<Point2f> > imagePoints;
vector<string> imageList;
Pattern pattern = CIRCLES_GRID;//标定图案类型,对称圆形图案
boardSize.width = parser.get<int>("w");
boardSize.height = parser.get<int>("h");
if (parser.has("pt"))
{
string val = parser.get<string>("pt");
if (val == "circles")
pattern = CIRCLES_GRID;
else if (val == "acircles")
pattern = ASYMMETRIC_CIRCLES_GRID;
else if (val == "chessboard")
pattern = CHESSBOARD;
else
return fprintf(stderr, "Invalid pattern type: must be chessboard or circles\n"), -1;
}
squareSize = parser.get<float>("s");
nframes = parser.get<int>("n");
aspectRatio = parser.get<float>("a");
delay = parser.get<int>("d");
writePoints = parser.has("op");
writeExtrinsics = parser.has("oe");
if (parser.has("a"))
flags |= CALIB_FIX_ASPECT_RATIO;
if (parser.has("zt"))
flags |= CALIB_ZERO_TANGENT_DIST;
if (parser.has("p"))
flags |= CALIB_FIX_PRINCIPAL_POINT;
flipVertical = parser.has("v");
videofile = parser.has("V");
if (parser.has("o"))
outputFilename = parser.get<string>("o");
showUndistorted = parser.has("su");
if (isdigit(parser.get<string>("input_data")[0]))
cameraId = parser.get<int>("input_data");
else
inputFilename = parser.get<string>("input_data");
if (!parser.check())
{
parser.printErrors();
return -1;
}
if (squareSize <= 0)
return fprintf(stderr, "Invalid board square width\n"), -1;
if (nframes <= 3)
return printf("Invalid number of images\n"), -1;
if (aspectRatio <= 0)
return printf("Invalid aspect ratio\n"), -1;
if (delay <= 0)
return printf("Invalid delay\n"), -1;
if (boardSize.width <= 0)
return fprintf(stderr, "Invalid board width\n"), -1;
if (boardSize.height <= 0)
return fprintf(stderr, "Invalid board height\n"), -1;
if (!inputFilename.empty())
{
if (!videofile && readStringList(inputFilename, imageList))
mode = CAPTURING;
else
capture.open(inputFilename);
}
else
capture.open(cameraId);
if (!capture.isOpened() && imageList.empty())
return fprintf(stderr, "Could not initialize video (%d) capture\n", cameraId), -2;
if (!imageList.empty())
nframes = (int)imageList.size();
namedWindow("Image View",WINDOW_AUTOSIZE);
for (i = 0;; i++)
{
Mat view, viewGray;
bool blink = false;
if (i < (int)imageList.size())
{
view = imread(imageList[i], 1);
cvtColor(view, viewGray, COLOR_BGR2GRAY);
}
else
{
break;
}
imageSize = view.size();
vector<Point2f> pointbuf;
bool found;
imagePoints.push_back(pointbuf);
if (found)
drawChessboardCorners(view, boardSize, Mat(pointbuf), found);//在原图中绘制找到的圆心点,图4为其中的一幅图
string msg = mode == CAPTURING ? "100/100" :
mode == CALIBRATED ? "Calibrated" : "Press 'g' to start";
int baseLine = 0;
Size textSize = getTextSize(msg, 1, 1, 1, &baseLine);
Point textOrigin(view.cols - 2 * textSize.width - 10, view.rows - 2 * baseLine - 10);
/*if (mode == CAPTURING)
{
if (undistortImage)
msg = format("%d/%d Undist", (int)imagePoints.size(), nframes);
else
msg = format("%d/%d", (int)imagePoints.size(), nframes);
}*/
putText(view, msg, textOrigin, 1, 1,
mode != CALIBRATED ? Scalar(0, 0, 255) : Scalar(0, 255, 0));
if (blink)
bitwise_not(view, view);
if (mode == CALIBRATED && undistortImage)
{
Mat temp = view.clone();
undistort(temp, view, cameraMatrix, distCoeffs);
}
imshow("Image View", view);
char key = (char)waitKey(capture.isOpened() ? 50 : 500);
if (key == 27)
break;
if (key == 'u' && mode == CALIBRATED)
undistortImage = !undistortImage;
if (capture.isOpened() && key == 'g')
{
mode = CAPTURING;
imagePoints.clear();
}
if (imagePoints.size() >= (unsigned)nframes)
{
if (runAndSave(outputFilename, imagePoints, imageSize, boardSize, pattern, squareSize, aspectRatio, flags, cameraMatrix, distCoeffs, writeExtrinsics, writePoints))
mode = CALIBRATED;
else
mode = DETECTION;
}
}
//畸变矫正
/*if (!capture.isOpened() && !showUndistorted)
{
Mat view, rview, map1, map2;
initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),imageSize, CV_16SC2, map1, map2);
for (size_t i = 0; i < imageList.size(); i++)
{
view = imread(imageList[i], IMREAD_COLOR);
if (view.empty())
continue;
remap(view, rview, map1, map2, INTER_LINEAR);
imshow("Image View", rview);
char c = (char)waitKey();
}
}*/
return 0;
}
opencv_fx = halcon_f / sx *1000
opencv_fy = halcon_f / sy *1000
opencv的内参矩阵,其中f是焦距,dx是单个像元的宽,dy是单个像元的高,u0是中心点X坐标,v0是中心点Y坐标(对应halcon标定的参数)
链接: [link](https://pan.baidu.com/s/1ukJ87D820tR6m7xzlvR66w /)
提取码:q5nu