在opencv c ++中播放视频文件

时间:2015-06-29 13:17:58

标签: c++ opencv

我正在尝试使用以下代码播放视频文件。

运行时只显示带有窗口名称(视频)的黑屏,任何人都可以帮我修复它。

#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2\core\core.hpp>
#include "opencv2/opencv.hpp"
using namespace cv;

int main( int argc, char** argv ) 
{
  CvCapture* capture = cvCreateFileCapture( "1.avi" );
  Mat frame= cvQueryFrame(capture);

  imshow("Video", frame);
  waitKey();
  cvReleaseCapture(&capture);
}

2 个答案:

答案 0 :(得分:4)

如果您只想播放视频,请尝试此操作:::::::::::::::::::::::::::

#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2\core\core.hpp>
#include "opencv2/opencv.hpp"
int main(int argc, char** argv)
{
cvNamedWindow("Example3", CV_WINDOW_AUTOSIZE);

//CvCapture* capture = cvCreateFileCapture("20051210-w50s.flv");
CvCapture* capture = cvCreateFileCapture("1.wmv");
/* if(!capture)
    {
        std::cout <<"Video Not Opened\n";
        return -1;
    }*/
IplImage* frame = NULL;

while(1) {

    frame = cvQueryFrame(capture);
    //std::cout << "Inside loop\n";
    if (!frame)
        break;
    cvShowImage("Example3", frame);
    char c = cvWaitKey(33);
    if (c == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Example3");
std::cout << "Hello!";
return 0;
}

答案 1 :(得分:3)

实际上,您发布的代码甚至无法编译。

只需查看OpenCV documentation: Reading and Writing images and video

即可
#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
VideoCapture cap(0); // open the default camera
//Video Capture cap(path_to_video); // open the video file
if(!cap.isOpened())  // check if we succeeded
    return -1;

namedWindow("Video",1);
for(;;)
{
    Mat frame;
    cap >> frame; // get a new frame from camera        
    imshow("Video", frame);
    if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}