我尝试使用emgu CV检测图片上的面部,但我遇到了异常
SEHException was unhandled
在
Rectangle[] facesDetected = face.DetectMultiScale(gray, 1.1, 10, new Size(20, 20), Size.Empty);
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV.Structure;
using Emgu.CV;
using System.Runtime.InteropServices;
using Emgu.CV.GPU;
using Emgu.CV.CvEnum;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Capture capture;
Image<Bgr, Byte> image;
bool captureInProgress;
List<Rectangle> faces = new List<Rectangle>();
public Form1()
{
InitializeComponent();
}
void ShowFromCam(object sender, EventArgs e){
image = capture.QueryFrame();
}
private void button1_Click(object sender, EventArgs e)
{
if (capture == null)
{
try
{
capture = new Capture();
CascadeClassifier face = new CascadeClassifier("haarcascade_frontalface_default.xml");
Image<Gray, Byte> gray = capture.RetrieveGrayFrame();
Rectangle[] facesDetected = face.DetectMultiScale(
gray, //image
1.1, //scaleFactor
10, //minNeighbors
new Size(20, 20), //minSize
Size.Empty); //maxSize
faces.AddRange(facesDetected);
//Draw detected faces
foreach (Rectangle face1 in faces)
image.Draw(face1, new Bgr(Color.Red), 2);
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
if (capture != null)
{
if (captureInProgress)
{ //if camera is getting frames then stop the capture and set button Text
// "Start" for resuming capture
button1.Text = "Start!"; //
Application.Idle -= ShowFromCam;
}
else
{
//if camera is NOT getting frames then start the capture and set button
// Text to "Stop" for pausing capture
button1.Text = "Stop";
Application.Idle += ShowFromCam;
}
captureInProgress = !captureInProgress;
}
}
}
详细例外:
System.Runtime.InteropServices.SEHException was unhandled
Message=External component has thrown an exception.
Source=Emgu.CV
ErrorCode=-2147467259
StackTrace:
at Emgu.CV.CvInvoke.CvCascadeClassifierDetectMultiScale(IntPtr classifier, IntPtr image, IntPtr objects, Double scaleFactor, Int32 minNeighbors, Int32 flags, Size minSize, Size maxSize)
at Emgu.CV.CascadeClassifier.DetectMultiScale(Image`2 image, Double scaleFactor, Int32 minNeighbors, Size minSize, Size maxSize)
at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in c:\users\moamen\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 44
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() in c:\users\moamen\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
提前致谢:)