c++ - 在 OpenCV 中播放视频

标签 c++ video opencv

我是 OpenCV 的初学者,我想在 OpenCV 中播放视频。我做了一个代码,但它只显示一个图像。 我正在使用 OpenCV 2.1 和 Visual Studio 2008。 如果有人指导我哪里出错了,我将非常感激。 这是我粘贴的代码:

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int main()
{
CvCapture* capture = cvCaptureFromAVI("C:/OpenCV2.1/samples/c/tree.avi");
IplImage* img = 0; 
if(!cvGrabFrame(capture)){              // capture a frame 
printf("Could not grab a frame\n\7");
exit(0);}
cvQueryFrame(capture); // this call is necessary to get correct 
                   // capture properties
int frameH    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int fps       = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int) cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT);
///numFrames=total number of frames



printf("Number of rows %d\n",frameH);
printf("Number of columns %d\n",frameW,"\n");
printf("frames per second %d\n",fps,"\n");
printf("Number of frames %d\n",numFrames,"\n");

for(int i=0;i<numFrames;i++)
{
IplImage* img = 0;
img=cvRetrieveFrame(capture); 
cvNamedWindow( "img" );
cvShowImage("img", img);

}
cvWaitKey(0);
cvDestroyWindow( "img" );
cvReleaseImage( &img );
cvReleaseCapture(&capture);


return 0;
}

最佳答案

您必须使用 cvQueryFrame 而不是 cvRetrieveFrame。同样正如@Chipmunk 所指出的,您必须在 cvShowImage 之后添加延迟。

#include "stdafx.h" 
#include "cv.h"       
#include "highgui.h"
cvNamedWindow( "img" );
for(int i=0;i<numFrames;i++)
{
   IplImage* img = cvQueryFrame(capture); 
   cvShowImage("img", img);
   cvWaitKey(10);
}

下面是使用 OpenCV 播放视频的完整方法:

int main()
{
    CvCapture* capture = cvCreateFileCapture("C:/OpenCV2.1/samples/c/tree.avi");

    IplImage* frame = NULL;

    if(!capture)
    {
        printf("Video Not Opened\n");
        return -1;
    }

    int width = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
    int height = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT);
    double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    int frame_count = (int)cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT);

    printf("Video Size = %d x %d\n",width,height);
    printf("FPS = %f\nTotal Frames = %d\n",fps,frame_count);

    while(1)
    {
        frame = cvQueryFrame(capture);

        if(!frame)
        {
            printf("Capture Finished\n");
            break;
        }

        cvShowImage("video",frame);
        cvWaitKey(10);
    }

    cvReleaseCapture(&capture);
    return 0;
}

关于c++ - 在 OpenCV 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15332446/

相关文章:

javascript - 根据两个选择框更新html视频,加载但不播放视频

python - OpenCV HOG 人物检测 - 如何判断检测到的人是否与之前检测到的人相同?

java - OpenCV - 椭圆根本不显示

c++ - 是否可以在 GPU 中运行一段纯 C++ 代码

c++ - 这是将指向对象的指针分配给函数的正确方法吗?

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

matlab - Matlab中的AVIREAD适合什么样的编码器?

c++ - 当我返回其引用时,如果超出范围,静态 std::vector 将取消

video - 以 ffmpeg -i %04d.png 模式重复图像

c++ - 将预制的 OpenCV 安装移动到另一台 Linux 计算机