C# - WPF/Xaml - EMGU 显示图像

C# - WPF/Xaml - EMGU Display image

我有一个 C#/WPF 程序,它使用 EMGU 来处理 4K 视频(每个图像的图像)。我正在对图像进行一些操作,例如旋转、裁剪……这些操作很快。

我的问题是显示图像需要很长时间(40 毫秒)。如果我这次加上我的操作,我的帧率不到 20FPS。显示图像是 70% 的时间。

我正在使用此代码:

<Image x:Name="VideoWidget" Stretch="Uniform"/>

if (_cameraWritableBitmap == null || _cameraWritableBitmap.PixelWidth != width || _cameraWritableBitmap.PixelHeight != height)
{
    _cameraWritableBitmap = new WriteableBitmap(
        width,
        height,
        dpiX,
        dpiY,
        pixelFormat,
        null);

    _mainWindow.VideoWidget.Source = _cameraWritableBitmap;
    OnPropertyChanged("ZoomImage");
    OnPropertyChanged("ZoomScreen");
    OnPropertyChanged("IsZoomImageAlert");
    OnPropertyChanged("IsZoomScreenAlert");
}

timePrev = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;

_cameraWritableBitmap.Lock();
_cameraWritableBitmap.WritePixels(new Int32Rect(0, 0, width, height), imgColor.Bytes, width * 4, 0);
//Marshal.Copy(imgColor.Bytes, 0, _cameraWritableBitmap.BackBuffer, imgColor.Bytes.Length);
//_cameraWritableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
_cameraWritableBitmap.Unlock();
Console.WriteLine("# ECRITURE " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - timePrev));

你知道显示我的图像的最快方法吗?

谢谢,

好的,我找到了完美的解决方案。

  1. 我创建了可写位图
  2. 我根据可写位图缓冲区创建 EMGU 图像

然后在我的过程中我有:

  1. 获取灰度图
  2. 直接在EMGU Image中转换灰度图
  3. 告诉可写位图缓冲区已更新

分配变量:

             _cameraWritableBitmap = new WriteableBitmap(
                width,
                height,
                dpiX,
                dpiY,
                PixelFormats.Bgra32,
                null);

            _mainWindow.VideoWidget.Source = _cameraWritableBitmap;

            _cameraImageBuffer = new Image<Bgra, byte>(width, height, _cameraWritableBitmap.BackBufferStride, _cameraWritableBitmap.BackBuffer);

将灰色转换为彩色:

CvInvoke.CvtColor(imgGray, _cameraImageBuffer, Emgu.CV.CvEnum.ColorConversion.Gray2Bgra);

更新画面:

_cameraWritableBitmap.Lock();
_cameraWritableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
_cameraWritableBitmap.Unlock();