简体   繁体   English

Emgu CV视频播放缓慢吗?

[英]Emgu CV video playing is slow?

i am trying to open a video file from disk and try to play it using openCV wrapper: Emgu CV in visual studio c#. 我正在尝试从磁盘打开视频文件,并尝试使用openCV包装器播放视频文件:Visual Studio C#中的Emgu CV。 i'm using imagebox provided by Emgu CV to show my video on a form. 我正在使用Emgu CV提供的imagebox在窗体上显示我的视频。 i calculate waiting time for showing next frame of video using this code: 我使用以下代码计算显示下一帧视频的等待时间:

await Task.Delay(Convert.ToInt32( 1000.0 / FPS));

problem is the video plays slower than calculated formula. 问题是视频播放速度比计算公式慢。 i open a 29.97 FPS video file and the formula should return (1000/30)=33. 我打开一个29.97 FPS视频文件,公式应返回(1000/30)= 33。 but when i play the video i can immediately see the video is playing slower than 33 FPS. 但是当我播放视频时,我可以立即看到视频播放速度低于33 FPS。 if i go to my imagebox properties by right clicking on the imagebox , it says that the FPS is 21 which i guess is the actual FPS video is playing. 如果我通过右键单击imagebox进入我的imagebox属性,它表示FPS为21,我想这是实际播放的FPS视频。 what is happening? 怎么了?

this is my code. 这是我的代码。 it's very basic: 这是非常基本的:

public partial class Form1 : Form
{

    VideoCapture videoCapture;
    double FPS;
    double totalFrames;
    int currentFrameNo;

    public Form1()
    {
        InitializeComponent();
    }

    private void btnOpen_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            videoCapture = new VideoCapture(ofd.FileName);
            FPS = videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps);
            totalFrames = videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameCount);
            currentFrameNo = 0;
        }
    }

    private async void btnPlay_Click(object sender, EventArgs e)
    {
        while (currentFrameNo<totalFrames)
        {
            imageBox1.Image = videoCapture.QueryFrame();
            currentFrameNo += 1;
            await Task.Delay(Convert.ToInt32( 1000.0 / FPS));
        }
    }
}

what i want is a smooth playing. 我想要的是流畅播放。 what should change? 应该改变什么? and sorry for the bad language. 对不起,我的语言不好。

As other comments have suggested - the problem is that a constant delay is being added to whatever the loop processing time is. 正如其他评论所建议的那样-问题是无论循环处理时间是多少,都会添加恒定的延迟。

A better solution would be to use a Timer (roughly!) as follows: 更好的解决方案是使用Timer(大约!),如下所示:

bool playingVideo = false;
Timer frameTimer = new Timer(this.GrabFrame, null, 0, 1000 / 33);

private void btnPlay_Click(object sender, EventArgs e)
{
    playingVideo = true;
}


private void GrabFrame(object sender, EventArgs e)
{
    if(playingVideo)
    {
        while (currentFrameNo<totalFrames)
        {
            imageBox1.Image = videoCapture.QueryFrame();
            currentFrameNo += 1;
        }
    }
}

This way the frames will be requested every 33ms regardless of the capture time 这样,无论捕获时间如何,每33ms将请求一次帧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM