Ask Your Question
-1

How to play a video in opencv using C++?

asked 2017-08-22 00:35:56 -0600

Sowmya Shree gravatar image

updated 2017-08-22 00:37:41 -0600

Please tell me how a video file is played in Opencv. I tried using cvCapture. here is the following code I used. it is displaying 'video not opened'.

int main(int argc, char* argv[]) { CvCapture* capture = cvCreateFileCapture("C:\Users\sbv\Documents\MyVideo.avi");

IplImage* frame = NULL;

if (!capture)
{
    printf("Video Not Opened\n");
    return -1;
}

int width = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int height = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int frame_count = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);

printf("Video Size = %d x %d\n", width, height);
printf("FPS = %f\nTotal Frames = %d\n", fps, frame_count);

while (1)
{
    frame = cvQueryFrame(capture);

    if (!frame)
    {
        printf("Capture Finished\n");
        break;
    }

    cvShowImage("video", frame);
    cvWaitKey(10);
}

cvReleaseCapture(&capture);
return 0;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-08-22 01:02:46 -0600

berak gravatar image

updated 2017-08-22 01:04:29 -0600

this is code from the outdated opencv1.0 c-api. YOU MUST NOT USE IT !

(and if you were using a more recent opencv version, it would not let you !)

opencv moved to c++ a long time ago, you'll have to follow up.

no idea, where you found that code, but please throw it away, and rather have a look at current opencv tutorials

#include "opencv2/opencv.hpp"
using namespace cv;

#include <iostream>
using namespace std;

int main()
{
    VideoCapture cap("path/to/my.avi");
    if (! cap.isOpened())
    {
        cout << "could not open the VideoCapture !" << endl;
        return -1;
    }
    while(true)
    {
        Mat frame;
        cap >> frame;
        if (frame.empty()) // movie is over
        {
            break;
        }
        imshow("ocv",frame);
        int k = waitKey(10);
        if (k==27) break;  // esc. pressed
    }
    return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-08-22 00:35:56 -0600

Seen: 595 times

Last updated: Aug 22 '17