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

问题描述 投票:0回答:2

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

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

#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);
}
c++ opencv
2个回答
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;
}

3
投票

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

只需查看 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.