findChessboardCorners() gives False in OpenCV 4.1.2 C++

asked 2019-11-18 12:38:05 -0600

TheEMcoder gravatar image

updated 2019-11-19 09:36:21 -0600

Hi everybody, I want to calibrate my camera for getting distance from camera to object. For it as I read in OpenCV Docs and other places I need to do test->findChessboardCorners(). For it I use C++,Opencv 4.1.2(.hpp library) and camera(on Computer). And as Chessboard imge I use Chessboard. And it get this image from camera and says me FALSE; Size of chessboard is (8x4) , but I use (7x3). I have tried to give or not give flags CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_FILTER_QUADS or CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FILTER_QUADS; Nothing changed. I've tried to give the non-gray color image; Nothing changed too.I don't know what to do..... Please,answer my question,plzzzzzzzzzzzzzzzzzzzzz...

Code:

VideoCapture vc(0);
int numBoards = 0;
int numCornersHor;
int numCornersVer;
printf("Enter number of corners along width: ");
scanf("%d", &numCornersHor);

printf("Enter number of corners along height: ");
scanf("%d", &numCornersVer);

printf("Enter number of boards: ");
scanf("%d", &numBoards);


int numSquares = numCornersHor * numCornersVer;

Size board_sz = Size(numCornersHor, numCornersVer);

int numSquares = numCornersHor * numCornersVer;

Size board_sz = Size(numCornersHor, numCornersVer);

vector<vector<Point3f>> object_points;
vector<vector<Point2f>> image_points;
vector<Point2f> corners;
int successes = 0; 
Mat image;
Mat gray_image;
vc >> image;


vector<Point3f> obj;
for (int j = 0; j < numSquares; j++)
    obj.push_back(Point3f(j / numCornersHor, j % numCornersHor, 0.0f));
while (successes < numBoards)
{
    if (image.empty())break;
    cvtColor(image, gray_image, COLOR_BGR2GRAY);


    bool found = findChessboardCorners(gray_image, board_sz, corners, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FILTER_QUADS);

    if (found)
    {
        cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 30, 0.1));
        drawChessboardCorners(gray_image, board_sz, corners, found);

    }
    printf("%d", corners.size());
    imshow("win1", image);
    imshow("win2", gray_image);
    if (image.empty())break;
    vc >> 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, image.size(), intrinsic, distCoeffs, rvecs, tvecs);
Mat imageUndistorted;
while (1)
{
    if (image.empty())break;
vc>> image;
    undistort(image, imageUndistorted, intrinsic, distCoeffs);

    imshow("win1", image);
    imshow("win2", imageUndistorted);
    waitKey(1);
}
edit retag flag offensive close merge delete