Ask Your Question

better ways to re-initialize webcam via VideoCapture?

asked 2019-04-19 04:46:56 -0500

gino0717 gravatar image

I'm considering a condition: if my computer is running but the USB of a webcam is accidentally unplugged. I want to write something to make the webcam can re-initialize.

int main()
   VideoCapture video(0);
    Mat frame;
   while (true) {
           imshow("show video",frame);
       else {
               cout<<"wait key input"<<endl;
               char temp[1]; //get a key input
                  VideoCapture video_temp(0);
  1. not sure but it seems video.isOpened() is not always return false even if the USB webcam is not available any more after the webcam first be launched so I may need a video.grab() to do some double check.

  2. in Linux the system can immediately detect the disconnect of webcam. However, in windows system, it takes a while ( about 30 seconds ) to find out that the webcam is disconnected.

is there any better way to make the webcam re-initialize in the program?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-04-19 06:03:41 -0500

Yashas gravatar image

updated 2019-04-19 06:05:52 -0500

You can use the open method to restart webcam capture.

VideoCapture cap(0);
// some code here;
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-04-19 04:46:56 -0500

Seen: 291 times

Last updated: Apr 19 '19