3

我是 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;
}
4

2 回答 2

3

您必须使用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;
}
于 2013-03-11T07:19:52.080 回答
1

在窗口上显示图像后,必须延迟或等待才能显示下一个图像,我想你可以猜到这是为什么。好的,所以对于我们使用的延迟cvWaitKey()。这就是我在循环代码中添加的内容。

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

   cvShowImage("img", img);
   cvWaitKey(10); 

}
于 2013-03-11T06:37:05.663 回答