1

我是 opencv 的新手。我正在尝试读取视频 mp4 文件,但我的代码总是说无法打开视频设备或文件。我已将视频文件放在代码文件夹中。

VideoCapture capture;


capture.open("a.mp4");  // Open file


if (!capture.isOpened())
{
    cout << "Cannot open video device or file!" << endl;
    getch();
    return -1;
}

Mat frame;
namedWindow("video", CV_WINDOW_AUTOSIZE);

while(true)
{
    capture >> frame;
    if (frame.empty())
        break;
    imshow("video", frame);
    if (waitKey(30) == 'q')
        break;
}   
4

2 回答 2

1

我发布这个答案以防有人像我一样遇到这个问题。

我面临着几乎同样的问题,试图打开一个视频文件。我的代码可以使用.avi但无法打开带有文件的.mp4文件。

原来openCV需要在系统上安装ffmpeg解码器。在 Windows 操作系统的情况下,您需要将opencv_ffmpeg2411.dll文件复制到C:\Windows\System32目录中。不同系统的DLL可以在OpenCV解压目录中找到。

希望这可以帮助某人。

于 2015-04-08T13:52:23.397 回答
-3

这是视频和图像读取的正确代码。我不知道确切的错误是什么。但是使用与 orieally 不同的代码可以帮助我以不同的方式完成任务。

// newproject.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "highgui.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //Image Reading
    IplImage* img = cvLoadImage( "b.jpg" );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage( "Example1", img );
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );

    //Video Reading

//  cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );

//  CvCapture* capture = cvCreateFileCapture( "video.avi" );
//  IplImage* frame;

    /*  while(1) {
        frame = cvQueryFrame( capture );
        if( !frame ) break;
        cvShowImage( "Example2", frame );
        char c = cvWaitKey(33);
        if( c == 27 ) break;//if user enter escape key then exit
    }*/

    //Summarize by sampling
    CvCapture* capture = 0;
    capture = cvCreateFileCapture( "video.avi" );
    if(!capture){
        return -1;
    }
    IplImage *bgr_frame=cvQueryFrame(capture);//Init the video read
    double fps = cvGetCaptureProperty (capture,CV_CAP_PROP_FPS);

    CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));
    //double fps = cvGetCaptureProperty (capture,CV_CAP_PROP_FPS);
    CvVideoWriter *writer = cvCreateVideoWriter("Ayesha",CV_FOURCC('M','J','P','G'),fps,size);

    int x=0;
    while(1){
    x++;
        bgr_frame = cvQueryFrame( capture );
        if(!bgr_frame)
            break;

        if(x==19||x==21){
            cvShowImage("Example2",bgr_frame);
            cvWriteFrame( writer, bgr_frame );
        }

        if(x==20){
            cvShowImage("Example2",bgr_frame);
            cvWriteFrame( writer, bgr_frame );
            x=0;
        }
        char c=cvWaitKey(30);
        if (c==27) break;
    }

    cvReleaseVideoWriter( &writer );
    cvReleaseCapture( &capture );
    cvDestroyWindow( "Example2" );
}
于 2013-09-13T13:55:38.607 回答