利用所得到的内外参矩阵读取矫正后的相机的视频流
(因为用到的摄像头是红外线摄像头(监控摄像头),有必要了解其原理)从光谱来讲,和普通摄像头感可见光原理类似。红外摄像头工作原理是红外灯发出红外线照射物体,红外线漫反射,被监控摄像头接收,形成视频图像。
整个项目主要包括以下程序
#include
#include
#include
#include"CameraCalibrator.h"
using namespace cv;
using namespace std;
int main()
{
VideoCapture capture(1);
CameraCalibrator cameraCalibrator;
if (capture.isOpened())
{
cout << "success" << endl;
}
Mat frame;
Mat ROI;
while (capture.isOpened())
{
capture >> frame;
Mat uImage = cameraCalibrator.remap(frame);
ROI = uImage(Rect(60, 0, 560, 410));
imshow("video1", ROI);
cout << uImage.size() << endl;
char key = static_cast(waitKey(50));//控制视频流的帧率,10ms一帧
if (key == 27) //按esc退出
break;
}
return 0;
}
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& filelist, cv::Size & boardSize);
// Add scene points and corresponding image points
void addPoints(const std::vector& imageCorners, const std::vector& 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 &image);
// Getters
cv::Mat getCameraMatrix() { return cameraMatrix; }
cv::Mat getDistCoeffs() { return distCoeffs; }
};
#include "CameraCalibrator.h"
using namespace cv;
cv::Mat CameraCalibrator::remap(const cv::Mat &image) {
cv::Mat undistorted;
cv::Mat cameraMatrix = cv::Mat::eye(3, 3, CV_64F);
//内参矩阵
cameraMatrix.at(0, 0) = 304.5343301172393;
cameraMatrix.at(0, 1) = 0;
cameraMatrix.at(0, 2) = 355.6868747437238;
cameraMatrix.at(1, 0) = 0;
cameraMatrix.at(1, 1) = 304.4062190996952;
cameraMatrix.at(1, 2) = 275.6767450385067;
cameraMatrix.at(2, 0) = 0;
cameraMatrix.at(2, 1) = 0;
cameraMatrix.at(2, 2) = 1;
//畸变参数
cv::Mat distCoeffs = cv::Mat::zeros(5, 1, CV_64F);
distCoeffs.at(0, 0) = -0.3559459318901729;
distCoeffs.at(1, 0) = 0.1634235977115495;
distCoeffs.at(2, 0) = 0.000291221640019827;
distCoeffs.at(3, 0) = 0.002862694736722168;
distCoeffs.at(4, 0) = -0.04160147169190362;
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;
}
原文:https://www.cnblogs.com/elong1995/p/10878638.html