bool CameraCalibration::runCalibrationAndSave( Settings& s, Size imageSize, Mat&  cameraMatrix, Mat& distCoeffs,vector<vector<Point2f> > imagePoints )
{
    vector<Mat> rvecs, tvecs;
    vector<float> reprojErrs;
    double totalAvgErr = 0;

    bool ok = runCalibration( s,imageSize, cameraMatrix, distCoeffs, imagePoints, rvecs, tvecs, reprojErrs, totalAvgErr);
    cout << ( ok ? "Calibration succeeded" : "Calibration failed" ) << ". avg re projection error = "  << totalAvgErr << endl;

    if ( ok ) {
        saveCameraParams( s, imageSize, cameraMatrix, distCoeffs, rvecs ,tvecs, reprojErrs, imagePoints, totalAvgErr );
    }
    return( ok );
}
Beispiel #2
0
/** Runs and saves file. -------------------------------------------------------------------------*/
bool runAndSave(const string& outputFilename,
                const vector<vector<Point2f> >& imagePoints,
                Size imageSize, Size boardSize, float squareSize,
                float aspectRatio, int flags, Mat& cameraMatrix,
                Mat& distCoeffs, bool writeExtrinsics, bool writePoints, int qr_size_px )
{
    vector<Mat> rvecs, tvecs;
    vector<float> reprojErrs;
    double totalAvgErr = 0;
    
    // QR code dependant parameters
    int qr_size_mm;
    int known_distance;
    cout << "QR code size (mm): ";
    cin >> qr_size_mm;
    cout << "QR code distance from source (mm): ";
    cin >> known_distance;
    int scale_factor = known_distance * qr_size_px / qr_size_mm;
    
    
    bool ok = runCalibration(imagePoints, imageSize, boardSize, squareSize,
                   aspectRatio, flags, cameraMatrix, distCoeffs,
                   rvecs, tvecs, reprojErrs, totalAvgErr);
    printf("%s. avg reprojection error = %.2f\n",
           ok ? "Calibration succeeded" : "Calibration failed",
           totalAvgErr);
    
    if(ok)
        saveCameraParams( outputFilename, imageSize,
                         boardSize, squareSize, aspectRatio,
                         flags, cameraMatrix, distCoeffs,
                         writeExtrinsics ? rvecs : vector<Mat>(),
                         writeExtrinsics ? tvecs : vector<Mat>(),
                         writeExtrinsics ? reprojErrs : vector<float>(),
                         writePoints ? imagePoints : vector<vector<Point2f> >(),
                         totalAvgErr, scale_factor, qr_size_mm );
    return ok;
}
void QCvCameraCalibration::calibrate()
{
    this->setEnable(false);
    QString msg;
    if (imagePoints.size() >= s.minNumSamples)
    {
        // compute board geometry
        vector<vector<cv::Point3f> > objectPoints(1);
        calcBoardCornerPositions(s.boardSize, s.squareSize, objectPoints[0]);
        objectPoints.resize(imagePoints.size(),objectPoints[0]);
        rmsError = calibrateCamera(objectPoints, imagePoints, imgSize, cameraMatrix,
                                    distCoeffs, rvecs, tvecs, CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);

        msg = "updated object points successfully, RMS = "+QString::number(rmsError);
        s.bCALIBRATED=true;
        saveCameraParams();
    }
    else
    {
        msg = "Insufficient number of samples taken!";
    }
    emit statusUpdate(msg);
    this->setEnable(true);
}