设置图片框的.image属性的最后一行给我一个错误,错误是
无法将类型'Emgu.CV.Image'隐式转换为'System.Drawing.Image']
我尝试使用toBitmap(),但它说没有属于imageToShow的东西。任何帮助都会很棒。.
Image<Bgr, byte> source = new Image<Bgr, byte>(@"C:\\Users\\ereno\\OneDrive\\Masaüstü\\TRButton.png");
Image<Bgr, byte> template = new Image<Bgr, byte>(@"C:\\Users\\ereno\\OneDrive\\Masaüstü\\Screenshot_2.png");
Image<Bgr, byte> imageToShow = source.Copy();
using (Image<Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
{
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
if (maxValues[0] > 0.9)
{
Rectangle match = new Rectangle(maxLocations[0], template.Size);
imageToShow.Draw(match, new Bgr(Color.Red), 3);
}
}
pictureBox1.Image = imageToShow;
如果要显示在WPF中读取的图像,则不能使用Xaml中的图像源。您应该将图像对象转换为位图,然后再将位图转换为图像源对象,因为这将需要显示图像。这里的stack表单详细介绍了如何执行此操作。
首先将图像对象转换为位图。
//Convert the image object to a bitmap
Bitmap img = image.ToBitmap();
//Using the method below, convert the bitmap to an imagesource
imgOutput.Source = ImageSourceFromBitmap(img);
我在上面调用的函数可以从下面的代码中实现。
//If you get 'dllimport unknown'-, then add 'using System.Runtime.InteropServices;'
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);
public ImageSource ImageSourceFromBitmap(Bitmap bmp)
{
var handle = bmp.GetHbitmap();
try
{
return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally { DeleteObject(handle); }
}
您将不得不向您的项目添加一些引用,但是该方法过去对我有用。