OPENCV 3.1 videoio错误:v4l:设备/ dev / video无法查询通道数

时间:2016-07-13 20:22:17

标签: c++ opencv ubuntu-12.04 opencv3.1 v4l

我在运行ubuntu 12.04的工作站上使用opencv 3.1编写了一个C ++程序。该程序每5分钟从USB摄像头(/ dev / video2)捕获一帧,执行一些操作,并将帧保存到磁盘。由于某种原因,程序抛出了这个运行时错误,但代码似乎工作正常,即。它可以捕捉和保存帧,即使在运行数天......我只是想知道运行时错误意味着什么以及它被抛出的原因,以及我是否需要担心它? 视频错误:V4L:设备/ dev / video2:无法查询频道数

这里是相关的捕获代码:

    int capture_frame(int doAlign){

    try{

    vector<int> compression_params;
            compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);//(CV_IMWRITE_PXM_BINARY);
            compression_params.push_back(0);





        VideoCapture cap(cameranum); // open the default camera


        long
        c=0;
        while(!cap.isOpened()){ // check if we succeeded
           if (!cap.isOpened() && c < 3){
               sleep(1);
           }
           if (!cap.isOpened() && c >=3) return -1;
           c++;
        }//end while not opened

        cap.set(CV_CAP_PROP_FRAME_WIDTH, 1920);

        cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);


        Mat frame;
        Mat frame_gray;


        while(frame.empty()){
            for (int i=0; i < 10; i++) {cap >> frame;} // get a new frame from camera
        }



        cvtColor(frame, frame_gray, CV_BGR2GRAY); //make it gray



        Mat lastframe;
        Mat im2_aligned;



        stringstream filename;
        stringstream lastfilename;
        stringstream number;
        number << setfill('0') << setw(6) << currentframe;
        filename << directory << "frame" << number.str() << ".png";
        if (currentframe <1 || doAlign == 0) {

            imwrite(filename.str(), frame_gray, compression_params ); //frame vs frame_gray
            //cout << "contrast: " << cvGetCaptureProperty(cap,11) << endl;
        }else{
            number.str("");
            number << setfill('0') << setw(6) << currentframe-1;
            lastfilename << directory << "/frame" << number.str() << ".pgm";
            Mat im1= imread(lastfilename.str());
            Mat im1_gray;
            cvtColor(im1, im1_gray, CV_BGR2GRAY);

            // Define the motion model
            const int warp_mode = MOTION_TRANSLATION;

            // Set a 2x3 warp matrix
            Mat warp_matrix;
             warp_matrix = Mat::eye(2, 3, CV_32F);
             // Specify the number of iterations.
             int number_of_iterations = 5000;

             // Specify the threshold of the increment
             // in the correlation coefficient between two iterations
             double termination_eps = 1e-10;

             // Define termination criteria
             TermCriteria criteria (TermCriteria::COUNT+TermCriteria::EPS, number_of_iterations, termination_eps);

             // Run the ECC algorithm. The results are stored in warp_matrix.
             findTransformECC(im1_gray,frame_gray,warp_matrix,warp_mode,criteria);
             warpAffine(frame, im2_aligned, warp_matrix, im1.size(), INTER_LINEAR + WARP_INVERSE_MAP);
             Mat im2_aligned_gray;
             cvtColor(im2_aligned, im2_aligned_gray, CV_BGR2GRAY);
             imwrite(filename.str(), im2_aligned_gray, compression_params );
             cap.release();




        }//end if not first frame





        currentframe++;
        cap.release();
    }catch (cv::Exception ex){
        cout << " frame was bad! try again" << endl;
        return (0);
     }//end caught exception trying to load

        return (1);

}//end capture frame

0 个答案:

没有答案