OpenCV - Opening and closing webcam

I feel a bit dumb asking this silly questing. I am trying to switch between 2 states, and the event that determines which state i am in is based on the key i've pressed.

so in this case i want the webcam to start automatically, and when key is pressed it then turns it off.

The way i've done it is like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool webcam_on()
{
    int c = 0;
    VideoCapture cap(0);
    if ( !cap.isOpened() )  // if not success, exit program
        {
            cout << "Cannot open the web cam" << endl;
            return -1;
        }
    while(c==0){
        c=cin.get()
        Mat cameraFrame;
        cap.read(cameraFrame);
        imshow("cam", cameraFrame);
        if(c==1)
        {
            cap.release();
        }
        if (waitKey(30) >= 0)
        break;
    }

}


The problem with this method is that cin.get() blocks the video stream.. I wanted something that the webcam turned on until i press some key, which would trigger the release.. How can this be done?
http://docs.opencv.org/modules/highgui/doc/user_interface.html#waitkey
It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.
hmm.. ok. i am able to turn if off using the wait key..

But how can i turn it on and turn off.. switch between those stages.
1
2
3
4
if( cap.isOpened() )
   cap.release();
else
   cap.open(0);
(keep in mind that you shouldn't read from an unopened device)
I tried something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool webcam_on()
{
    VideoCapture cap(0);
    if ( !cap.isOpened() )  // if not success, exit program
    {
        cout << "Cannot open the web cam" << endl;
        return -1;
    }
    while(true){
        Mat cameraFrame;
        cap.read(cameraFrame);
        imshow("cam", cameraFrame);
        if (waitKey(1) == 27){
            if( cap.isOpened() )
                cap.release();
        }
        if(waitKey(1) == 32){
            VideoCapture cap(0);
            if(!cap.isOpened()){
                cout << "error occured " << endl;
            }
            cap.open(0);
            
        }
    }
}


But instead of turning of it crashes. how come?
Last edited on
If you turn it off in line 15, ¿what do you expect to happen when you try to read from it in line 11?

In the if block at line 17 you are creating another object (review scope).
1
2
if( waitKey(1) == 32 )
   cap.open(0);
should suffice.
It doesn't not react after i turn it off..
It doesn't respond to another press 'ESC'
1
2
3
4
5
6
7
8
9
10
11
12
	while(true){
		if( cap.isOpened() ){ //avoid reading from an unopened device
			Mat cameraFrame;
			cap.read(cameraFrame);
			imshow("cam", cameraFrame);
		}
		int keypress = waitKey(1); //saving the pressed key
		if (keypress == 27) //if pressed Esc key
			cap.release(); //turn off camera
		if(keypress == 32) //if pressed Espace key
			cap.open(0); //turn on camera
	}
you have called `waitkey()' twice.

If you want to use Esc to toggle then
1
2
3
4
5
6
if( keypress==27 ){
   if( cap.isOpened() )
      cap.release();
   else
      cap.open(0);
}
Last edited on
Topic archived. No new replies allowed.