Calibrating a single camera using OpenCV 2.3.1 and C ++

I am trying to calibrate a webcam using OpenCV 2.3.1 and Visual Studio 2010 (C ++ Console Application). I am using this class:

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);
    }
};

      

I am using 10 checkerboard images (assuming enough for calibration) at 640x480. The main function looks like this:

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;
}

      

Everything works without errors. cameraMatrix looks something like this:

685.65 0 365.14
0 686.38 206.98
0 0 1

Calibration error 0.310157, which is acceptable.

But when I use remapping, the output image looks even worse than the original . Here's an example:

Original Image: Original image]

Undistorted image: Undistorted image]

So the question is, am I doing something wrong during the calibration process? Is 10 checkerboard images enough for calibration? Do you have any suggestions?

+3


source to share


2 answers


The camera sensor does not distort the lens, these 4 values ​​are just the focal length (in H and V) and the center of the image (in X and Y)



There are 3 or 4 more lines of value string ( distCoeffs

) in your code that contains the lens mapping - see Karl's comment for example code

+1


source


Calibration is performed using numerical optimization, which has a rather shallow slope near the solution. Moreover, the function to be minimized is highly non-linear. So my guess is that your 10 images are not enough. I am calibrating cameras with very wide angle lenses (i.e. very distorted images) and I am trying to get both 50 and 60 images.

I'm trying to get 3 or 4 positions checkerboard images along each edge of the image, plus some in the middle, with multiple orientations relative to the camera and 3 different distances (super close, typical and up to how you can get and still solve the checkerboard ).



Getting the checkerboard close to the corners is very important. Your reference images do not have a checkerboard very close to the corner of the image. These are the points that limit the calibration to work correctly in highly distorted parts of the image (corners).

+1


source







All Articles