问题描述
我想播放视频
如何使用opencv?
我使用中的代码构建一个最小示例,显示如何从相机检索帧,并在 QLabel
中显示它们。
传递到 cvCreateCameraCapture()
的值可以在您的系统上更改。在我的Linux 0
是幻数。在Windows和Mac上,我使用 -1
。
为简单起见,我只是分享 main()
function:
* argv)
{
QApplication app(argc,argv);
CvCapture * capture = cvCreateCameraCapture(0); // try -1或values> 0如果你的相机不工作
if(!capture)
{
std :: cout< 无法创建捕获;
return -1;
}
IplImage * frame = NULL;
QLabel label;
while(1)
{
frame = cvQueryFrame(capture);
if(!frame)break;
QImage qt_img = IplImage2QImage(frame);
label.setPixmap(QPixmap :: fromImage(qt_img));
label.show();
//调用cvWaitKey()是在抓取框架之间创建一些延迟的必要条件
cvWaitKey(10); // wait 10ms
}
return app.exec();
}
EDIT: b
从相机播放视频文件或视频的过程相同。唯一真正改变的是 cvCreateCameraCapture()
for cvCreateFileCapture()
。
如果 cvCreateFileCapture()
正在返回 NULL
,则表示无法打开视频文件。这可能是由于两个原因:它没有编解码器来处理视频,或者找不到该文件。
很多人在加载文件时在Windows上犯了错误:
capture = cvCreateFileCapture(C:\path\video.avi);
他们使用单斜杠,当他们应该使用double:
capture = cvCreateFileCapture(C:\\path\\video.avi);
如果某些操作可能失败,您必须始终检查呼叫的返回:
capture = cvCreateFileCapture(C:\\path\\video.avi);
if(!capture)
{
//打印错误消息,然后退出
}
I would like to play video
How can it be done using opencv ?
I'm using the code from our previous conversation to build a minimal example that shows how to retrieve frames from the camera and display them inside a QLabel
.
Notice that the value passed to cvCreateCameraCapture()
can change on your system. On my Linux 0
is the magic number. On Windows and Mac I use -1
.
For simplicity purposes, I'm just sharing the body of the main()
function:
int main(int argc, char** argv)
{
QApplication app(argc, argv);
CvCapture* capture = cvCreateCameraCapture(0); // try -1 or values > 0 if your camera doesn't work
if (!capture)
{
std::cout << "Failed to create capture";
return -1;
}
IplImage* frame = NULL;
QLabel label;
while (1)
{
frame = cvQueryFrame(capture);
if( !frame ) break;
QImage qt_img = IplImage2QImage(frame);
label.setPixmap(QPixmap::fromImage(qt_img));
label.show();
// calling cvWaitKey() is essencial to create some delay between grabbing frames
cvWaitKey(10); // wait 10ms
}
return app.exec();
}
EDIT:
The procedure to play a video file or video from a camera are the same. The only thing that really changes is cvCreateCameraCapture()
for cvCreateFileCapture()
.
If cvCreateFileCapture()
is returning NULL
, it means that it couldn't open the video file. This could happen for 2 reasons: it doesn't have the codec to deal with the video, or it couldn't find the file.
A lot of people make the folowwing mistake on Windows when loading files:
capture = cvCreateFileCapture("C:\path\video.avi");
They use single slashes, when they should be using double:
capture = cvCreateFileCapture("C:\\path\\video.avi");
and if something can fail, you must ALWAYS check the return of the call:
capture = cvCreateFileCapture("C:\\path\\video.avi");
if (!capture)
{
// print error message, then exit
}
这篇关于opencv库视频播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!