0

How can I release the camera in OpenCV without closing the running program? I used the following code but camera is still in on condition.

main( int argc, char* argv[] ) 
{
    int j;
    CvCapture* capture = NULL;
    capture = cvCreateCameraCapture( 0 );

    IplImage *frames = cvQueryFrame(capture);

    //Create a new window
    cvNamedWindow( "Recording ...press ESC to stop !", CV_WINDOW_AUTOSIZE );

    while(1)
    {
        if (j<10)
        {
            frames = cvQueryFrame( capture );
            cvShowImage( "Recording ...press ESC to stop !", frames );
        }
        j++;

        if(j==10)
        cvReleaseCapture ( &capture );

        char c = cvWaitKey(33);
        if( c == 27 ) break;
    }
    cvDestroyWindow ( "Recording ...press ESC to stop !");
    return 0;
}
Aurelius
  • 11,111
  • 3
  • 52
  • 69
user2551056
  • 23
  • 1
  • 4

1 Answers1

0

The problem is that you compare (j < 10) and (j == 10) without first initializing j. This is Undefined Behavior, which is a Very Bad Thing. In your case, it happens that the camera doesn't turn off.

The fix is simply to initialize j before you use it, like so:

int j = 0;
Community
  • 1
  • 1
Aurelius
  • 11,111
  • 3
  • 52
  • 69
  • Aurelius thank you for answer but it didn't solve the problem. I have initialized the variable j, still the camera is on state. my aim is to use single camera for two separate processes. so I started with this code. – user2551056 Jul 05 '13 at 09:17