以高帧频显示图像 [英] Displaying images with a high frame rate

查看:112
本文介绍了以高帧频显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题出在这里:我有一个自定义的硬件设备,我必须从C#/WPF中抓取图像并将其显示在一个窗口中,所有这些都具有120+ FPS.

here's the problem: I have a custom hardware device and I have to grab images from it in C#/WPF and display them in a window, all with 120+ FPS.

问题在于没有事件表明图像已准备就绪,但是我必须不断轮询设备并检查是否有任何新图像,然后再下载它们.

The problem is that there is no event to indicate the images are ready, but I have to constantly poll the device and check whether there are any new images and then download them.

显然有很多方法可以做到,但是我还没有找到合适的方法.

There are apparently a handful of ways to do it, but I haven't been able to find the right one yet.

这是我尝试过的:

  • 一个简单的计时器(或DispatcherTimer)-适用于较低的帧频,但我不能说超过60 FPS.

  • A simple timer (or DispatcherTimer) - works great for slower frame rates but I can't get it past let's say, 60 FPS.

单线程无限循环-相当快,但是我必须将DoEvents/WPF等效项放入循环中,以便重新绘制窗口;这还会带来其他一些不希望的(奇怪的)后果,例如某些控件未被触发的按键事件等.

An single threaded infinite loop - quite fast but I have to put the DoEvents/it's WPF equivalent in the loop in order for window to be redrawn; this has some other unwanted (strange) consequences such as key press events from some controls not being fired etc..

在另一个线程中进行轮询/下载并在UI线程中显示,如下所示:

Doing polling/downloading in another thread and displaying in UI thread, something like this:

 new Thread(() =>
        {
            while (StillCapturing)
            {
                if (Camera.CheckForAndDownloadImage(CameraInstance))
                {
                    this.Dispatcher.Invoke((Action)this.DisplayImage);
                }
            }
        }).Start();

这比较好用,但是给CPU带来了相当大的负担,而且如果它的CPU/内核不超过一个,那么当然会完全杀死机器,这是不可接受的.另外,我通过这种方式有大量的线程争用.

Well, this works relatively well, but puts quite a load on a CPU and of course completely kills the machine if it doesn't have more than one CPU/core, which is unacceptable. Also, I there is a large number of thread contentions this way.

问题显而易见-在这种情况下,还有其他更好的选择吗?

The question is obvious - are there any better alternatives, or is one of these the way to go in this case?

更新:
我以某种方式忘了提及(嗯,在写这个问题时忘了思考),但是我当然不需要显示所有 框架,但是我仍然需要捕获所有框架因此可以将它们保存到硬盘驱动器中.

Update:
I somehow forgot to mention that (well, forgot to think about it while writing this question), but of course I don't need all frames to be displayed, however I still need to capture all of them so they can be saved to a hard drive.

Update2: 我发现DispatcherTimer方法 slow 不是因为它不能足够快地处理所有事情,而是因为DispatcherTimer在触发滴答事件之前会等待下一个垂直同步.对于我而言,这实际上是很好的,因为在滴答事件中,我可以将所有待处理的图像保存到内存缓冲区(用于将图像保存到磁盘)并仅显示最后一个.

Update2: I found out that the DispatcherTimer method is slow not because it can't process everything fast enough, but because the DispatcherTimer waits for the next vertical sync before firing the tick event; which is actually good in my case, because in the tick event I can save all pending images to a memory buffer (used for saving images to disk) and display just the last one.

对于被捕获完全杀死"的旧计算机,WPF似乎退回到非常慢的软件渲染中.我可能无能为力.

As for the old computers being completely "killed" by capturing, it appears that WPF falls back to software rendering which is very slow. There's probably nothing I can do about.

感谢所有答案.

推荐答案

我认为您正在尝试一种过于简单的方法.这就是我要做的.

I think you're trying for too simplistic of an approach. Here's what I would do.

a)在轮询循环中放置一个Thread.Sleep(5),这应该使您可以接近120fps,同时仍保持较低的CPU时间.

a) put a Thread.Sleep(5) in your polling loop, that should allow you to get close to 120fps while still keeping CPU times low.

b)仅每5帧左右更新一次显示.因为我不确定WPF是否可以处理超过60fps的帧,这将减少处理量.

b) Only update the display with every 5th frame or so. That will cut down on the amount of processing as I'm not sure that WPF is made to handle much more than 60fps.

c)使用ThreadPool为每个帧生成一个子任务,然后将其保存到磁盘(每帧单独的文件)中,这样您就不会受到磁盘性能的限制.多余的帧只会堆积在内存中.

c) Use ThreadPool to spawn a subtask for each frame that will then go and save it to the disk (in a seperate file per frame), that way you won't be as limited by disk performance. Extra frames will just pile up in memory.

我个人将按此顺序实施它们. a或b可能会解决您的问题.

Personally I would implement them in that order. Chances are a or b will fix your problems.

这篇关于以高帧频显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆