繁体   English   中英

Emgu.Cv WPF(C#)如何从图像盒加载图像(image.source)

[英]Emgu.Cv WPF (C#)How to load image from Image Box (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;

我可以在imagebox whisc中显示图像,名称是imgPlate

希望我能正确理解您的问题。 如果我要从Image控件获取图像,然后将其放入EmguCV对象进行处理。 尽管这样做有些麻烦,但这是Windows。 为此,请使用我包含的“图像到BitmapSource”转换器。 BitmapSource对象 ImageSource对象,因为BitmapSource是从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
    }

以及转换方法

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

希望这可以帮助。

道格

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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