EMGU与C#WPF

时间:2013-05-16 20:30:51

标签: c# wpf image opencv emgucv

我正在尝试按照以下教程但使用WPF而不是Win Forms:

A Basic Program

WPF不使用PictureBox,而是使用Image

所以这里尝试加载Image

XAML

<Image x:Name="srcImg" Width="400" Height="300"></Image>

CS尝试1:

Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
srcImg.Source = My_Image.ToBitmap();

错误消息

Cannot implicitly convert type 'System.Drawing.Bitmap' 
to 'System.Windows.Media.ImageSource'

CS尝试2:

Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
srcImg.Source = new BitmapImage(My_Image);

错误消息

Error   1   The best overloaded method match for 'System.Windows.Media.Imaging.BitmapImage.BitmapImage(System.Uri)' has some invalid arguments  
Error   2   Argument 1: cannot convert from 'Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>' to 'System.Uri' 

任何想法我做错了什么?

5 个答案:

答案 0 :(得分:7)

解决了问题。要转换图像:

Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
srcImg.Source = BitmapSourceConvert.ToBitmapSource(myImage);

BitmapSourceConvert类:

public static class BitmapSourceConvert
{
    [DllImport("gdi32")]
    private static extern int DeleteObject(IntPtr o);

    public static BitmapSource ToBitmapSource(IImage image)
    {
        using (System.Drawing.Bitmap source = image.Bitmap)
        {
            IntPtr ptr = source.GetHbitmap();

            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                ptr,
                IntPtr.Zero,
                Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            DeleteObject(ptr);
            return bs;
        }
    }
}

答案 1 :(得分:1)

对于EmguCV v4.4版本,一旦添加Emgu.CV.Bitmap nuget包,就可以使用image.ToBitmap()函数将Image 与.NetStandard中的System.Drawing.Bitmap相互转换。

请注意,对于非Windows平台,System.Drawing.Bitmap需要安装本机gdi +才能正常工作。

答案 2 :(得分:0)

您可以获得ImageSource from a Bitmap(浪费但已经拥有ToBitmap()),或者您可以实施直接转换。

答案 3 :(得分:0)

如果你想将Emgu CV与WPF一起使用,你应该考虑使用Emgu的图片框进行用户控制(这个控件只能用于win表格),然后将它与WindowsFormsHost一起使用。

答案 4 :(得分:0)

BitmapSourceConverter.cs复制%installfolder%/Emgu/emgucv-windesktop x.x.x/Emgu.CV.WPF并添加到您的项目以转换为BitmapSource。它是完整版本:

//----------------------------------------------------------------------------
//  Copyright (C) 2004-2017 by EMGU Corporation. All rights reserved.
//----------------------------------------------------------------------------

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Emgu.CV;
using Emgu.CV.CvEnum;

namespace Emgu.CV.WPF
{
   public static class BitmapSourceConvert
   {
      /// <summary>
      /// Delete a GDI object
      /// </summary>
      /// <param name="o">The poniter to the GDI object to be deleted</param>
      /// <returns></returns>
      [DllImport("gdi32")]
      private static extern int DeleteObject(IntPtr o);

      /// <summary>
      /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
      /// </summary>
      /// <param name="image">The Emgu CV Image</param>
      /// <returns>The equivalent BitmapSource</returns>
      public static BitmapSource ToBitmapSource(IImage image)
      {
         using (System.Drawing.Bitmap source = image.Bitmap)
         {
            IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap

            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                ptr,
                IntPtr.Zero,
                Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            DeleteObject(ptr); //release the HBitmap
            return bs;
         }
      }

      public static Mat ToMat(BitmapSource source)
      {

         if (source.Format == PixelFormats.Bgra32)
         {
            Mat result = new Mat();
            result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 4);
            source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step*result.Rows, result.Step);
            return result;
         } else if (source.Format == PixelFormats.Bgr24)
         {
            Mat result = new Mat();
            result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 3);
            source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step * result.Rows, result.Step);
            return result;
         }
         else
         {
            throw new Exception(String.Format("Convertion from BitmapSource of format {0} is not supported.", source.Format));
         }
      }
   }
}
相关问题