0

我使用佳能 EOS SDK 并希望通过相机流实现实时绿屏。

我有一个带有像素操作优化的简单绿屏算法。

 public Bitmap greenScreen(Bitmap input, int tolerance)
        {
            FastPixel fpinput = new FastPixel(input);
            fpinput.Lock();

            Bitmap output = new Bitmap(input.Width, input.Height);
            FastPixel fpoutput = new FastPixel(output);
            fpoutput.Lock();

            for (int y = 0; y < output.Height; y++)
            {
                for (int x = 0; x < output.Width; x++)
                {
                    Color camColor = fpinput.GetPixel(x, y);

                    // Every component (red, green, and blue) can have a value from 0 to 255, so determine the extremes
                    byte max = Math.Max(Math.Max(camColor.R, camColor.G), camColor.B);
                    byte min = Math.Min(Math.Min(camColor.R, camColor.G), camColor.B);

                    bool replace =
                        camColor.G != min // green is not the smallest value
                        && (camColor.G == max // green is the bsiggest value
                        || max - camColor.G < 8) // or at least almost the biggest value
                        && (max - min) > tolerance; // minimum difference between smallest/biggest value (avoid grays)

                    if (!replace)
                        fpoutput.SetPixel(x, y, camColor);

                }
            }

            fpinput.Unlock(true);
            fpoutput.Unlock(true);

            return fpoutput.Bitmap;
        }

对于 800x600 图片,这在我的 i5 上大约需要 300 毫秒。当然,这对于流畅的直播来说还不够快。

我有哪些优化选项?有机会使用 gpu 功能吗?会有什么机会?我不能在佳能相机上使用 DirectShow(不存在驱动程序)

4

1 回答 1

2
  1. GetPixel/SetPixel

    不了解您的环境(我不使用 C# 编写代码),但这些功能在所有环境中都会非常缓慢。位图通常有其他选择,例如:

  2. 图像采集

    同样,我不知道您正在使用的SDK也不知道您需要的目标 fps,但您的SDK流式传输可能很慢,而不仅仅是您的应用程序。DirectShow可以为您提供较低的30 fps分辨率。但是我注意到,对于更大的分辨率,它比VFW慢,但它可能与USB有关,而不是与流和驱动程序本身有关。由于您没有DirectShow驱动程序,因此您很不走运。如果你有VFW驱动程序,那么你可以使用它,但它通常只能给你最多,15 fps但代码明显更简单,CPU要求更低。

    顺便说一句,一些驱动程序能够为流式传输(或时间间隔)设置fps ,因此在您的SDK中搜索此类功能或令牌,也许您默认设置的fps较低。

    还可以尝试使用不同的USB(如果使用 USB 连接),某些 USB 的带宽可能已经被其他设备占用(理想情况下,在整个 Root HUB 上使用单个摄像头进行测试)。

要检测哪一个是问题,请在没有像素被传输或查看的情况下计算fps(只需 rem 所有与图像相关的代码并仅测量偶数发生)。如果采集速度很快,那么驱动程序就不是问题。然后用传输进行测量,如果速度太慢,该代码也是一个问题。

于 2016-10-23T10:20:28.080 回答