相机标定&读取摄像头
(因为用到的摄像头是红外线摄像头(监控摄像头),有必要了解其原理)从光谱来讲,和普通摄像头感可见光原理类似。红外摄像头工作原理是红外灯发出红外线照射物体,红外线漫反射,被监控摄像头接收,形成视频图像。
calibrate.h()
```
class CameraCalibrator {
vector> objectPoints;
vector> imagePoints;
Mat cameraMatrix;
Mat distCoeffs;
int flag;
Mat map1, map2;
bool mustInitUndistort;
public:
CameraCalibrator() : flag(0), mustInitUndistort(true) {};
int addChessboardPoints(const std::vector<:string>& filelist, cv::Size & boardSize);
// Add scene points and corresponding image points
void addPoints(const std::vector<:point2f>& imageCorners, const std::vector<:point3f>& objectCorners);
// Calibrate the camera
double calibrate(cv::Size &imageSize);
// Set the calibration flag
void setCalibrationFlag(bool radial8CoeffEnabled = false, bool tangentialParamEnabled = false);
// Remove distortion in an image (after calibration)
cv::Mat CameraCalibrator::remap(const cv::Mat ℑ);
// Getters
cv::Mat getCameraMatrix() { return cameraMatrix; }
cv::Mat getDistCoeffs() { return distCoeffs; }
};
```
CameraCalibrator.cpp
```
#include "CameraCalibrator.h"
using namespace cv;
cv::Mat CameraCalibrator::remap(const cv::Mat ℑ) {
cv::Mat undistorted;
if (mustInitUndistort) { // called once per calibration
cv::initUndistortRectifyMap(
cameraMatrix, // computed camera matrix
distCoeffs, // computed distortion matrix
cv::Mat(), // optional rectification (none)
cv::Mat(), // camera matrix to generate undistorted
cv::Size(720, 540),
// image.size(), // size of undistorted
CV_32FC1, // type of output map
map1, map2); // the x and y mapping functions
mustInitUndistort = false;
}
// Apply mapping functions
cv::remap(image, undistorted, map1, map2,
cv::INTER_LINEAR); // interpolation type
return undistorted;
}
```