0

In OpenCV—when initializing a VideoCapture object with a USB webcam—once every 2 or 3 runs the camera will fail to initialize. I have this incredibly ugly piece of code to fix that, but maybe somebody knows what can cause the camera init failure and how I can prevent it?

// Initialize video capture
camera_ = cv::VideoCapture(1);
camera_.set(CV_CAP_PROP_CONVERT_RGB , false);

camera_ >> frame_full_;
while (frame_full_.empty()){ // Could be !cap.isOpened
    cerr << "Camera failure." << endl;
    camera_.release();
    camera_ = cv::VideoCapture(1);
    camera_.set(CV_CAP_PROP_CONVERT_RGB , false);
    camera_ >> frame_full_;
}

/// Do something with the camera feed.

When I close my program, I do get Cleaned up camera. in the console, so I'm thinking it's being released properly.

Added information: On the runs in which the camera fails to init, the call to >>/.read() takes really long to return.

Edit: There is no difference between using the >> operator and .read(), as suggested by @4nonymou5.

1 Answer 1

1

If you meant, you need video feed out of camera and want to do some processing on the individual frames, the the following code might help you.

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std
int main(int argc, char *argv[])
{
  VideoCapture cap(1);
 if ( !cap.isOpened() )  // if not success, exit program
 {
     cout << "Cannot access camera" << endl;
     return -1;
 }
namedWindow("cam",1);
while(true)
        {
        Mat frame;
         bool check = cap.read(frame); // read a new frame from video

        if (!check) //if not success, break loop
         {
                    cout << "Cannot read the frame from video file" << endl;
                    break;
        }

// do what ever processing you want to do on frame

        imShow("cam", frame);
        waitKey(33);
    }
 return 0;
}

And for your code to respond, add

camera_.read(frame_full_);

just before while loop, it would work.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I have no problem getting a camera feed and using it; it's just that every once in a while, the camera fails to initialize for no apparent reason, i.e. your line cap.isOpened() would fail; which is why I have the ugly while loop. I was wondering why the camera would fail so intermittently.
yeah, I had a similar problem when i changed the version of opencv some time back, but, it wasn't initialization of camera, but, while accessing the frames. " camera_ >> frame_full_; " this was returning an empty frame for me, so I added, "camera_.read(frame_full_); " to the line, it worked then. So, just check if the issue is same or else, even I would like to hear a proper answer from someone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.