CalibrateCamera Windows Memory Exception

asked 2016-10-28 17:01:05 -0600

CharlesB gravatar image

I'm trying to calibrate my video camera feed with the chessboard functions. calibratecamera and undistort functions but it always crashes on calibrate camera with a windows memory exception after viewing, displaying and saving the chesboard corners. Here is the code below:

using namespace cv;
using namespace std;
int main()
{
    Mat img = imread("TestChessboard.png", CV_LOAD_IMAGE_UNCHANGED);
    int numBoards = 1;
    float numCornersHor = 6;
    float numCornersVer = 5;

int numSquares = (int)numCornersHor * (int)numCornersVer;
Size board_sz = Size((int)numCornersHor, (int)numCornersVer);

VideoCapture capture = VideoCapture(0);

vector<vector<Point3f>> object_points;
vector<vector<Point2f>> image_points;

vector<Point2f> corners;
int successes = 0;

Mat image;
Mat gray_image;

vector<Point3f> obj;
for (int j = 0;j < numSquares;j++)
    obj.push_back(Point3f((float)floor(j / numCornersHor), (float)(j % (int)numCornersHor), 0.0f));


while (successes < numBoards)
{
    capture >> image;
    //cvtColor(image, gray_image, CV_BGR2GRAY);
    //cvtColor(img, gray_image, CV_BGR2GRAY);
    int kernel_size = 3;
    bool found = findChessboardCorners(image, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);

    if (found)
    {
        cornerSubPix(image, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
        drawChessboardCorners(image, board_sz, corners, found);
    }

    imshow("win", gray_image);

    int key = waitKey(1);

    if (key == 27)

        return 0;

    if (key == ' ' && found != 0)
    {

        image_points.push_back(corners);
        object_points.push_back(obj);

        printf("Snap stored!");

        successes++;

        if (successes >= numBoards)
            break;
    }
}

Mat intrinsic = Mat(3, 3, CV_32FC1);
Mat distCoeffs;
vector<Mat> rvecs;
vector<Mat> tvecs;

intrinsic.ptr<float>(0)[0] = 1;
intrinsic.ptr<float>(1)[1] = 1;

calibrateCamera(object_points, image_points, Size(image.rows, image.cols), intrinsic, distCoeffs, rvecs, tvecs);




Mat imageUndistorted;
while (1)
{
    capture >> image;
    undistort(image, imageUndistorted, intrinsic, distCoeffs);

    imshow("win1", image);
    imshow("win", image);
    waitKey(1);
}

capture.release();

return 0;
}

Which returns the error (at different memory locations every time, don't know if that's relevant):

Exception thrown at 0x000007FEFD47A06D in OpenCVC++.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000000002A8190.

Help would be very much appreciated!

edit retag flag offensive close merge delete