使用OpenCV 2.3.1和C ++校准单个摄像头

时间:2012-04-05 12:00:28

标签: c++ opencv camera-calibration

我正在尝试使用OpenCV 2.3.1和Visual Studio 2010(c ++控制台应用)校准网络摄像头。我正在上课:

class CameraCalibrator{
private:
   std::vector<std::vector<cv::Point3f>> objectPoints;
   std::vector<std::vector<cv::Point2f>> imagePoints;
   //Square Lenght
   float squareLenght;
   //output Matrices
   cv::Mat cameraMatrix; //intrinsic
   cv::Mat distCoeffs;
   //flag to specify how calibration is done
   int flag;
   //used in image undistortion
   cv::Mat map1,map2;
   bool mustInitUndistort;
public:
    CameraCalibrator(): flag(0), squareLenght(36.0), mustInitUndistort(true){};
    int addChessboardPoints(const std::vector<std::string>& filelist,cv::Size& boardSize){
        std::vector<std::string>::const_iterator itImg;
        std::vector<cv::Point2f> imageCorners;
        std::vector<cv::Point3f> objectCorners;
        //initialize the chessboard corners in the chessboard reference frame
        //3d scene points
        for(int i = 0; i<boardSize.height; i++){
            for(int j=0;j<boardSize.width;j++){
                objectCorners.push_back(cv::Point3f(float(i)*squareLenght,float(j)*squareLenght,0.0f));
            }
        }
        //2D Image points:
        cv::Mat image; //to contain chessboard image
        int successes = 0;
        //cv::namedWindow("Chess");
        for(itImg=filelist.begin(); itImg!=filelist.end(); itImg++){
            image = cv::imread(*itImg,0);
            bool found = cv::findChessboardCorners(image, boardSize, imageCorners);
            //cv::drawChessboardCorners(image, boardSize, imageCorners, found);
            //cv::imshow("Chess",image);
            //cv::waitKey(1000);
            cv::cornerSubPix(image, imageCorners, cv::Size(5,5),cv::Size(-1,-1),
                cv::TermCriteria(cv::TermCriteria::MAX_ITER+cv::TermCriteria::EPS,30,0.1));
            //if we have a good board, add it to our data
            if(imageCorners.size() == boardSize.area()){
                addPoints(imageCorners,objectCorners);
                successes++;
            }
        }
        return successes;
    }
    void addPoints(const std::vector<cv::Point2f>& imageCorners,const std::vector<cv::Point3f>& objectCorners){
        //2D image point from one view
        imagePoints.push_back(imageCorners);
        //corresponding 3D scene points
        objectPoints.push_back(objectCorners);
    }
    double calibrate(cv::Size &imageSize){
        mustInitUndistort = true;
        std::vector<cv::Mat> rvecs,tvecs;
        return
            cv::calibrateCamera(objectPoints, //the 3D points
                imagePoints,
                imageSize, 
                cameraMatrix, //output camera matrix
                distCoeffs,
                rvecs,tvecs,
                flag);
    }
    void remap(const cv::Mat &image, cv::Mat &undistorted){
        std::cout << cameraMatrix;
        if(mustInitUndistort){ //called once per calibration
            cv::initUndistortRectifyMap(
                cameraMatrix,
                distCoeffs,
                cv::Mat(),
                cameraMatrix,
                image.size(),
                CV_32FC1,
                map1,map2);
            mustInitUndistort = false;
        }
        //apply mapping functions
        cv::remap(image,undistorted,map1,map2,cv::INTER_LINEAR);
    }
};

我正在使用10张棋盘图像(假设这足以进行校准),分辨率为640x480。主要功能如下:

int main(){
    CameraCalibrator calibrateCam;
    std::vector<std::string> filelist;
    filelist.push_back("img10.jpg");
    filelist.push_back("img09.jpg");
    filelist.push_back("img08.jpg");
    filelist.push_back("img07.jpg");
    filelist.push_back("img06.jpg");
    filelist.push_back("img05.jpg");
    filelist.push_back("img04.jpg");
    filelist.push_back("img03.jpg");
    filelist.push_back("img02.jpg");
    filelist.push_back("img01.jpg");

    cv::Size boardSize(8,6);
    double calibrateError;
    int success;
    success = calibrateCam.addChessboardPoints(filelist,boardSize);
    std::cout<<"Success:" << success << std::endl;
    cv::Size imageSize;
    cv::Mat inputImage, outputImage;
    inputImage = cv::imread("img10.jpg",0);
    outputImage = inputImage.clone();
    imageSize = inputImage.size();
    calibrateError = calibrateCam.calibrate(imageSize);
    std::cout<<"Calibration error:" << calibrateError << std::endl;
    calibrateCam.remap(inputImage,outputImage);
    cv::namedWindow("Original");
    cv::imshow("Original",inputImage);
    cv::namedWindow("Undistorted");
    cv::imshow("Undistorted",outputImage);
    cv::waitKey();
    return 0;
}

一切都运行没有错误。 cameraMatrix看起来像这样(大约):

685.65 0 365.14
0 686.38 206.98
0 0 1

校准误差为0.310157,这是可以接受的。

但是当我使用重映射时,输出图像看起来比原来的更差。以下是样本:

原始图片:Original image]

未失真的图片:Undistorted image]

所以,问题是,我在校准过程中做错了吗? 10个不同的棋盘图像是否足以进行校准?你有什么建议吗?

2 个答案:

答案 0 :(得分:1)

相机矩阵不会使镜头失真,这4个值只是焦距(在H和V中)和图像中心(在X和Y中)

您的代码中还有另外3或4个值行矩阵(distCoeffs),其中包含镜头映射 - 请参阅Karl的答案,例如代码

答案 1 :(得分:1)

校准通过数值优化完成,该解决方案附近具有非常浅的斜率。而且,最小化的功能是非常非线性的。所以,我的猜测是你的10张图片还不够。我使用非常广角镜头(即非常扭曲的图像)校准相机,我尝试拍摄50或60张图像。

我尝试使用棋盘沿着图像的每个边缘在3或4个位置处获取图像,加上一些位于中间,具有相对于相机的多个方向和3个不同距离(超近,典型和远因为你可以得到并仍然解决棋盘)。

让棋盘靠近角落非常重要。您的示例图像没有非常靠近图像角落的棋盘。正是那些限制校准的点在图像的非常扭曲的部分(角落)做正确的事情。