0

我正在尝试遵循以下教程,但使用 WPF 而不是 Win Forms:

基本程序

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' 

任何想法我做错了什么?

4

5 回答 5

8

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

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;
        }
    }
}
于 2013-05-16T21:41:21.563 回答
2

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

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

于 2020-09-22T12:37:43.660 回答
0

您可以ImageSource从 aBitmap中获取(浪费但您已经拥有ToBitmap()),或者您可以实现直接转换。

于 2013-05-16T20:33:29.110 回答
0

如果您想将 Emgu CV 与 WPF 一起使用,您应该考虑使用 Emgu 的图片框做一个用户控件(此控件仅适用于 win 表单),然后将其与 WindowsFormsHost 一起使用。

于 2013-05-16T20:49:26.227 回答
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));
         }
      }
   }
}
于 2018-02-26T05:58:55.427 回答