System.Runtime.InteropServices.SEHException

asked 2012-10-01 14:56:17 -0500

Josiah gravatar image

updated 2012-10-02 02:34:51 -0500

Daniil Osokin gravatar image

I created a simple windows form application of OpenCV that captures stream of images from a webcam and detect the face and then display the image to a picture box. I created a separate thread to execute the face detector function. The form has a button and picture box and when the button is pressed, the face detector thread starts. It works fine for approximately 2 and half minutes and then it throws this exception:

"An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in Sampleapp.exe

Additional information: External component has thrown an exception."

Here is the entire code for the face detector function:


#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)
            {
            }
        }
    }
}

The exception is thrown at:

faces = cvHaarDetectObjects( .....)
segment.

If anyone has any idea what I should to solve this problem, please let me know.

Sincerely, Josiah

edit retag flag offensive close merge delete