Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Stereo calibration using findChessboardCorners

I try to calculate 3D position from stereo camera using epipolar line. I want to calibrate my cameras. I use below code, but the findChessboardCorners never returned the true value.What can I do? Is there an logical error in this code?

int board_w = 9;//atoi(argv[2]);
int board_h = 7;//atoi(argv[3]);

Size board_sz = Size(board_w, board_h);
int board_n = board_w*board_h;

vector<vector<Point3f> > object_points;
vector<vector<Point2f> > imagePoints1, imagePoints2;
vector<Point2f> corners1, corners2;

vector<Point3f> obj;
for (int j=0; j<board_n; j++)
{
    obj.push_back(Point3f(j/board_w, j%board_w, 0.0f));
}

Mat img1, img2, gray1, gray2;
VideoCapture cap1 = VideoCapture(1);
VideoCapture cap2 = VideoCapture(2);

cap1.set(CV_CAP_PROP_FRAME_WIDTH, 300);
cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 300);
cap2.set(CV_CAP_PROP_FRAME_WIDTH, 300);
cap2.set(CV_CAP_PROP_FRAME_HEIGHT, 300);

int k = 0;
bool found1 = false, found2 = false;
bool success = false;
while (!(found1==true && found2==true))
{
    std::cout << "cap frame\n";
    cap1 >> img1;
    cap2 >> img2;

cvtColor(img1, gray1, CV_BGR2GRAY);
cvtColor(img2, gray2, CV_BGR2GRAY);

found1 = findChessboardCorners(img1, board_sz, corners1, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
found2 = findChessboardCorners(img2, board_sz, corners2, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);

if (found1)
{
    cornerSubPix(gray1, corners1, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
    drawChessboardCorners(gray1, board_sz, corners1, found1);
    cout << "findChessboardCorners-1\n";
    waitKey(0);
}

if (found2)
{
    cornerSubPix(gray2, corners2, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
    drawChessboardCorners(gray2, board_sz, corners2, found2);
    cout << "findChessboardCorners-2\n";
    waitKey(0);
}

imshow("image1", gray1);
imshow("image2", gray2);
}