This article aims to learn how to display a video in OpenCV. To do so is a simple task just like displayed as a single image. But here, this display of frames have to be read in a loop as a part of the sequence.
Lets’s understand the complete process line by line in detail.
cvCreateFileCapture() function takes name/path of the AVI file of the video (to be loaded as a parameter). Then a pointer to a CvCapture structure is returned. All the information about the video file is contained in this structure. The CvCapture structure gets initialised to the beginning of the video when created in this way.
The reading from video file begins once it is inside the while(1) loop. A pointer to a CvCapture structure is taken as an argument by the cvQueryFrame(). Then the next video frame is taken into the memory which is actually a part of CvCapture structure. And that particular frame requires the pointer. cvQueryFrame uses the already allocated memory present in the CvCapture structure, unlike the cvLoadImage that allocates actual memory for the image. Thus, calling a cvReleaseImage() is not necessary for this “frame” pointer. When the CvCapture structure is released, the frame image memory will be freed.
Waiting for 10ms after the frame is displayed. c will be set to the ASCII value of the pressed key if a user hits that particular key. Otherwise, it is set to ‘-1’. If ESC key (having an ASCII value of 27) is pressed by the user, then the code will exit the read loop. Otherwise, after 10ms are passed, the loop is executed again.
Code : Display video using OpenCV.
VIDEO OUTPUT IN THE DISPLAY WINDOW
In the code above we are actually controlling the speed of the video in a very intelligent manner, we are just relying on the timer in cvWaitKey() for controlling the pace of frames to load.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Python | Play a video using OpenCV
- OpenCV C++ Program to play a video
- OpenCV C++ Program to blur a Video
- Saving Operated Video from a webcam using OpenCV
- Python OpenCV: Capture Video from Camera
- Python - Process images of a video using OpenCV
- Python | Create video using multiple images using OpenCV
- Python | Play a video in reverse mode using OpenCV
- OpenCV Python program for Vehicle detection in a Video frame
- OpenCV - Overview
- Implementation of KNN using OpenCV
- Image Inpainting using OpenCV
- OpenCV | Saving an Image
- OpenCV: Segmentation using Thresholding
- Filter Color with OpenCV
- OpenCV | Displaying an Image
- Background subtraction - OpenCV
- Histograms Equalization in OpenCV
- Python | Grayscaling of Images using OpenCV
- OpenCV | Hands on Image Contrast
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.