OpenCV 中有时正常有时报错(assertion failed)的解决办法

写给自己的note

================================

I met this strange problem from time to time, the programs runs sometimes but some times it throws out some run time error: assertion failed (different kinds of assertion failed errors will happen from time to time).

 

Solution:

http://stackoverflow.com/questions/20604132/cant-capture-video-from-cam-in-opencv-2-4-7

" I solve it by puttingeverything after cap >> frame into anif-statement: "



#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace cv;
using namespace std;


////////////////////////////////////////////////////////////
static void test_image(Mat img_l, Mat img_r, Size boardSize, Mat& view_l, Mat& view_r)
{
	//dosomething   
}


/////////////////////////////////////////////////////////////////
int main(){


    Size boardSize = Size(9, 6);
    

    VideoCapture cap1(1);
    VideoCapture cap2(2);
    cap1.set(CV_CAP_PROP_SETTINGS, 1);
    cap2.set(CV_CAP_PROP_SETTINGS, 1);

    cap1.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
    
    cap2.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap2.set(CV_CAP_PROP_FRAME_HEIGHT, 720);

    Mat l, r, view_l, view_r;

    namedWindow("left");
    namedWindow("right");

    int i = 1;
    while (1){
        cap1 >> l;
        cap2 >> r;

        if (!l.empty() && !r.empty()){
            view_l = l.clone();
            view_r = r.clone();


            test_image(l, r, boardSize, view_l, view_r);
            imshow("left", view_l);
            imshow("right", view_r);

            char c = waitKey(50);
            if (c == 'g'){
                string filename_l, filename_r;
                ostringstream s1;
                ostringstream s2;
                if (i < 10){
                    s1 << "left0" << i << ".jpg";
                    s2 << "right0" << i << ".jpg";
                }
                else{
                    s1 << "left" << i << ".jpg";
                    s2 << "right" << i << ".jpg";
                }
                filename_l = s1.str();
                filename_r = s2.str();
                imwrite(filename_l, l);
                imwrite(filename_r, r);


                cout << "capture " << i << "th pair: success\n";
                i++;
            }
            if (c == 'q' || c == 27)
                break;
        }
    }


    return 0;
}


红色的if statement就是增加的语句。加完以后就没有问题啦╮(╯▽╰)╭

你可能感兴趣的:(Computer,Vision,opencv,assertion,failed,videoCaputre)