简体   繁体   中英

EmguCV show Mat in Image tag C# WPF

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM