如何使程序等到我移动棋盘

时间:2015-04-23 19:31:03

标签: c++ opencv camera-calibration

我想校准我的立体相机,所以我试图从右侧和左侧相机拍摄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;
}

此外,在绘制角落后,它会保留图像并在其上绘制角点。我希望保存图像而不做任何更改。

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,那是因为你在if语句条件上使用了后递增:

if(frame++ % board_dt == 0)

您将frame初始化为0,因此在第一次调用时,if语句有效地测试(0 % 20) == 0,这是真的。

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