Another important I/O task in computer vision is being able to open and process a video file. Fortunately, OpenCV can easily deal with videos through the VideoCapture
class. Instead of constructing it with a device number, as was done previously, we need to create it with the file path. We can also use the empty constructor and make the open(String filename)
method responsible for pointing to the file.
The videoplayback
project available in the chapter's source code has the same structure as the swing-imageshow
project, explained previously. It only differs when you initialize the VideoCapture
instance:
VideoCapture capture = new VideoCapture("src/main/resources/videos/tree.avi");
We have also put a 50ms delay between each frame so that the whole video doesn't play too fast. There is also code that you can use to manipulate InterruptedException
. Note that the video files won't play with the same velocity as seen in a video player device. This is because the capture.read(webcamMatImage...