EmguCV show Mat in Image tag C# WPF

半城伤御伤魂 提交于 2021-01-29 14:50:05

问题


Is there a way to show a Mat object inside a WPF image tag in c#?

Mat image= CvInvoke.Imread(op.FileName, Emgu.CV.CvEnum.ImreadModes.AnyColor);

Also, is there a way to draw on the image inside the canvas/image tag, instead of the new window that Imshow opens?

CvInvoke.Imshow("ponaredek", slika);

And lastly, for a begginer, which is better to use? EmguCV or regular openCV?


回答1:


If you want to show the image that you read in inside of your WPF, than you could use an image source, which is found in Xaml. You should convert your Mat object to a bitmap, then a bitmap to an image source object, as that would be needed to display the image. This stack form here goes into good detail on how to do this.

Convert your Mat object to a bitmap first.

//Convert the Mat object to a bitmap
Bitmap img = image.ToBitmap();

//Using the method below, convert the bitmap to an imagesource
imgOutput.Source = ImageSourceFromBitmap(img);

The function that I called above can be achieved from the code below.

//If you get 'dllimport unknown'-, then add 'using System.Runtime.InteropServices;'
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);

public ImageSource ImageSourceFromBitmap(Bitmap bmp)
{
    var handle = bmp.GetHbitmap();
    try
    {
        return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    }
    finally { DeleteObject(handle); }               
}

You will have to add some references to your project, but this method has worked for me in the past. As for drawing on an image, I am not to sure how to accomplish this, because you would have to update your drawing on your image every time the mouse moves, which you would have to incorporate some mouse events args.



来源:https://stackoverflow.com/questions/60746911/emgucv-show-mat-in-image-tag-c-sharp-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!