简体   繁体   English

在OpenCV C ++中播放视频文件

[英]Playing a video file in opencv c++

I'm trying to play a video file using the following code. 我正在尝试使用以下代码播放视频文件。

When run it only shows a black screen with the window name (Video), can anyone help me fix it. 运行时,它仅显示带有窗口名称(视频)的黑屏,任何人都可以帮助我修复它。

#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);
}

Try this if you only want to play a video :::::::::::::::::::::::::: 如果您只想播放视频::,:::::::::::::::::::::::::::

#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;
}

Actually the code you posted won't even compile. 实际上,您发布的代码甚至无法编译。

Just have a look at OpenCV documentation: Reading and Writing images and video 看看OpenCV文档:读写图像和视频

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM