2017-08-10 142 views
0

我有一个辅助类是这样的:Emgu.Cv WPF(C#)如何从图像框(image.source)加载图像

class helper 
{ 
    [System.Runtime.InteropServices.DllImport("gdi32.dll")] 
    // System.Runtime.InteropServices.dll 
    public static extern bool DeleteObject(IntPtr handle); 
    public static BitmapSource bs; 
    public static IntPtr ip; 
    public static BitmapSource LoadBitmap(System.Drawing.Bitmap source) 
    { 

     ip = source.GetHbitmap(); 

     bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty, 

      System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 

     DeleteObject(ip); 

     return bs; 

    } 

我想读的图像,当我点击这样的按钮:

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     //plaka resşm aytıştırma gelecek 
     imgPlate1.Source = img.Source; 

     // Mat image = CvInvoke.Imread(imgPlate1.Source,ImreadModes.Color); 
     Mat image = CvInvoke.Imread(helper.LoadBitmap((System.Drawing.Bitmap)imgPlate1.Source,ImreadModes.Color)); 
     // ProcessImage(image); 
     // helper.SaveImageCapture((BitmapSource)imgCapture.Source); 
    } 

processImage来是另一个函数后读取图像 我会用,但我不能我的形象。另一方面我没有问题,这条线:

imgPlate1.Source = img.Source; 

我可以看到我的图像我的图像箱声名是imgPlate

回答

0

我希望我正确理解你的问题。如果我想要从Image控件获取图像并将其放入EmguCV对象中进行处理。虽然它有点愚蠢,但人们必须这样做,那就是Windows。为此,请使用我包含的Image to BitmapSource转换器。 BitmapSource对象作为BitmapSource的ImageSource对象派生自ImageSource。请参阅以下....

主窗口

<Window x:Class="ReadImageSourceWpf.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:ReadImageSoureWpf" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="612" Width="512"> 
    <StackPanel Orientation="Vertical"> 
     <Image Name="img" Stretch="Fill" Loaded="img_Loaded"/> 
     <Button Name="btnConvert" Content="Convert..." Background="Firebrick" BorderBrush="White" Click="btnConvert_Click" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Height="40"/> 
    </StackPanel> 
</Window> 

using Emgu.CV; 
using Emgu.CV.Structure; 
using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 

namespace ReadImageSourceWpf 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     #region Public Constructors 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     #endregion Public Constructors 

     #region Private Methods 

     private void btnConvert_Click(object sender, RoutedEventArgs e) 
     { 
      Mat readImage = new Mat(); 

      readImage = BitmapSourceConvert.ToMat((BitmapSource)img.Source); 

      Image<Bgra, Byte> newImg = new Image<Bgra, byte>(readImage.Bitmap); 

      BitmapSource bs = BitmapSourceConvert.ToBitmapSource(newImg); 

      Window convImageWin = new ConvertedImageWindow((ImageSource)bs); 

      convImageWin.Show(); 
     } 

     private void img_Loaded(object sender, RoutedEventArgs e) 
     { 
      BitmapImage b = new BitmapImage(); 
      b.BeginInit(); 
      b.UriSource = new Uri(@"D:\OpenCV\opencv-3.2.0\samples\data\Lena.jpg"); 
      b.EndInit(); 
      var image = sender as Image; 
      image.Source = b; 
     } 

     #endregion Private Methods 
    } 
} 

结果窗口

<Window x:Class="ReadImageSourceWpf.ConvertedImageWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:ReadImageSoureWpf" 
     mc:Ignorable="d" 
     Title="ConvertedImageWindow" Height="512" Width="512"> 
    <Grid> 
     <Image Name="convertedImg" Stretch="Fill" /> 
    </Grid> 
</Window> 

using System.Windows; 
using System.Windows.Media; 

namespace ReadImageSourceWpf 
{ 
    /// <summary> 
    /// Interaction logic for ConvertedImageWindow.xaml 
    /// </summary> 
    public partial class ConvertedImageWindow : Window 
    { 
     #region Public Constructors 

     public ConvertedImageWindow(ImageSource img) 
     { 
      InitializeComponent(); 

      convertedImg.Source = img; 
     } 

     #endregion Public Constructors 
    } 

和转换方法

​​3210

希望这有助于。

Doug