【发布时间】:2013-01-27 19:38:48
【问题描述】:
我有一个视频 engine2.avi,我想用 openCV 阅读和展示。这是我的代码:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
int main(int argc, char** argv)
{
string filename = "D:\\BMDvideos\\engine2.avi";
VideoCapture capture(filename);
Mat frame;
if( !capture.isOpened() )
throw "Error when reading steam_avi";
namedWindow("w", 1);
for( ; ; )
{
capture >> frame;
//if(!frame)
// break;
imshow("w", frame);
waitKey(20); // waits to display frame
}
waitKey(0);
}
如果我的文件具有编解码器 YUV 4:2:2 (UYVY)(我使用 Direct-Show 录制视频),则此代码不起作用,但当我使用我通过 openCV 抓取的视频时,此代码有效!!
有没有人知道这如何工作?
更新:
看了一些链接后,提示捕获异常会解决问题,我修改了我的代码。它没有帮助,但这里是修改后的代码:
cv::VideoCapture cap("d:\\BMDvideos\\engine2.avi");
cv::Mat frame;
try
{
cap >> frame;
}
catch(cv::Exception ex)
{
std::cout << ex.what() << std::endl;
}
catch(...)
{
std::cout << "Unknown exception" << std::endl;
}
程序在cap>>frame 中崩溃。我读过类似的问题,但他们在 YUV (4:2:0) 中使用帧,而我的视频有 UYVY (4:2:2)。如何将其转换为 RGB 颜色模型?
更新 2:
在karlphillip的建议下,我使用了OpenCV2.4.3,但使用下面的代码仍然出现同样的错误:
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main(){
cv::Mat frame;
cv::VideoCapture cap("d:\\BMDvideos\\B\\Aufnahme.avi");
if(!cap.isOpened())
{
cout << "Error can't find the file"<<endl;
}
while(1){
if(!cap.read(frame))
imshow("",frame);
cv::waitKey(33);
}
return 0;
}
【问题讨论】:
标签: c++ opencv video-streaming directshow video-processing