标题: opencv单目标定程序源码 [打印本页]

作者: tmac1    时间: 2016-11-7 14:39
标题: opencv单目标定程序源码
#include <iostream>
#include <iomanip>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>

#include "CameraCalibrator.h"

int main()
{

        cv::namedWindow("Image");
        cv::Mat image;
        std::vector<std::string> filelist;

        // generate list of chessboard image filename
        for (int i=1; i<=20; i++) {

                std::stringstream str;
                str << "../chessboards/chessboard" << std::setw(2) << std::setfill('0') << i << ".jpg";
                std::cout << str.str() << std::endl;

                filelist.push_back(str.str());
                image= cv::imread(str.str(),0);
                cv::imshow("Image",image);
       
                 cv::waitKey(100);
        }

        // Create calibrator object
    CameraCalibrator cameraCalibrator;
        // add the corners from the chessboard
        cv::Size boardSize(7,7);
        cameraCalibrator.addChessboardPoints(
                filelist,        // filenames of chessboard image
                boardSize);        // size of chessboard
                // calibrate the camera
    //        cameraCalibrator.setCalibrationFlag(true,true);
        cameraCalibrator.calibrate(image.size());

    // Image Undistortion
    image = cv::imread(filelist[6]);
        cv::Mat uImage= cameraCalibrator.remap(image);

        // display camera matrix
        cv::Mat cameraMatrix= cameraCalibrator.getCameraMatrix();
        std::cout << " Camera intrinsic: " << cameraMatrix.rows << "x" << cameraMatrix.cols << std::endl;
        std::cout << cameraMatrix.at<double>(0,0) << " " << cameraMatrix.at<double>(0,1) << " " << cameraMatrix.at<double>(0,2) << std::endl;
        std::cout << cameraMatrix.at<double>(1,0) << " " << cameraMatrix.at<double>(1,1) << " " << cameraMatrix.at<double>(1,2) << std::endl;
        std::cout << cameraMatrix.at<double>(2,0) << " " << cameraMatrix.at<double>(2,1) << " " << cameraMatrix.at<double>(2,2) << std::endl;

    imshow("Original Image", image);
    imshow("Undistorted Image", uImage);
        //打印相机内参数矩阵(3*3矩阵)
   /* cv::  Mat cameraMatrix = cameraCalibrator.getCameraMatrix();
    std::cout << " 相机内参数矩阵:" << cameraMatrix.rows << "x" << cameraMatrix.cols << std::endl;
    for (int i=0; i for (int j=0; j {
    cout<(i, j);
    if (j==2)
    cout< }*/
    //打印畸变系数矩阵(1*5矩阵)
    cv:: Mat distCoeffs = cameraCalibrator.getDistCoeffs();
    std::cout << "畸变系数矩阵:" << distCoeffs.rows << "x" << distCoeffs.cols << std::endl;
    //for (int i=0; i for (int j=0; j cout<(i, j)<<"\t";
        std::cout << distCoeffs.at<double>(0,0) << " " << distCoeffs.at<double>(0,1) << " " << distCoeffs.at<double>(0,2)<<" " << distCoeffs.at<double>(0,3)<<" "<< distCoeffs.at<double>(0,4)<< std::endl;



        cv::waitKey();
        return 0;
}






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1