使用OpenCV读取mp4(Go Pro)视频 [英] Reading mp4 (Go Pro) video using OpenCV

查看:1694
本文介绍了使用OpenCV读取mp4(Go Pro)视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2013中使用C ++界面使用OpenCV时,我在阅读某些视频文件时遇到困难。我已经能够以其他视频格式阅读,因此相信我的代码还行。



问题视频文件是使用Go Pro拍摄的,并且是mp4。我可以在同一台机器上播放他们使用OpenCV使用经典的媒体播放器。我使用MediaInfo收集视频文件的信息:



格式:MPEG-4
格式配置文件:JVT
编解码器ID:avc1
文件大小:126 MiB



我试图使用OpenCV的set函数显式地提供fourcc代码,并提供divx作为代码和avc1但没有运气。 p>

我的程序代码如下:

  int main char ** argv){
if(argc!= 2){
help(argv);
system(Pause);
return 1;
}

std :: string arg = argv [1];
VideoCapture capture;
capture.set(CV_CAP_PROP_FOURCC,CV_FOURCC('A','V','C','1'));
capture.open(arg);
if(!capture.isOpened()){
cerr<< 无法打开视频文件!\\\
< endl;
help(argv);
system(Pause);
return 1;
}
return process(capture);
}

void help(char ** argv){
cout< \\\
TBC< endl;
}

int process(VideoCapture& src){
标量常量LOW_HSV_THRESHOLD(120,45,75);
标量常量HIGH_HSV_THRESHOLD(140,55,85);
string windowName [] = {Original,HSV,In Range,Binary};
namedWindow(windowName [0],CV_WINDOW_KEEPRATIO);
namedWindow(windowName [1],CV_WINDOW_KEEPRATIO);
namedWindow(windowName [2],CV_WINDOW_KEEPRATIO);
namedWindow(windowName [3],CV_WINDOW_KEEPRATIO);
Mat mat原始;
Mat matHsv;
Mat matInRange;
Mat dst;
src>>原始

cvtColor(matOriginal,matHsv,CV_BGR2HSV);
GaussianBlur(matHsv,matHsv,Size(5,5),1.5);

cvtColor(matOriginal,matInRange,CV_BGR2GRAY);
threshold(matInRange,dst,100,255,THRESH_BINARY);
// inRange(matHsv,LOW_HSV_THRESHOLD,HIGH_HSV_THRESHOLD,matInRange);
imshow(windowName [0],matOriginal);
imshow(windowName [1],matHsv);
imshow(windowName [2],matInRange);
imshow(windowName [3],dst);

waitKey(0);
return 0;
}

}

任何帮助将非常感激。



非常感谢,



Kevin

解决方案

出于文档目的,我已经扩展了我的评论作为这个问题的解决方案。



预构建的windows的opencv库可能没有编译时使用ffmpeg支持,可以读取多种视频格式。



一个快速的解决方法是安装 ffmpeg 并将其添加到Windows PATH
环境变量。



适当的解决方案,但将在安装ffmpeg后从源代码构建opencv库。这可以通过使用 CMake-GUI 来简化,它允许您轻松配置opencv的多个选项,例如添加ffmpeg支持( USE_FFMPEG flag)。 Cmake还将生成Visual Studio项目文件,以便您可以轻松地编译库。



有关更详细的指南,请查看官方opencv文档页面


I am having difficulty reading in certain video files when using OpenCV with the C++ interface in Visual Studio 2013. I have been able to read in other video formats so believe my code is okay.

The problem video files have been taken using a Go Pro and are mp4. I can play them on the same machine I am using OpenCV on using classic media player. I have used MediaInfo to gather information on the video file:

Format : MPEG-4 Format profile : JVT Codec ID : avc1 File size : 126 MiB

I have tried providing fourcc codes explicitly using the set function of OpenCV and providing divx as the code and avc1 but with no luck.

My program code is as follows:

int main(int argc, char** argv) {
    if (argc != 2) {
        help(argv);
        system("Pause");
        return 1;
    }

    std::string arg = argv[1];
    VideoCapture capture;
    capture.set(CV_CAP_PROP_FOURCC, CV_FOURCC('A', 'V', 'C', '1'));
    capture.open(arg);
    if (!capture.isOpened()) {
        cerr << "Failed to open video file!\n" << endl;
        help(argv);
        system("Pause");
        return 1;
    }
    return process(capture);
}

   void help(char** argv) {
        cout << "\n TBC" << endl;
    }

    int process(VideoCapture& src) {
        Scalar const LOW_HSV_THRESHOLD(120, 45, 75);
        Scalar const HIGH_HSV_THRESHOLD(140, 55, 85);
        string windowName[] = { "Original", "HSV", "In Range", "Binary"};
        namedWindow(windowName[0], CV_WINDOW_KEEPRATIO); 
        namedWindow(windowName[1], CV_WINDOW_KEEPRATIO); 
        namedWindow(windowName[2], CV_WINDOW_KEEPRATIO);
        namedWindow(windowName[3], CV_WINDOW_KEEPRATIO);
        Mat matOriginal;
        Mat matHsv;
        Mat matInRange;
        Mat dst;
        src >> matOriginal;

        cvtColor(matOriginal, matHsv, CV_BGR2HSV);
        GaussianBlur(matHsv, matHsv, Size(5, 5), 1.5);

        cvtColor(matOriginal, matInRange, CV_BGR2GRAY);
        threshold(matInRange, dst, 100, 255, THRESH_BINARY);
        //inRange(matHsv, LOW_HSV_THRESHOLD, HIGH_HSV_THRESHOLD, matInRange);
        imshow(windowName[0], matOriginal);
        imshow(windowName[1], matHsv);
        imshow(windowName[2], matInRange);
        imshow(windowName[3], dst);

        waitKey(0);
        return 0;
    }

}

Any help would be hugely appreciated.

Many Thanks,

Kevin

解决方案

For documentation purposes, I have expanded my comment as a solution to this problem.

The prebuilt opencv libraries for windows might have not been compiled with ffmpeg support which enables a multitude of video formats to be read.

One quick'n'dirty workaround would be installing ffmpeg and adding it to the windows PATH environment variable.

The proper solution though, would be building the opencv library from source after installing ffmpeg. This is simplified by using CMake-GUI which allows you easily configure opencv's multiple options such as adding ffmpeg support(USE_FFMPEG flag). Cmake will also generate the Visual Studio project files so you can easily compile the library.

For a more detailed guide check out the video guide on the official opencv documentation page

这篇关于使用OpenCV读取mp4(Go Pro)视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆