Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV CalibleCamera()返回过高的重新投影错误(C+;+;)_C++_Opencv - Fatal编程技术网

C++ OpenCV CalibleCamera()返回过高的重新投影错误(C+;+;)

C++ OpenCV CalibleCamera()返回过高的重新投影错误(C+;+;),c++,opencv,C++,Opencv,我尝试校准Iphone 11 Pro摄像头,openCV calibrateCamera()函数返回的错误超过1.2+(与下面的ComputerProjectionErrors()相同)。我希望它们低于0.5,因为在这之后,我正在制作视差图,我需要它是平滑的。我怎样才能最小化它?这是我的代码,我从不同角度制作了90张棋盘图片,这些图片存储在../chess\u图像中。下面是代码:github.com/markilebyak/opencv.git 这里还有:4shared.com/folder/P

我尝试校准Iphone 11 Pro摄像头,openCV calibrateCamera()函数返回的错误超过1.2+(与下面的ComputerProjectionErrors()相同)。我希望它们低于0.5,因为在这之后,我正在制作视差图,我需要它是平滑的。我怎样才能最小化它?这是我的代码,我从不同角度制作了90张棋盘图片,这些图片存储在../chess\u图像中。下面是代码:github.com/markilebyak/opencv.git

这里还有:4shared.com/folder/PSDaeTcI/opencv.html是我使用的90幅图像中的36幅图像,以及36幅图像中检测到的线条的34幅灰度图

void createKnownBoardPosition(Size &boardSize, float squareEdgeLength, vector<Point3f>& corners) {
    for (int i = 0; i < boardSize.height; i++) {
        for (int j = 0; j < boardSize.width; j++) {
            corners.emplace_back((float)j * squareEdgeLength, (float)i * squareEdgeLength, 0.0f);
        }
    }
}



void process_images(std::string &path_to_directory, Size &chessboard_size, vector<vector<Point2f>>& allFoundCorners) {
//    Function to iterate through images
//    taken for camera calibration
    vector<String> filenames;
    cv::glob(path_to_directory, filenames);
    vector<Point2f> pointBuffer;
    int counter = 0;
    for (const auto & filename : filenames) {
        if (filename != "../chess_images/.DS_Store"){
            Mat im = imread(filename);
            std::cout << filename << std::endl;
            Mat gray_im;
            cvtColor(im, gray_im, COLOR_BGR2GRAY);
            bool found = findChessboardCorners(gray_im, chessboard_size, pointBuffer,
                                               CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE);
            if (found) {
                counter++;
                TermCriteria criteria = TermCriteria( TermCriteria::EPS + TermCriteria::MAX_ITER,
                        30,0.0001 );
                Size winSize = Size( 11, 11);
                Size zeroZone = Size( -1, -1 );
                //cornerSubPix is the algorithm focused on relocating the points. it receives the image, the corners
                // a window size, zeroZone and the actual criteria. The window size is the search area.
                cornerSubPix(gray_im, pointBuffer, winSize, zeroZone, criteria );
                drawChessboardCorners( gray_im, chessboard_size, Mat(pointBuffer), found );
                string nametext = "../drawn_chessboard/" + to_string(counter) + ".jpg";
                imwrite(nametext, gray_im);
                allFoundCorners.push_back(pointBuffer);
            }
        }
    }
    std::cout << "Images used: " << counter << std::endl;
}


double computeReprojectionErrors( const vector<vector<Point3f> >& objectPoints,
                                         const vector<vector<Point2f> >& imagePoints,
                                         const vector<Mat>& rvecs, const vector<Mat>& tvecs,
                                         const Mat& cameraMatrix , const Mat& distCoeffs,
                                         vector<float>& perViewErrors, bool fisheye) {
    vector<Point2f> imagePoints2;
    size_t totalPoints = 0;
    double totalErr = 0, err;
    perViewErrors.resize(objectPoints.size());
    for(size_t i = 0; i < objectPoints.size(); ++i )
    {
        if (fisheye)
        {
            fisheye::projectPoints(objectPoints[i], imagePoints2, rvecs[i], tvecs[i], cameraMatrix,
                                   distCoeffs);
        }
        else
        {
            projectPoints(objectPoints[i], rvecs[i], tvecs[i], cameraMatrix, distCoeffs, imagePoints2);
        }
        err = norm(imagePoints[i], imagePoints2, NORM_L2);
        size_t n = objectPoints[i].size();
        perViewErrors[i] = (float) std::sqrt(err*err/n);
        totalErr        += err*err;
        totalPoints     += n;
    }
    return std::sqrt(totalErr/totalPoints);
}


void calibration_process(Size &boardSize, float &squareEdgeLength, Mat& cameraMatrix, Mat& distortionCoefficients,
                         std::string &path_to_directory) {

    vector<vector<Point2f>> checkerboardImageSpacePoints;
    process_images(path_to_directory, boardSize, checkerboardImageSpacePoints);

    vector<vector<Point3f>> worldSpaceCornerPoints(1);
    createKnownBoardPosition(boardSize, squareEdgeLength, worldSpaceCornerPoints[0]);
    worldSpaceCornerPoints.resize(checkerboardImageSpacePoints.size(), worldSpaceCornerPoints[0]);
    //rotation and translation vectors (rVectors, tVectors)
    vector<Mat> rVectors, tVectors;
    distortionCoefficients = Mat::zeros(8, 1, CV_64F);
    calibrateCamera(worldSpaceCornerPoints, checkerboardImageSpacePoints, boardSize, cameraMatrix,
                                 distortionCoefficients, rVectors, tVectors, CALIB_FIX_K5 + CALIB_FIX_INTRINSIC);
    vector<float> perViewErrors;
    double rep_error = computeReprojectionErrors(worldSpaceCornerPoints, checkerboardImageSpacePoints, rVectors, tVectors, cameraMatrix,
            distortionCoefficients, perViewErrors, false);
    std::cout << "Reprojection Error: " << rep_error << std::endl;
}


int main() {
    Mat cameraMatrix, distortionCoefficient;
    Size chessboardDimensions = Size(6, 9);
    float calibrationSquareDimension = 0.03f;
    std::string path = "../chess_images";
    calibration_process(chessboardDimensions, calibrationSquareDimension, cameraMatrix,
                        distortionCoefficient, path);
    return 0;
}
void createKnownBoardPosition(大小和板大小、浮点平方长度、向量和角点){
对于(int i=0;istd::你能显示图像和检测到的棋盘吗?@Micka我如何与你联系?你可以上传到文件托管网站并链接它们here@Micka下面是36张校准图片(彩色)和34张灰度图片,在这些图片上可以找到ChessboardCorners()返回true:@mika谢谢)过一段时间,我会告诉你我取得了什么成就。你能显示图像和检测到的棋盘吗?@mika我如何与你联系?你可以上传到文件托管网站并链接它们here@Micka下面是36张校准图片(彩色)和34张灰度图片,在这些图片上可以找到ChessboardCorners()返回真:@Mika谢谢你)过些时候,我会告诉你我取得了什么成就。