# MATLAB + OpenCV 简单相机标定教程

解决相机的标定问题！接下来介绍Matlab 进行相机标定的流程操作！

这种标定的方式在网上有很多的公式啊，原理讲解，这里只是在应用上进行介绍！
当然了，还是需要简单了解下标定基础知识的： 点击打开链接

在标定时需要标定板，就是黑白网格图例 ，opencv的官网上可以得到

#### 首先就是进行标定板的图像采集

对于标定板，由于时间有限，就不采用打印的方式了，我直接在网页上显示！环保绿色！

标定嘛，肯定需要具体尺寸样标的，所以我直接在电脑上量取，这个误差在小范围内可以允许！

#include "opencv2/opencv.hpp"
#include <string>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
VideoCapture inputVideo(0);
inputVideo.set(CV_CAP_PROP_FRAME_WIDTH, 800);
inputVideo.set(CV_CAP_PROP_FRAME_HEIGHT, 600);
if (!inputVideo.isOpened())
{
cout << "Could not open the input video " << endl;
return -1;
}
Mat frame;
string imgname;
int f = 1;
while (1) //Show the image captured in the window and repeat
{
if (frame.empty()) break;         // check if at end
imshow("Camera", frame);
char key = waitKey(1);
if (key == 27)break;
if (key == 'q' || key == 'Q')
{
imgname = to_string(f++) + ".jpg";
imwrite(imgname, frame);
}
}
cout << "Finished writing" << endl;
return 0;
}

inputVideo.set(CV_CAP_PROP_FRAME_HEIGHT, 600);

#### 矫正代码示例：

#include "opencv2/opencv.hpp"
#include <string>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
VideoCapture inputVideo(1);
inputVideo.set(CV_CAP_PROP_FRAME_WIDTH, 800);
inputVideo.set(CV_CAP_PROP_FRAME_HEIGHT, 600);
if (!inputVideo.isOpened())
{
cout << "Could not open the input video: " << endl;
return -1;
}
Mat frame;
Mat frameCalibration;

inputVideo >> frame;
Mat cameraMatrix = Mat::eye(3, 3, CV_64F);
cameraMatrix.at<double>(0, 0) = 917.052472085750;
cameraMatrix.at<double>(0, 1) = 0.657056681717874;
cameraMatrix.at<double>(0, 2) = 408.884053678499;
cameraMatrix.at<double>(1, 1) = 916.541676777971;
cameraMatrix.at<double>(1, 2) = 332.189066871859;

Mat distCoeffs = Mat::zeros(5, 1, CV_64F);
distCoeffs.at<double>(0, 0) = -0.108750634204250;
distCoeffs.at<double>(1, 0) = -0.155068804309725;
distCoeffs.at<double>(2, 0) = -0.00261486335789016;
distCoeffs.at<double>(3, 0) = 0.00154770538482982;
distCoeffs.at<double>(4, 0) = 0;

Mat view, rview, map1, map2;
Size imageSize;

imageSize = frame.size();
initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),
imageSize, CV_16SC2, map1, map2);

while (1) //Show the image captured in the window and repeat
{
if (frame.empty()) break;         // check if at end
remap(frame, frameCalibration, map1, map2, INTER_LINEAR);
imshow("Origianl", frame);
imshow("Calibration", frameCalibration);
char key = waitKey(1);
if (key == 27 || key == 'q' || key == 'Q')break;
}
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客