1

I have Written the Code below in my XAML code behind to show webcame frames Received as Mat with Opencvsharp VideoCapture.Read() method in my Image Control named View.

Mat mat = new Mat();
VideoCapture videoCapture = new VideoCapture(2);

while (true)
{
    videoCapture.Read(mat);
    viewer.Source = mat.ToBitmapImage();
    if (btn_stop.IsPressed)
    {
        break;
    }
}
videoCapture.Release();

As u can see I used a converter to convert form Mat to BitmapImage so I can use it as image Source of my control. here are the Converters I used:

static class Converters
{
    public static BitmapImage ToBitmapImage(this Bitmap bitmap)
    {
        BitmapImage bi = new BitmapImage();
        MemoryStream ms = new MemoryStream();
        bi.BeginInit();
        bitmap.Save(ms, ImageFormat.Bmp);
        ms.Seek(0, SeekOrigin.Begin);
        bi.StreamSource = ms;
        bi.EndInit();
        bi.Freeze();
        return bi;
    }

    public static BitmapImage ToBitmapImage(this Mat mat)
    {
        return BitmapConverter.ToBitmap(mat).ToBitmapImage();
    }
}  

Simply this code shows nothing in my image control and the app is freezed. I know that this code is generating too much garbage and I can't do anything about it. Any ideas about my problem? i Also changed my code with the instructions given in this link like below:

viewer.Source = (BitmapSource)new ImageSourceConverter().ConvertFrom(mat.ToBytes());  

and also these converters:

public static BitmapImage ToBitmapImage(this Mat mat)
        {
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = new System.IO.MemoryStream(mat.ToBytes());
            image.EndInit();
            return image;
        }

public static BitmapImage ToBitmapImage(this Mat mat)
        {
            using (var ms = new System.IO.MemoryStream(mat.ToBytes()))
            {
                var image = new BitmapImage();
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = ms;
                image.EndInit();
                return image;
            }
        }

none of these worked for me.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Alireza0772
  • 77
  • 1
  • 10

1 Answers1

1

heres the answer according to Clemens's comment:
Simply insantiate a DispatcherTimer object in MainWindow's constructor and use the Tick event to update the UI:

DispatcherTimer Timer = new DispatcherTimer();  
Timer.Tick += Timer_Tick;  
Timer.Interval = TimeSpan.FromMilliseconds(30);  
Timer.Start();

private void Timer_Tick(object sender, EventArgs e)
{
    if (videoCapture.Read(frame))
    {
        view.Source = OpenCvSharp.Extensions.BitmapSourceConverter.ToBitmapSource(frame);
    }
}
Alireza0772
  • 77
  • 1
  • 10