首页 > 其他 > 详细

相机畸变矫正&读取

时间:2019-05-16 23:17:52      阅读:182      评论:0      收藏:0      [点我收藏+]

主要目的


  • 利用所得到的内外参矩阵读取矫正后的相机的视频流
    (因为用到的摄像头是红外线摄像头(监控摄像头),有必要了解其原理)从光谱来讲,和普通摄像头感可见光原理类似。红外摄像头工作原理是红外灯发出红外线照射物体,红外线漫反射,被监控摄像头接收,形成视频图像。

    程序源码

    整个项目主要包括以下程序

main.cpp
  
#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;
}
  
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& 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; }
    };


  
CameraCalibrator.cpp
  
        #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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!