EmguCV在图像标签C#WPF中显示Mat [英] EmguCV show Mat in Image tag C# WPF

查看:83
本文介绍了EmguCV在图像标签C#WPF中显示Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在c#中的WPF图像标签中显示Mat对象?

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

还有,是否有办法在canvas/image标签内绘制图像,而不是Imshow打开的新窗口?

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

最后,对于初学者来说,哪个更好用?是EmguCV还是常规的openCV?

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

推荐答案

如果要显示在WPF中读取的图像,则可以使用Xaml中的图像源.您应该将Mat对象转换为位图,然后再将位图转换为图像源对象,因为这将需要显示图像.此堆栈此处详细介绍了如何执行此操作.

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.

首先将Mat对象转换为位图.

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

您将不得不向您的项目添加一些引用,但是该方法在过去对我有用.至于在图像上绘图,我不确定如何完成此操作,因为每次鼠标移动时,您都必须在图像上更新绘图,并且必须合并一些鼠标事件args.

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.

这篇关于EmguCV在图像标签C#WPF中显示Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆