基于C#和WPF的Emgu-CV人脸检测
我正在用C#和WPF中的基于C#和WPF的Emgu-CV人脸检测,c#,wpf,C#,Wpf,我正在用C#和WPF中的Emgu CV进行人脸检测,问题是我的程序正在运行,但窗口中没有显示任何内容- 代码是- MainWindow.xaml <Window x:Class="FaceRecognition.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/20
Emgu CV
进行人脸检测,问题是我的程序正在运行,但窗口中没有显示任何内容-
代码是-
MainWindow.xaml
<Window x:Class="FaceRecognition.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800" Loaded="Window_Loaded">
<Grid>
<Image Name="image1" Stretch="Fill"></Image>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Emgu.CV.Structure;
using Emgu.CV;
using System.Runtime.InteropServices;
using System.Windows.Threading;
using System.Drawing;
namespace FaceRecognition
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Capture capture;
private HaarCascade haarCascade;
DispatcherTimer timer;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
capture = new Capture();
haarCascade = new HaarCascade(@"haarcascade_frontalface_default.xml");
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
Image<Bgr, Byte> currentFrame = capture.QueryFrame();
if (currentFrame != null)
{
Image<Gray, Byte> grayFrame = currentFrame.Convert<Gray, Byte>();
var detectedFaces = grayFrame.DetectHaarCascade(haarCascade)[0];
foreach (var face in detectedFaces)
currentFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);
image1.Source = ToBitmapSource(currentFrame);
}
}
[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(); //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;
}
}
}
}
没有检测到任何面?是的,没有检测到任何面,而且当我关闭应用程序时,出现此异常“Emgu.CV.CvInvoke”的类型初始值设定项引发异常。
D:\dot net\handouts\handoutsprogram\FaceRecognition\FaceRecognition\bin\Debug