我有一个这样的助手类:
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 :(得分:0)
我希望我能正确理解你的问题。如果我想从Image控件获取图像并将其放入EmguCV对象进行处理。虽然有点傻到如何做到这一点,那就是Windows。为此,请使用我包含的Image to 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;
}
}
希望这有帮助。
道格