我想校准我的立体声相机,因此,我试图从左右相机捕获10张棋盘图像。

代码应该做什么:
从左右摄像机捕获10张国际象棋棋盘的图像。如果两个图像都包含清晰的角点,则程序将保存这两个图像。该程序应等待20帧,以便我更改棋盘的位置。

怎么了 :
它捕获了10张好图像并将其保存,但是,它不等我改变棋盘的位置

这是我的代码:

int captureImages_stereoCal()
{
    CvCapture* captureL = cvCreateCameraCapture(1);
    assert(captureL);
    waitKey(10000);
    CvCapture* captureR = cvCreateCameraCapture(2);
    assert(captureR);
    waitKey(10000);
    /*Mat imageL ;
    Mat imageR*/ ;
    int nx=8 , nh=5;


    int frame=0;
    int s =1;
    int ss;

    CvPoint2D32f* cornersL = new CvPoint2D32f[nx*nh];
    int corner_countL;
    CvPoint2D32f* cornersR = new CvPoint2D32f[nx*nh];
    int corner_countR;

    IplImage *imageL=cvQueryFrame(captureL);
    IplImage *gray_imageL=cvCreateImage(cvGetSize(imageL),8,1);
    IplImage *CimageL=cvCreateImage(cvGetSize(imageL),32,3);
    IplImage *imageR=cvQueryFrame(captureR);
    IplImage *gray_imageR=cvCreateImage(cvGetSize(imageR),8,1);
    IplImage *CimageR=cvCreateImage(cvGetSize(imageL),32,3);
    const int board_dt=20;

    while(s<=12)
    {
        if(frame++ % board_dt == 0)
        {
            string Result;
            ostringstream convert;
            ss=s-2;
            convert << ss;
            Result = convert.str();

            //n=(char)s;
            //waitKey(1000);
            //Left -----------------------------------------------------------------------------------------------------------
            string nameL="L.jpg";

            //Find chessboard corners:
            int foundL = cvFindChessboardCorners(imageL, Size(nx,nh), cornersL, &corner_countL,CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
            //Get Subpixel accuracy on those corners
            cvCvtColor(imageL, gray_imageL, CV_BGR2GRAY);
            cvFindCornerSubPix(gray_imageL, cornersL, corner_countL,cvSize(11,11),cvSize(-1,-1), cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
            //Draw it
            Mat MimageL(imageL);
            CimageL=imageL;
            cvDrawChessboardCorners(CimageL, Size(nx,nh), cornersL,corner_countL, foundL);

            //Right -----------------------------------------------------------------------------------------------
            string nameR="R.jpg";

            //Find chessboard corners:
            int foundR = cvFindChessboardCorners(imageR, Size(nx,nh), cornersR, &corner_countR,CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
            //Get Subpixel accuracy on those corners
            cvCvtColor(imageR, gray_imageR, CV_BGR2GRAY);
            cvFindCornerSubPix(gray_imageR, cornersR, corner_countR,cvSize(11,11),cvSize(-1,-1), cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
            //Draw it
            Mat MimageR(imageR);
            CimageR=imageR;
            cvDrawChessboardCorners(CimageR, Size(nx,nh), cornersR,corner_countR, foundR);
            cvShowImage( "CalibrationL", CimageL );
            cvShowImage( "CalibrationR", CimageR );



            if(s>2)
            {
                if((corner_countL==(nx*nh)) && (corner_countR==(nx*nh)) )
                {
                    nameL.insert(1,Result);

                    imwrite(nameL,MimageL);
                    nameR.insert(1,Result);

                    imwrite(nameR,MimageR);
                    s++;
                }
            }
            int c = cvWaitKey(15);
            if(c == 'p')
            {
                c = 0;
                while(c != 'p' && c != 27)
                {
                    c = cvWaitKey(250);
                }
            }
            if(c == 27)
                return 0;
            imageL = cvQueryFrame(captureL);

            imageR = cvQueryFrame(captureR);
            if(s<3)
                s++;
        }// frame++ end

    } // while end
    return 0;
}

同样,在绘制了角点之后,将保存绘制有角点的图像。我要保存图像而没有任何更改。

最佳答案

如果我正确理解问题,那是因为您在if语句条件中使用了后递增:
if(frame++ % board_dt == 0)
您将frame初始化为0以上,因此在第一个调用中,if语句有效地测试了(0 % 20) == 0,这是正确的。

您可以将if语句更改为预先递增的值,例如if(++frame % board_dt == 0)获得您正在寻找的行为

关于c++ - 如何使程序等到我移动棋盘,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29832690/

10-11 18:15