OpenCV | Loading Video Last Updated : 26 Aug, 2019 Comments Improve Suggest changes Like Article Like Report 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. Code : CPP CvCapture* capture = cvCreateFileCapture("...input\\video_file.avi"); 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. Code : CPP frame = cvQueryFrame(capture); 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. Code : CPP c = cvWaitKey(10); if (c == 27) break; 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. CPP // Using OpenCV to display video #include <highlevelmonitorconfigurationapi.h> #include <opencv2\highgui\highgui.hpp> #include <opencv2\opencv.hpp> using namespace cv; using namespace std; iint main(int argc, char** argv) { // creating window cvNamedWindow("Display_video", CV_WINDOW_AUTOSIZE); // loading video CvCapture* capture = cvCreateFileCapture("..input\\tree.avi"); IplImage* frame; while (1) { frame = cvQueryFrame(capture); if (!frame) break; cvShowImage("Display_video", frame); char c = cvWaitKey(0); if (c == 27) break; } cvReleaseCapture(&capture); // destroying window cvDestroyWindow("Display_video"); } Output : 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. Comment More infoAdvertise with us Next Article Object Oriented Programming in C++ M mathemagic Follow Improve Article Tags : Computer Subject C++ OpenCV Practice Tags : CPP Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read ASCII Values Alphabets ( A-Z, a-z & Special Character Table ) ASCII (American Standard Code for Information Interchange) is a standard character encoding used in telecommunication. The ASCII pronounced 'ask-ee', is strictly a seven-bit code based on the English alphabet. ASCII codes are used to represent alphanumeric data. The code was first published as a sta 7 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio 10 min read What is an Operating System? An Operating System is a System software that manages all the resources of the computing device. Acts as an interface between the software and different parts of the computer or the computer hardware. Manages the overall resources and operations of the computer. Controls and monitors the execution o 9 min read What is a Neural Network? Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental 12 min read C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i 15+ min read Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so 9 min read C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith 9 min read Like