Got following code online:

#include “highgui.h”
int main( int argc, char** argv ) {
cvNamedWindow( “Example2”, CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( argv[1] );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( “Example2”, frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
cvReleaseCapture( &capture );
cvDestroyWindow( “Example2” );

But “cvCreateFileCapture” and “cvQueryFrame” seems to be depreciated and were not found in documentation of new version.

Alternative: Found “VideoCapture” class in documentation here.

VideoCapture is new class in version 3.0 which does the work of capturing video from devices / file system

and has following important methods:

  • VideoCapture::Open() : Open video file or a capturing device for video capturing
  • VideoCapture::isOpened() : Returns true if video capturing has been initialized already.
  • VideoCapture::release(): Releases memory
  • VideoCapture::read() : Grabs, decodes and returns the next video frame. This is the single most important method which performs the task of loading video and rendering it.


The methods/functions combine VideoCapture::grab() and VideoCapture::retrieve() in one call. This is the most convenient method for reading video files or capturing data from decode and return the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.

This method takes on output image array as argument and returns a bool based on the result if it is able to load the file or not.

This function returns a bool value. Here is a sample code using this method:

#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"

using namespace cv;

int main(int argc, char** argv) {

    namedWindow("Example1", CV_WINDOW_AUTOSIZE);
    VideoCapture cap(argv[1]);
    if (!cap.isOpened()) return -1;
    //    CvCapture* capture = cvCreateFileCapture(argv[1]);
    Mat img;
        bool success =;
        if(!success) break;
        imshow("Example1", img);
        char c = cvWaitKey(33);
        if (c == 27) break;

    return 0;

Also note that we need to include videoio header and corresponding library as well.

imshow() function:

Displays an image in the specified window.

C++: void imshow(const string& winname, InputArray mat)


    • winname – Name of the window.
    • image – Image to be shown.

The function imshow displays an image in the specified window. If the window was created with the CV_WINDOW_AUTOSIZE flag, the image is shown with its original size, however it is still limited by the screen resolution. Otherwise, the image is scaled to fit the window. The function may scale the image, depending on its depth:

  • If the image is 8-bit unsigned, it is displayed as is.
  • If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
  • If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].

If the window was not created before this function, it is assumed creating a window with CV_WINDOW_AUTOSIZE.

If you need to show an image that is bigger than the screen resolution, you will need to call namedWindow(“”, WINDOW_NORMAL) before the imshow.

If window was created with OpenGL support, imshow also support ogl::Buffer , ogl::Texture2D and gpu::GpuMat as input.

The function “cvShowImage” is depreciated now. More infor here.


Creates a window.

C++: void namedWindow(const string& winname, int flags=WINDOW_AUTOSIZE )

More info on namedWindow() can be found here.


Destroys a window.

C++: void destroyWindow(const string& winname)

winname – Name of the window to be destroyed.

The function destroyWindow destroys the window with the given name.

cv::createTrackbar() :

Creates a trackbar and attaches it to the specified window.

The function createTrackbar creates a trackbar (a slider or range control) with the specified name and range, assigns a variable value to be a position synchronized with the trackbar and specifies the callback function onChange to be called on the trackbar position change. The created trackbar is displayed in the specified window winname.


trackbarname : Name of the created trackbar.

winname : Name of the window that will be used as a parent of the created trackbar.

value : Optional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable.

count : Maximal position of the slider. The minimal position is always 0.

onChange : Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated.

userdata: User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.

More info. can be found here.

Here are some examples of using trackbar.