以前工作的代码突然无法构建

时间:2015-07-20 19:42:11

标签: c++ opencv visual-studio-2013 camera-calibration

所以我一直在使用Visual Studio 2013中的OpenCV 3.0.0在C ++中使用这个相机校准代码。我遇到了一个错误,我不确定是由与我的代码相关的问题引起的还是编译器问题,所以我注释掉了我的大部分代码,创建了一个测试代码文件,构建/运行它,然后注释掉所有代码,取消注释原始代码并构建/运行它。

突然间,以前工作过的代码(ish)甚至都无法构建。我收到错误,例如“找不到lambda捕获变量”,Intellisense说我的所有变量都是未定义的,等等。我不知道为什么在以前我的代码构建和运行时发生了这种情况。我已经清理了项目和解决方案并删除了测试代码文件,但没有任何帮助。对于导致这种情况的任何想法?

代码如下所示,我得到的错误低于此。

    //Camera Calibration
    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    #include <time.h>
    #include <stdio.h>

    #include <opencv2/core.hpp>
    #include <opencv2/core/utility.hpp>
    #include <opencv2/imgproc.hpp>
    #include <opencv2/calib3d.hpp>
    #include <opencv2/imgcodecs.hpp>
    #include <opencv2/videoio.hpp>
    #include <opencv2/highgui.hpp>


    #ifndef _CRT_SECURE_NO_WARNINGS
    # define _CRT_SECURE_NO_WARNINGS
    #endif

    using namespace cv;
    using namespace std;

    static void help()
    {
        cout << "This is a camera calibration sample." << endl
            << "Usage: calibration configurationFile" << endl
            << "Near the sample file you'll find the configuration file, which has detailed help of "
            "how to edit it.  It may be any OpenCV supported file format     XML/YAML." << endl;
    }
    class Settings
    {
    public:
        Settings() : goodInput(false) {}
        enum Pattern { NOT_EXISTING, CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID };
        enum InputType { INVALID, CAMERA, VIDEO_FILE, IMAGE_LIST };

        void write(FileStorage& fs) const                        //Write serialization for this class
        {
            fs << "{"
            << "BoardSize_Width" << boardSize.width
            << "BoardSize_Height" << boardSize.height
            << "Square_Size" << squareSize
            << "Calibrate_Pattern" << patternToUse
            << "Calibrate_NrOfFrameToUse" << nrFrames
            << "Calibrate_FixAspectRatio" << aspectRatio
            << "Calibrate_AssumeZeroTangentialDistortion" << calibZeroTangentDist
            << "Calibrate_FixPrincipalPointAtTheCenter" << calibFixPrincipalPoint

            << "Write_DetectedFeaturePoints" << writePoints
            << "Write_extrinsicParameters" << writeExtrinsics
            << "Write_outputFileName" << outputFileName

            << "Show_UndistortedImage" << showUndistorsed

            << "Input_FlipAroundHorizontalAxis" << flipVertical
            << "Input_Delay" << delay
            << "Input" << input
            << "}";
    }
    void read(const FileNode& node)                          //Read serialization for this class
    {
        node["BoardSize_Width"] >> boardSize.width;
        node["BoardSize_Height"] >> boardSize.height;
        node["Calibrate_Pattern"] >> patternToUse;
        node["Square_Size"] >> squareSize;
        node["Calibrate_NrOfFrameToUse"] >> nrFrames;
        node["Calibrate_FixAspectRatio"] >> aspectRatio;
        node["Write_DetectedFeaturePoints"] >> writePoints;
        node["Write_extrinsicParameters"] >> writeExtrinsics;
        node["Write_outputFileName"] >> outputFileName;
        node["Calibrate_AssumeZeroTangentialDistortion"] >> calibZeroTangentDist;
        node["Calibrate_FixPrincipalPointAtTheCenter"] >> calibFixPrincipalPoint;
        node["Input_FlipAroundHorizontalAxis"] >> flipVertical;
        node["Show_UndistortedImage"] >> showUndistorsed;
        node["Input"] >> input;
        node["Input_Delay"] >> delay;
        validate();
    }
    void validate()
    {
        goodInput = true;
        if (boardSize.width <= 0 || boardSize.height <= 0)
        {
            cerr << "Invalid Board size: " << boardSize.width << " " << boardSize.height << endl;
            goodInput = false;
        }
        if (squareSize <= 10e-6)
        {
            cerr << "Invalid square size " << squareSize << endl;
            goodInput = false;
        }
        if (nrFrames <= 0)
        {
            cerr << "Invalid number of frames " << nrFrames << endl;
            goodInput = false;
        }

        if (input.empty())      // Check for valid input
            inputType = INVALID;
        else
        {
            if (input[0] >= '0' && input[0] <= '9')
            {
                stringstream ss(input);
                ss >> cameraID;
                inputType = CAMERA;
            }
            else
            {
                if (readStringList(input, imageList))
                {
                    inputType = IMAGE_LIST;
                    nrFrames = (nrFrames < (int)imageList.size()) ? nrFrames : (int)imageList.size();
                }
                else
                    inputType = VIDEO_FILE;
            }
            if (inputType == CAMERA)
                inputCapture.open(cameraID);
            if (inputType == VIDEO_FILE)
                inputCapture.open(input);
            if (inputType != IMAGE_LIST && !inputCapture.isOpened())
                inputType = INVALID;
        }
        if (inputType == INVALID)
        {
            cerr << " Input does not exist: " << input;
            goodInput = false;
        }

        flag = 0;
        if (calibFixPrincipalPoint) flag |= CALIB_FIX_PRINCIPAL_POINT;
        if (calibZeroTangentDist)   flag |= CALIB_ZERO_TANGENT_DIST;
        if (aspectRatio)            flag |= CALIB_FIX_ASPECT_RATIO;


        calibrationPattern = NOT_EXISTING;
        if (!patternToUse.compare("CHESSBOARD")) calibrationPattern = CHESSBOARD;
        if (!patternToUse.compare("CIRCLES_GRID")) calibrationPattern = CIRCLES_GRID;
        if (!patternToUse.compare("ASYMMETRIC_CIRCLES_GRID")) calibrationPattern = ASYMMETRIC_CIRCLES_GRID;
        if (calibrationPattern == NOT_EXISTING)
        {
            cerr << " Camera calibration mode does not exist: " << patternToUse << endl;
            goodInput = false;
        }
        atImageList = 0;

    }
    Mat nextImage()
    {
        Mat result;
        if (inputCapture.isOpened())
        {
            Mat view0;
            inputCapture >> view0;
            view0.copyTo(result);
        }
        else if (atImageList < imageList.size())
            result = imread(imageList[atImageList++], IMREAD_COLOR);

        return result;
    }

    static bool readStringList(const string& filename, vector<string>& l)
    {
        l.clear();
        FileStorage fs(filename, FileStorage::READ);
        if (!fs.isOpened())
            return false;
        FileNode n = fs.getFirstTopLevelNode();
        if (n.type() != FileNode::SEQ)
            return false;
        FileNodeIterator it = n.begin(), it_end = n.end();
        for (; it != it_end; ++it)
            l.push_back((string)*it);
        return true;
    }
public:
    Size boardSize;              // The size of the board -> Number of items by width and height
    Pattern calibrationPattern;  // One of the Chessboard, circles, or asymmetric circle pattern
    float squareSize;            // The size of a square in your defined unit (point, millimeter,etc).
    int nrFrames;                // The number of frames to use from the input for calibration
    float aspectRatio;           // The aspect ratio
    int delay;                   // In case of a video input
    bool writePoints;            // Write detected feature points
    bool writeExtrinsics;        // Write extrinsic parameters
    bool calibZeroTangentDist;   // Assume zero tangential distortion
    bool calibFixPrincipalPoint; // Fix the principal point at the center
    bool flipVertical;           // Flip the captured images around the horizontal axis
    string outputFileName;       // The name of the file where to write
    bool showUndistorsed;        // Show undistorted images after calibration
    string input;                // The input ->

    int cameraID;
    vector<string> imageList;
    size_t atImageList;
    VideoCapture inputCapture;
    InputType inputType;
    bool goodInput;
    int flag;

private:
    string patternToUse;


};

static inline void read(const FileNode& node, Settings& x, const Settings& default_value = Settings())
{
    if (node.empty())
        x = default_value;
    else
        x.read(node);
}

static inline void write(FileStorage& fs, const String&, const Settings& s)
{
    s.write(fs);
}

enum { DETECTION = 0, CAPTURING = 1, CALIBRATED = 2 };

bool runCalibrationAndSave(Settings& s, Size imageSize, Mat&  cameraMatrix, Mat& distCoeffs,
    vector<vector<Point2f> > imagePoints);

bool actually_findChessboardCorners(Mat& frame, Size& size, Mat& corners, int flags);

double actually_calibrateCamera(vector<vector<Point3f>> _objectPoints,
    vector<vector<Point2f>> _imagePoints,
    Size imageSize, InputOutputArray _cameraMatrix, InputOutputArray _distCoeffs,
    OutputArrayOfArrays _rvecs, OutputArrayOfArrays _tvecs, int flags, TermCriteria criteria);

static Mat prepareDistCoeffs(Mat& distCoeffs0, int rtype);
static Mat prepareCameraMatrix(Mat& cameraMatrix0, int rtype);
static void actually_collectCalibrationData(vector<vector<Point3f>> objectPoints,
    vector<vector<Point2f>> imagePoints1,
    InputArrayOfArrays imagePoints2,
    Mat& objPtMat, Mat& imgPtMat1, Mat* imgPtMat2,
    Mat& npoints);

int main(int argc, char* argv[])
{
    help();

    ![file_read]
        Settings s;
    const string inputSettingsFile = argc > 1 ? argv[1] : "default.xml";
    FileStorage fs(inputSettingsFile, FileStorage::READ); // Read the settings
    if (!fs.isOpened())
    {
        cout << "Could not open the configuration file: \"" << inputSettingsFile << "\"" << endl;
        return -1;
    }
    fs["Settings"] >> s;
    fs.release();                                         // close Settings file
    ![file_read]

        FileStorage fout("settings.yml", FileStorage::WRITE); // write config as YAML
    fout << "Settings" << s;

    if (!s.goodInput)
    {
        cout << "Invalid input detected. Application stopping. " << endl;
        return -1;
    }

    vector<vector<Point2f>> imagePoints;
    Mat cameraMatrix, distCoeffs;
    Size imageSize;
    int mode = s.inputType == Settings::IMAGE_LIST ? CAPTURING : DETECTION;
    clock_t prevTimestamp = 0;
    const Scalar RED(0, 0, 255), GREEN(0, 255, 0);
    const char ESC_KEY = 27;

    ![get_input]
        for (int i = 0;; ++i)
        {
            Mat view_large;
            Mat view;
            bool blinkOutput = false;

            view_large = s.nextImage();
            if (!view_large.empty())
                resize(view_large, view, Size(), .5, .5);

            ---- - If no more image, or got enough, then stop calibration and show result------------ -
                if (mode == CAPTURING && imagePoints.size() >= (size_t)s.nrFrames)
                {
                    if (runCalibrationAndSave(s, imageSize, cameraMatrix, distCoeffs, imagePoints))
                        mode = CALIBRATED;
                    else
                        mode = DETECTION;
                }
            if (view.empty())          // If there are no more images stop the loop
            {
                if calibration threshold was not reached yet, calibrate now
                    if (mode != CALIBRATED && !imagePoints.empty())
                        runCalibrationAndSave(s, imageSize, cameraMatrix, distCoeffs, imagePoints);
                break;
            }
            ![get_input]

                imageSize = view.size();  // Format input image.
            if (s.flipVertical)    flip(view, view, 0);

            ![find_pattern]
                Mat pointBuf;
            vector<Point2f> corners;
            bool found;
            switch (s.calibrationPattern) // Find feature points on the input format
            {
            case Settings::CHESSBOARD:
                found = actually_findChessboardCorners(view, s.boardSize, pointBuf,
                    CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE);

                break;
            case Settings::CIRCLES_GRID:
                found = findCirclesGrid(view, s.boardSize, pointBuf);
                break;
            case Settings::ASYMMETRIC_CIRCLES_GRID:
                found = findCirclesGrid(view, s.boardSize, pointBuf, CALIB_CB_ASYMMETRIC_GRID);
                break;
            default:
                found = false;
                break;
            }
            ![find_pattern]
                ![pattern_found]
                if (found)                // If done with success,
                {
                    improve the found corners' coordinate accuracy for chessboard
                        if (s.calibrationPattern == Settings::CHESSBOARD)
                        {
                            Mat viewGray;
                            cvtColor(view, viewGray, COLOR_BGR2GRAY);
                            cornerSubPix(viewGray, pointBuf, Size(11, 11),
                                Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1));
                            corners.assign((Point2f*)pointBuf.datastart, (Point2f*)pointBuf.dataend);
                            corners.resize(54);

                        }

                    if (mode == CAPTURING &&  // For camera only take new samples after delay time
                        (!s.inputCapture.isOpened() || clock() - prevTimestamp > s.delay*1e-3*CLOCKS_PER_SEC))
                    {
                        imagePoints.push_back(corners);
                        prevTimestamp = (long)clock();
                        blinkOutput = s.inputCapture.isOpened();
                    }

                    Draw the corners.
                        drawChessboardCorners(view, s.boardSize, pointBuf, found);
                }
            ![pattern_found]
                ---------------------------- - Output Text------------------------------------------------
                ![output_text]
                string msg = (mode == CAPTURING) ? "100/100" :
                mode == CALIBRATED ? "Calibrated" : "Press 'g' to start";
            int baseLine = 0;
            Size textSize = getTextSize(msg, 1, 1, 1, &baseLine);
            Point textOrigin(view.cols - 2 * textSize.width - 10, view.rows - 2 * baseLine - 10);

            if (mode == CAPTURING)
            {
                if (s.showUndistorsed)
                    msg = format("%d/%d Undist", (int)imagePoints.size(), s.nrFrames);
                else
                    msg = format("%d/%d", (int)imagePoints.size(), s.nrFrames);
            }

            putText(view, msg, textOrigin, 1, 1, mode == CALIBRATED ? GREEN : RED);

            if (blinkOutput)
                bitwise_not(view, view);
            ![output_text]
                ------------------------ - Video capture  output  undistorted------------------------------
                ![output_undistorted]
                if (mode == CALIBRATED && s.showUndistorsed)
                {
                    Mat temp = view.clone();
                    undistort(temp, view, cameraMatrix, distCoeffs);
                }
            ![output_undistorted]
                ------------------------------Show image and check for input commands------------------ -
                ![await_input]
                imshow("Image View", view);
            char key = (char)waitKey(s.inputCapture.isOpened() ? 50 : s.delay);

            if (key == ESC_KEY)
                break;

            if (key == 'u' && mode == CALIBRATED)
                s.showUndistorsed = !s.showUndistorsed;

            if (s.inputCapture.isOpened() && key == 'g')
            {
                mode = CAPTURING;
                imagePoints.clear();
            }
            ![await_input]
        }

    ---------------------- - Show the undistorted image for the image list------------------------
        ![show_results]
        if (s.inputType == Settings::IMAGE_LIST && s.showUndistorsed)
        {
            Mat view, rview, map1, map2;
            initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
                getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),
                imageSize, CV_16SC2, map1, map2);

            for (size_t i = 0; i < s.imageList.size(); i++)
            {
                view = imread(s.imageList[i], 1);
                if (view.empty())
                    continue;
                remap(view, rview, map1, map2, INTER_LINEAR);
                imshow("Image View", rview);
                char c = (char)waitKey();
                if (c == ESC_KEY || c == 'q' || c == 'Q')
                    break;
            }
        }
    ![show_results]

        return 0;
}

![compute_errors]
static 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)
{
    vector<Point2f> imagePoints2;
    size_t totalPoints = 0;
    double totalErr = 0, err;
    perViewErrors.resize(objectPoints.size());

    for (size_t i = 0; i < objectPoints.size(); ++i)
    {
        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);
}
![compute_errors]
![board_corners]
static void calcBoardCornerPositions(Size boardSize, float squareSize, vector<Point3f>& corners,
Settings::Pattern patternType /*= Settings::CHESSBOARD*/)
{
    corners.clear();
    corners.reserve(54);
    switch (patternType)
    {
    case Settings::CHESSBOARD:
    case Settings::CIRCLES_GRID:
        for (int i = 0; i < boardSize.height; ++i)
            for (int j = 0; j < boardSize.width; ++j)
                corners.push_back(Point3f(j*squareSize, i*squareSize, 0.0f));
        break;

    case Settings::ASYMMETRIC_CIRCLES_GRID:
        for (int i = 0; i < boardSize.height; i++)
            for (int j = 0; j < boardSize.width; j++)
                corners.push_back(Point3f((2 * j + i % 2)*squareSize, i*squareSize, 0));
        break;
    default:
        break;
    }
}
![board_corners]
static bool runCalibration(Settings& s, Size& imageSize, Mat& cameraMatrix, Mat& distCoeffs,
vector<vector<Point2f>> imagePoints, vector<Mat>& rvecs, vector<Mat>& tvecs,
vector<float>& reprojErrs, double& totalAvgErr)
{
    ![fixed_aspect]
        cameraMatrix = Mat::eye(3, 3, CV_64F);
    if (s.flag & CALIB_FIX_ASPECT_RATIO)
        cameraMatrix.at<double>(0, 0) = s.aspectRatio;
    ![fixed_aspect]

        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]);
    objectPoints.resize(imagePoints.size());

    TermCriteria term;
    double rms = actually_calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
        distCoeffs, rvecs, tvecs, s.flag | CALIB_FIX_K4 | CALIB_FIX_K5, term);

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

Print camera parameters to the output file
static void saveCameraParams(Settings& s, Size& imageSize, Mat& cameraMatrix, Mat& distCoeffs,
const vector<Mat>& rvecs, const vector<Mat>& tvecs,
const vector<float>& reprojErrs, const vector<vector<Point2f>>& imagePoints,
double totalAvgErr)
{
    FileStorage fs(s.outputFileName, FileStorage::WRITE);

    time_t tm;
    time(&tm);
    struct tm *t2 = localtime(&tm);
    char buf[1024];
    strftime(buf, sizeof(buf), "%c", t2);

    fs << "calibration_time" << buf;

    if (!rvecs.empty() || !reprojErrs.empty())
        fs << "nr_of_frames" << (int)std::max(rvecs.size(), reprojErrs.size());
    fs << "image_width" << imageSize.width;
    fs << "image_height" << imageSize.height;
    fs << "board_width" << s.boardSize.width;
    fs << "board_height" << s.boardSize.height;
    fs << "square_size" << s.squareSize;

    if (s.flag & CALIB_FIX_ASPECT_RATIO)
        fs << "fix_aspect_ratio" << s.aspectRatio;

    if (s.flag)
    {
        sprintf(buf, "flags: %s%s%s%s",
            s.flag & CALIB_USE_INTRINSIC_GUESS ? " +use_intrinsic_guess" : "",
            s.flag & CALIB_FIX_ASPECT_RATIO ? " +fix_aspect_ratio" : "",
            s.flag & CALIB_FIX_PRINCIPAL_POINT ? " +fix_principal_point" : "",
            s.flag & CALIB_ZERO_TANGENT_DIST ? " +zero_tangent_dist" : "");
        cvWriteComment(*fs, buf, 0);
    }

    fs << "flags" << s.flag;

    fs << "camera_matrix" << cameraMatrix;
    fs << "distortion_coefficients" << distCoeffs;

    fs << "avg_reprojection_error" << totalAvgErr;
    if (s.writeExtrinsics && !reprojErrs.empty())
        fs << "per_view_reprojection_errors" << Mat(reprojErrs);

    if (s.writeExtrinsics && !rvecs.empty() && !tvecs.empty())
    {
        CV_Assert(rvecs[0].type() == tvecs[0].type());
        Mat bigmat((int)rvecs.size(), 6, rvecs[0].type());
        for (size_t i = 0; i < rvecs.size(); i++)
        {
            Mat r = bigmat(Range(int(i), int(i + 1)), Range(0, 3));
            Mat t = bigmat(Range(int(i), int(i + 1)), Range(3, 6));

            CV_Assert(rvecs[i].rows == 3 && rvecs[i].cols == 1);
            CV_Assert(tvecs[i].rows == 3 && tvecs[i].cols == 1);
            *.t() is MatExpr(not Mat) so we can use assignment operator
                r = rvecs[i].t();
            t = tvecs[i].t();
        }
        cvWriteComment(*fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0);
        fs << "extrinsic_parameters" << bigmat;
    }

    if (s.writePoints && !imagePoints.empty())
    {
        Mat imagePtMat((int)imagePoints.size(), (int)imagePoints[0].size(), CV_32FC2);
        for (size_t i = 0; i < imagePoints.size(); i++)
        {
            Mat r = imagePtMat.row(int(i)).reshape(2, imagePtMat.cols);
            Mat imgpti(imagePoints[i]);
            imgpti.copyTo(r);
        }
        fs << "image_points" << imagePtMat;
    }
}

![run_and_save]
bool 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;
}
bool actually_findChessboardCorners(Mat& frame, Size& size, Mat& corners, int flags) {
    int count = size.area() * 2;
    corners.create(count, 1, CV_32FC2);
    CvMat _image = frame;
    bool ok = cvFindChessboardCorners(&_image, size,
        reinterpret_cast<CvPoint2D32f*>(corners.data),
        &count, flags) > 0;
    return ok;
}

![run_and_save]
double actually_calibrateCamera(vector<vector<Point3f>> _objectPoints,
vector<vector<Point2f>> _imagePoints,
Size imageSize, InputOutputArray _cameraMatrix, InputOutputArray _distCoeffs,
OutputArrayOfArrays& _rvecs, OutputArrayOfArrays& _tvecs, int flags, TermCriteria criteria)
{
    int rtype = CV_64F;
    Mat cameraMatrix = _cameraMatrix.getMat();
    cameraMatrix = prepareCameraMatrix(cameraMatrix, rtype);
    Mat distCoeffs = _distCoeffs.getMat();
    distCoeffs = prepareDistCoeffs(distCoeffs, rtype);
    if (!(flags & CALIB_RATIONAL_MODEL) && (!(flags & CALIB_THIN_PRISM_MODEL)))
        distCoeffs = distCoeffs.rows == 1 ? distCoeffs.colRange(0, 5) : distCoeffs.rowRange(0, 5);

    int    i;
    size_t nimages = _objectPoints.size();
    CV_Assert(nimages > 0);
    Mat objPt, imgPt, npoints, rvecM((int)nimages, 3, CV_64FC1), tvecM((int)nimages, 3, CV_64FC1);
    actually_collectCalibrationData(_objectPoints, _imagePoints, noArray(),
        objPt, imgPt, 0, npoints);
    CvMat c_objPt = objPt, c_imgPt = imgPt, c_npoints = npoints;
    CvMat c_cameraMatrix = cameraMatrix, c_distCoeffs = distCoeffs;
    CvMat c_rvecM = rvecM, c_tvecM = tvecM;

    double reprojErr = cvCalibrateCamera2(&c_objPt, &c_imgPt, &c_npoints, imageSize,
        &c_cameraMatrix, &c_distCoeffs, &c_rvecM,
        &c_tvecM, flags, criteria);

    bool rvecs_needed = _rvecs.needed(), tvecs_needed = _tvecs.needed();

    if (rvecs_needed)
        _rvecs.create((int)nimages, 1, CV_64FC3);
    if (tvecs_needed)
        _tvecs.create((int)nimages, 1, CV_64FC3);

    for (i = 0; i < (int)nimages; i++)
    {
        if (rvecs_needed)
        {
            _rvecs.create(3, 1, CV_64F, i, true);
            Mat rv = _rvecs.getMat(i);
            memcpy(rv.ptr(), rvecM.ptr<double>(i), 3 * sizeof(double));
        }
        if (tvecs_needed)
        {
            _tvecs.create(3, 1, CV_64F, i, true);
            Mat tv = _tvecs.getMat(i);
            memcpy(tv.ptr(), tvecM.ptr<double>(i), 3 * sizeof(double));
        }
    }
    cameraMatrix.copyTo(_cameraMatrix);
    distCoeffs.copyTo(_distCoeffs);

    return reprojErr;
}
static void actually_collectCalibrationData(vector<vector<Point3f>> objectPoints,
    vector<vector<Point2f>> imagePoints1,
    InputArrayOfArrays imagePoints2,
    Mat& objPtMat, Mat& imgPtMat1, Mat* imgPtMat2,
    Mat& npoints)
{
    int nimages = (int)objectPoints.size();
    int i, j = 0, ni = 0, total = 0;
    CV_Assert(nimages > 0 && nimages == (int)imagePoints1.size() &&
        (!imgPtMat2 || nimages == (int)imagePoints2.total()));

    for (i = 0; i < nimages; i++)
    {
        ni = Mat(objectPoints[i]).checkVector(3, CV_32F);
        if (ni <= 0)
            CV_Error(CV_StsUnsupportedFormat, "objectPoints should contain vector of vectors of points of type Point3f");
        int ni1 = Mat(imagePoints1[i]).checkVector(2, CV_32F);
        if (ni1 <= 0)
            CV_Error(CV_StsUnsupportedFormat, "imagePoints1 should contain vector of vectors of points of type Point2f");
        CV_Assert(ni == ni1);

        total += ni;
    }

    npoints.create(1, (int)nimages, CV_32S);
    objPtMat.create(1, (int)total, CV_32FC3);
    imgPtMat1.create(1, (int)total, CV_32FC2);
    Point2f* imgPtData2 = 0;

    if (imgPtMat2)
    {
        imgPtMat2->create(1, (int)total, CV_32FC2);
        imgPtData2 = imgPtMat2->ptr<Point2f>();
    }

    Point3f* objPtData = objPtMat.ptr<Point3f>();
    Point2f* imgPtData1 = imgPtMat1.ptr<Point2f>();

    for (i = 0; i < nimages; i++, j += ni)
    {
        Mat objpt = Mat(objectPoints[i]);
        Mat imgpt1 = Mat(imagePoints1[i]);
        ni = objpt.checkVector(3, CV_32F);
        npoints.at<int>(i) = ni;
        memcpy(objPtData + j, objpt.ptr(), ni*sizeof(objPtData[0]));
        memcpy(imgPtData1 + j, imgpt1.ptr(), ni*sizeof(imgPtData1[0]));

        if (imgPtData2)
        {
            Mat imgpt2 = imagePoints2.getMat(i);
            int ni2 = imgpt2.checkVector(2, CV_32F);
            CV_Assert(ni == ni2);
            memcpy(imgPtData2 + j, imgpt2.ptr(), ni*sizeof(imgPtData2[0]));
        }
    }
}
static Mat prepareCameraMatrix(Mat& cameraMatrix0, int rtype)
{
    Mat cameraMatrix = Mat::eye(3, 3, rtype);
    if (cameraMatrix0.size() == cameraMatrix.size())
        cameraMatrix0.convertTo(cameraMatrix, rtype);
    return cameraMatrix;
}

static Mat prepareDistCoeffs(Mat& distCoeffs0, int rtype)
{
    Mat distCoeffs = Mat::zeros(distCoeffs0.cols == 1 ? Size(1, 12) : Size(12, 1), rtype);
    if (distCoeffs0.size() == Size(1, 4) ||
        distCoeffs0.size() == Size(1, 5) ||
        distCoeffs0.size() == Size(1, 8) ||
        distCoeffs0.size() == Size(1, 12) ||
        distCoeffs0.size() == Size(4, 1) ||
        distCoeffs0.size() == Size(5, 1) ||
        distCoeffs0.size() == Size(8, 1) ||
        distCoeffs0.size() == Size(12, 1))
    {
        Mat dstCoeffs(distCoeffs, Rect(0, 0, distCoeffs0.cols, distCoeffs0.rows));
        distCoeffs0.convertTo(dstCoeffs, rtype);
    }
    return distCoeffs;
}

错误:

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

在尝试追踪原始错误的过程中,很可能是您无意中更改了代码或构建文件。使用版本控制将代码恢复到已知良好状态。你确实有版本控制,不是吗?如果你有选择,我会建议Git,或Mercurial。

如果您确定没有无意中更改代码或构建文件,请检查您的编译器版本或工具是否已更改。如果你的代码没有改变,你的工具链没有改变,那就追求更多奇特的理论。