带C#WPF的EMGU
我尝试遵循以下教程,但使用WPF而不是Win表单: WPF不使用带C#WPF的EMGU,c#,wpf,image,opencv,emgucv,C#,Wpf,Image,Opencv,Emgucv,我尝试遵循以下教程,但使用WPF而不是Win表单: WPF不使用图片盒,而是使用图像 因此,尝试加载图像 XAML CS尝试2: Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName); srcImg.Source = My_Image.ToBitmap(); Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Open
图片盒
,而是使用图像
因此,尝试加载图像
XAML
CS尝试2:
Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
srcImg.Source = My_Image.ToBitmap();
Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
srcImg.Source = new BitmapImage(My_Image);
Image My\u Image=新图像(Openfile.FileName);
srcImg.Source=新的位图图像(My_图像);
错误消息
Cannot implicitly convert type 'System.Drawing.Bitmap'
to 'System.Windows.Media.ImageSource'
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'
错误1“System.Windows.Media.Imaging.BitmapImage.BitmapImage(System.Uri)”的最佳重载方法匹配具有一些无效参数
错误2参数1:无法从“Emgu.CV.Image”转换为“System.Uri”
你可以得到一个(浪费的,但是你已经有了<代码> toByMax()/Cux>),或者你可以实现一个直接转换。 < P>如果你想用WPF使用EGGU CV,你应该考虑用EGU的PICTIOBOX做一个用户控件(这个控件只与Win表单一起工作)。然后将其与WindowsFormsHost一起使用 问题解决了。要转换图像,请执行以下操作:
Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
srcImg.Source = BitmapSourceConvert.ToBitmapSource(myImage);
从
%installfolder%/Emgu/emgucv windesktop x.x.x/Emgu.CV.WPF
复制BitmapSourceConverter.cs
,并添加到项目中以转换为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));
}
}
}
}
//----------------------------------------------------------------------------
//EMGU公司2004-2017年版权所有(C)。版权所有。
//----------------------------------------------------------------------------
使用制度;
使用System.Runtime.InteropServices;
使用System.Windows;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用Emgu.CV;
使用Emgu.CV.CvEnum;
名称空间Emgu.CV.WPF
{
公共静态类BitmapSourceConvert
{
///
///删除GDI对象
///
///要删除的GDI对象的poniter
///
[DllImport(“gdi32”)]
私有静态外部intdeleteObject(IntPtr o);
///
///将IImage转换为WPF BitmapSource。结果可用于Image.Source的Set属性
///
///Emgu CV图像
///等效位图源
公共静态位图源到位图源(IImage图像)
{
使用(System.Drawing.Bitmap源=image.Bitmap)
{
IntPtr ptr=source.GetHbitmap();//获取Hbitmap
BitmapSource bs=System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
ptr,
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ptr);//释放HBitmap
返回bs;
}
}
公共静态Mat ToMat(位图源)
{
if(source.Format==PixelFormats.Bgra32)
{
Mat结果=新Mat();
结果.创建(source.PixelHeight,source.PixelWidth,DepthType.Cv8U,4);
source.CopyPixels(Int32Rect.Empty、result.DataPointer、result.Step*result.Rows、result.Step);
返回结果;
}else if(source.Format==PixelFormats.Bgr24)
{
Mat结果=新Mat();
创建(source.PixelHeight、source.PixelWidth、DepthType.Cv8U、3);
CopyPixels(Int32Rect.Empty、result.DataPointer、result.Step*result.Rows、result.Step);
返回结果;
}
其他的
{
抛出新异常(String.Format(“不支持从格式{0}的BitmapSource转换。”,source.Format));
}
}
}
}
对于EmguCV的v4.4版本,添加Emgu.CV.Bitmap nuget包后,您可以使用Image.ToBitmap()函数在.NetStandard中与System.Drawing.Bitmap进行图像转换
请注意,对于非windows平台,System.Drawing.Bitmap需要安装本机gdi+才能工作。来吧,阅读您正在使用的东西的文档,错误也非常清楚。我不知道EMGU是什么,但WPF不关心
System.Drawing
东西,也不会使用它。如果您希望在WPF中实现任何目标,请从所有项目中删除对System.Drawing.dll
的所有引用,然后重新开始。您好,我想您误解了这个问题。我正在尝试显示存储在EMGU(OpenCV)图像中的图像,而不是显示文件中的图像。当你似乎有路径时,你为什么要尝试这样做?你知道什么是OpenCV或什么是EMGU吗?我正在测试我是否能够在开始更改之前显示EMGU图像。@Gravy:你可以显示几乎任何东西,你只需要相应地转换它,如果你使用外文格式,将没有快捷方式,除非你想执行EMGU->位图->ImageSource,因为众所周知位图->ImageSource。
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;
}
}
}
//----------------------------------------------------------------------------
// 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));
}
}
}
}