Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

emgucv - C# - WPF/Xaml - EMGU Display image

I have a C#/WPF program which use EMGU to manipule a 4K video (image per image). I am doing some operations on the image like rotation, crop … these operations are fast.

My problem is that it take a long time to display the image (40ms). If I add my operations to this time I have less than 20FPS. Displaying the image is 70% of the time.

I am using this code :

<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));

Do you know a fastest way to display my image ?

Thank you,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok I have found the perfect solution.

  1. I create the writable Bitmap
  2. I create the EMGU image based on the writable bitmap buffer

Then In my process I have :

  1. Get the gray image
  2. Convert the gray image directly in the EMGU Image
  3. Tell the writable bitmap the buffer is updated

Allocate variables :

             _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);

Convert gray to Color :

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

Update the screen :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...