A lot of people don’t always check these sorts of things, thinking that nothing will go wrong. Don’t do that
here. The returned value of cv::VideoCapture::isOpen() will be false if for some reason the
file could not be opened (e.g., if the file does not exist), but that is not the only possible cause. The
constructed object will also not be ready to be used if the codec with which the video is compressed is not
known. As with the image codecs, you will need to have the appropriate library already residing on your
computer in order to successfully read the video file. This is why it is always important for your code to
check the return value of cv::VideoCapture::isOpen(), because even if it works on one machine
(where the needed codec DLL or shared library is available), it might not work on another machine (where
that codec is missing). Once we have an open cv::VideoCapture object, we can begin reading frames
and do a number of other things. But before we get into that, let’s take a look at how to capture images
from a camera.
The variant of cv::VideoCapture::VideoCapture() that takes an integer device argument
works very much like the string version we just discussed, except without the headache from the
codecs.
6
In this case, we give an identifier that indicates a camera we would like to access and how we