Accessing cameras

OpenCV supports accessing cameras that are available on a system by using the same VideoCapture class we used for accessing the video files. The only difference is that instead of passing a file name to the constructor of the VideoCapture class or its open function, you must provide a 0-based index number that corresponds to each available camera. For instance, the default webcam on a computer can be accessed and displayed by using the following example code:

VideoCapture cam(0); 
// check if camera was opened correctly 
if(!cam.isOpened()) 
    return -1; 
 
// infinite loop 
while(true) 
{ 
    Mat frame; 
    cam >> frame; 
    if(frame.empty()) 
        break; 
 
    // process the frame if necessary ... 
 
    // display the frame 
    imshow("Camera", frame); 
 
    // stop camera if space is pressed 
    if(waitKey(10) == ' ') 
        break; 
} 
 
cam.release(); 

As you can see, the only difference is in the constructor. This implementation of the VideoCapture class allows users to treat any type of video source the same way, thus writing almost the exact same code to deal with cameras instead of video files. This is also the case with network feeds, as described in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.197