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

Playing a video file in opencv c++

本文关键字:视频 文件 播放 opencv c++      更新时间:2023-10-16

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

当运行时,它只显示带有窗口名称(视频)的黑屏,有人能帮我修复它吗?

#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2corecore.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);
}

如果您只想播放视频:

#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2corecore.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 Openedn";
        return -1;
    }*/
IplImage* frame = NULL;
while(1) {
    frame = cvQueryFrame(capture);
    //std::cout << "Inside loopn";
    if (!frame)
        break;
    cvShowImage("Example3", frame);
    char c = cvWaitKey(33);
    if (c == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Example3");
std::cout << "Hello!";
return 0;
}

实际上,您发布的代码甚至不会编译。

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