Exemplo n.º 1
0
bool CameraCalibration::runCalibration( Settings& s, Size& imageSize, Mat& cameraMatrix, Mat& distCoeffs,
                            vector<vector<Point2f> > imagePoints, vector<Mat>& rvecs, vector<Mat>& tvecs,
                            vector<float>& reprojErrs, double& totalAvgErr )
{
    cameraMatrix = Mat::eye( 3, 3, CV_64F );
    if ( s.flag & CALIB_FIX_ASPECT_RATIO ) {
        cameraMatrix.at<double>( 0, 0 ) = 1.0;
    }

    distCoeffs = Mat::zeros( 8, 1, CV_64F );

    vector<vector<Point3f> > objectPoints( 1 );
    calcBoardCornerPositions( s.boardSize, s.squareSize, objectPoints[0], s.calibrationPattern );

    objectPoints.resize( imagePoints.size(), objectPoints[0] );

    // Find intrinsic and extrinsic camera parameters
    double rms = calibrateCamera( objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, s.flag | CALIB_FIX_K4 | CALIB_FIX_K5 );

    cout << "Re-projection error reported by calibrateCamera: " << rms << endl;

    bool ok = checkRange( cameraMatrix ) && checkRange( distCoeffs );

    totalAvgErr = computeReprojectionErrors( objectPoints, imagePoints, rvecs, tvecs, cameraMatrix, distCoeffs, reprojErrs );

    return( ok );
}
Exemplo n.º 2
0
static int runCalibration( vector<vector<Point2f> > imagePoints,
                    Size imageSize, Size boardSize, Pattern patternType,
                    float squareSize, float aspectRatio,
                    int flags, Mat& cameraMatrix, Mat& distCoeffs,
                    vector<Mat>& rvecs, vector<Mat>& tvecs,
                    vector<float>& reprojErrs,
                    double& totalAvgErr)
{
    cameraMatrix = Mat::eye(3, 3, CV_64F);
    if( flags & CV_CALIB_FIX_ASPECT_RATIO )
        cameraMatrix.at<double>(0,0) = aspectRatio;

    distCoeffs = Mat::zeros(8, 1, CV_64F);

    vector<vector<Point3f> > objectPoints(1);
    calcChessboardCorners(boardSize, squareSize, objectPoints[0], patternType);

    objectPoints.resize(imagePoints.size(),objectPoints[0]);

    double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
                    distCoeffs, rvecs, tvecs, flags|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
                    ///*|CV_CALIB_FIX_K3*/|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
    printf("RMS error reported by calibrateCamera: %g\n", rms);

    bool ok = checkRange(cameraMatrix) && checkRange(distCoeffs);

    if(ok == false)
    	return -1;

    totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints,
                rvecs, tvecs, cameraMatrix, distCoeffs, reprojErrs);

    return 0;
}
bool CameraCalibrationAlgorithm::runCalibration
(
    VectorOfVectorOf2DPoints imagePoints,
    cv::Size imageSize,
    cv::Size boardSize,
    PatternType patternType,
    float squareSize,
    float aspectRatio,
    int flags,
    cv::Mat& cameraMatrix,
    cv::Mat& distCoeffs,
    VectorOfMat& rvecs,
    VectorOfMat& tvecs,
    std::vector<float>& reprojErrs,
    double& totalAvgErr
)
{

    cameraMatrix = cv::Mat::eye(3, 3, CV_64F);

    if (flags & cv::CALIB_FIX_ASPECT_RATIO)
    {
        cameraMatrix.at<double>(0, 0) = aspectRatio;
    }

    distCoeffs = cv::Mat::zeros(8, 1, CV_64F);

    VectorOfVectorOf3DPoints objectPoints(1);
    calcChessboardCorners(boardSize, squareSize, objectPoints[0], patternType);

    objectPoints.resize(imagePoints.size(), objectPoints[0]);

    //LOG_TRACE_MESSAGE("Object points size:" << objectPoints.size());
    //LOG_TRACE_MESSAGE("Image points size :" << imagePoints.size());
    //LOG_TRACE_MESSAGE("Image size        :" << imageSize);

    double rms = cv::calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
                                     distCoeffs, rvecs, tvecs, flags | cv::CALIB_FIX_K4 | cv::CALIB_FIX_K5);

    //LOG_TRACE_MESSAGE("RMS error reported by calibrateCamera: " << rms);

    bool ok = cv::checkRange(cameraMatrix) && cv::checkRange(distCoeffs);

    totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints, rvecs, tvecs, cameraMatrix, distCoeffs, reprojErrs);

    return ok;
}