3

I have a C++ program written with opencv 3.1 on a workstation running ubuntu 12.04. The program captures a frame from a USB camera (/dev/video2) every 5 minutes, does a bit of stuff, and saves the frame to disk. For some reason the program is throwing this runtime error, but the code seems to be working fine, ie. it's capturing and saving frames, even when running for days...I'm just wondering what the runtime error means and why it's being thrown, and if I need to worry about it? VIDEOIO ERROR: V4L: device /dev/video2: Unable to query number of channels

here's the relevant capture code:

    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
Jason Pitt
  • 31
  • 6

0 Answers0