Search code examples
c#wpfemgucv

Emgu.Cv WPF (C#)How to load image from Image Box (image.source)


I have a helper class like this:

 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;

    }

And I want to read image when I click button like this :

 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 is another function I will use after read image But I cant my image On the other hand I have no problem this line :

imgPlate1.Source = img.Source;

I can seemy image my imagebox whisc is name is imgPlate


Solution

  • I hope I am properly understanding your question. If I am you want to get the image from an Image control and put it into an EmguCV object for processing. While its a bit goofy how one has to do this, that's Windows. To do this use the Image to BitmapSource converter that I am including. A BitmapSource object is an ImageSource object as BitmapSource derives from ImageSource. See the following....

    MainWindow

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

    Result Window

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

    And the Conversion methods

            public static Mat ToMat(BitmapSource source)
            {
                if (source.Format == PixelFormats.Bgr32)
                {
                    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;
                }
            }
    
    
    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;
        }
    }
    

    Hope this helps.

    Doug