我创建了一个简单的 OpenCV 窗体应用程序,它从网络摄像头捕获图像流并检测面部,然后将图像显示到图片框。我创建了一个单独的线程来执行人脸检测器功能。表单有一个按钮和图片框,当按下按钮时,人脸检测器线程启动。它可以正常工作大约 2 分半钟,然后抛出此异常:
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in Sampleapp.exe
Additional information: External component has thrown an exception.
以下是人脸检测功能的完整代码:
#include "stdafx.h"
#include "Form1.h"
#include <cv.h>
#include <highgui.h>
namespace Sampleapp {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
const char *frontalface_cascade_name = "haarcascade_frontalface_alt.xml";
CvHaarClassifierCascade *frontalface_cascade;
//----------------------
// Face detector
void Form1::face_detector()
{
IplImage* img;
IplImage *img_gray;
CvMemStorage *storage;
CvSeq* faces;
/*CvSeq* righteye;
CvSeq* lefteye;
CvSeq* nose;*/
frontalface_cascade = ( CvHaarClassifierCascade* )cvLoad( frontalface_cascade_name, 0, 0, 0 );
while(this->button1->Text == "no")
{
try
{
storage = cvCreateMemStorage( 0 );
CvCapture *capture = cvCreateCameraCapture(0);
img = cvQueryFrame(capture);
img_gray = cvCreateImage( cvSize( img->width, img->height ), IPL_DEPTH_8U, 1 );
cvCvtColor( img, img_gray, CV_BGR2GRAY );
cvEqualizeHist( img_gray, img_gray );
faces = cvHaarDetectObjects( img_gray, frontalface_cascade, storage, 1.1, 2, CV_HAAR_FIND_BIGGEST_OBJECT, cvSize(40, 40));
this->pictureBox1->Image = (gcnew System::Drawing::Bitmap(img->width,img->height,img->widthStep, System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)img->imageData));
cvReleaseMemStorage(&storage);
}
catch (System::NullReferenceException^ m)
{
}
}
}
}
异常被抛出:
faces = cvHaarDetectObjects( .....)
部分。
这是调试输出:
First-chance exception at 0x74b9c41f in Sampleapp.exe: Microsoft C++ exception: cv::Exception at memory location 0x069ce950..
A first chance exception of type 'System.Runtime.InteropServices.SEHException' occurred in Sampleapp.exe
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in Sampleapp.exe
Additional information: External component has thrown an exception.
如果有人知道我应该怎么做来解决这个问题,请告诉我。
真诚的,约西亚