C++ 使用 openCV 阅读视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14833553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading a video with openCV
提问by Engine
I have a video engine2.avithat I want to read and show with openCV. Here's my code:
我有一个视频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);
}
This code doesn't work if my file has the codec YUV 4:2:2 (UYVY)
(I recorded the video using Direct-Show), but works when I use a video I grabbed whit openCV !!
如果我的文件具有编解码器YUV 4:2:2 (UYVY)
(我使用 Direct-Show 录制视频),则此代码不起作用,但是当我使用我从 openCV 中抓取的视频时,该代码就起作用了!!
Has anybody an Idea how this could work ?
有没有人知道这是如何工作的?
UPDATE:
更新:
After reading some links, suggesting that catching exception will solve the problem, I modified my code. It didn't help, but here is the modified code:
看了一些链接,提示捕获异常可以解决问题,我修改了我的代码。它没有帮助,但这是修改后的代码:
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;
}
The program crashes in cap>>frame
. I readed similar questions but they use a frame in YUV (4:2:0), while my video has UYVY (4:2:2). How can I convert this into RGB color model?
程序在cap>>frame
. 我读过类似的问题,但他们在 YUV (4:2:0) 中使用了一个框架,而我的视频有 UYVY (4:2:2)。如何将其转换为 RGB 颜色模型?
UPDATE 2:
更新 2:
After karlphillip's suggestion, I used OpenCV2.4.3, but I still got the same error using the code below:
在 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;
}
回答by karlphillip
Here is a couple of links that might help you:
以下是一些可能对您有所帮助的链接:
- Load, Save and Show YUV 420 images
- How to read a frame from YUV file in OpenCV?
- Converting YUV into BGR or RGB in OpenCV
Edit:
编辑:
I must clear something first: OpenCV is capable of reading YUV frames from a video filebecause it's the underlying library (FFmpeg/GStreamer) that does the job. OpenCV also supports converting between a specific type of YUV and RGBthrough cvCvtColor()
with CV_YCrCb2RGB
or CV_RGBYCrCb
.
我必须首先清除一些东西:OpenCV 能够从视频文件中读取 YUV 帧,因为它是完成这项工作的底层库 (FFmpeg/GStreamer)。OpenCV 还支持通过with或 来在特定类型的YUV 和 RGB之间进行转换。cvCvtColor()
CV_YCrCb2RGB
CV_RGBYCrCb
Upon examining your question again, I noticed you didn't specify the kind of error that happened. You could do a better jobat dealingwith a possiblefailurefrom the capture interface by printing a message to the screen instead of throw
ing it.
再次检查您的问题后,我注意到您没有说明发生的错误类型。你可以做一个更好的工作,在处理与可能的故障通过打印一个消息到屏幕上,而不是从采集界面throw
荷兰国际集团它。
I tested the video file you shared and I had no problems playing it on a window using the following code:
我测试了您共享的视频文件,使用以下代码在窗口上播放它没有问题:
#include <cv.h>
#include <highgui.h>
#include <iostream>
int main(int argc, char* argv[])
{
cv::VideoCapture cap(argv[1]);
if (!cap.isOpened())
{
std::cout << "!!! Failed to open file: " << argv[1] << std::endl;
return -1;
}
cv::Mat frame;
for(;;)
{
if (!cap.read(frame))
break;
cv::imshow("window", frame);
char key = cvWaitKey(10);
if (key == 27) // ESC
break;
}
return 0;
}
If, for some reason, the capture interface fails to open the file it will quit the application immediately, instead of going further just to crash at cap.read(frame)
.
如果由于某种原因,捕获界面无法打开文件,它将立即退出应用程序,而不是在cap.read(frame)
.
回答by Mayur
If you are just looking out for displaying the video, here's the code that worked for me, Please check if it helps you in any way.
如果您只是想显示视频,这里是对我有用的代码,请检查它是否对您有任何帮助。
#include <stdio.h>
#include <opencv2/opencv.hpp>
int main(){
CvCapture *camera=cvCaptureFromFile("C:\test.avi");
if (camera==NULL)
printf("camera is null\n");
else
printf("camera is not null");
cvNamedWindow("img");
while (cvWaitKey(10)!=atoi("q")){
double t1=(double)cvGetTickCount();
IplImage *img=cvQueryFrame(camera);
/*if(img){
cvSaveImage("C:/opencv.jpg",img);
}*/
double t2=(double)cvGetTickCount();
printf("time: %gms fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
cvShowImage("img",img);
}
cvReleaseCapture(&camera);
}
Hope this helps you.
希望这对你有帮助。
回答by Manish Patil
I realised that, the VideoCapture object needs opencv_ffmpeg310_64.dll. Once you copy this dll in your binary folder your VideoCapture object should be able to read video file.
我意识到,VideoCapture 对象需要 opencv_ffmpeg310_64.dll。一旦您将此 dll 复制到您的二进制文件夹中,您的 VideoCapture 对象应该能够读取视频文件。