3

我在运行 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
4

0 回答 0