opencv打开mp4文件_使用OpenCV读取mp4(Go Pro)视频

当使用OpenCV C++接口在Visual Studio 2013中尝试读取某些Go Pro拍摄的mp4视频文件时遇到困难。虽然可以播放这些文件,但代码无法正常读取。问题可能在于预编译的OpenCV库没有包含ffmpeg支持,这使得能够读取多种视频格式。解决方案包括安装ffmpeg并将其添加到PATH环境变量,或者从源代码编译OpenCV并启用ffmpeg支持。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值