'Emgu.CV.Util.CvException' 发生在 'Emgu.CV.World.dll' - 尝试预测时

'Emgu.CV.Util.CvException' occurred in 'Emgu.CV.World.dll' - When trying to Predict

我正在使用 EmguCV 3.1 开发人脸识别应用程序。我正在使用 EigenFaceRecognizer 作为识别算法。我尝试使用下面的代码训练图像。

    List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
    FaceRecognizer recognizer;
    List<Face> faceList = new List<Face>();
        ...
        ...
        recognizer = new EigenFaceRecognizer(80, double.PositiveInfinity);
        ...
        ...            
        Image<Gray, byte> image = new Image<Gray, byte>(imgBox2.DisplayedImage.Bitmap);
        trainingImages.Add(image);
        List<int> trainigLabels = new List<int>();
        recognizer.Train(trainingImages.ToArray(), trainigLabels.ToArray());
        recognizer.Save("TraningData");
        faceList.Add(new Face(image.Bytes.Length, txtName.Text));
        ...
        ...
        Image<Gray, byte> image = new Image<Gray, byte>(imgBox2.DisplayedImage.Bitmap);
            recognizer.Load("TraningData");
            try
            {
                var result = recognizer.Predict(image);
                MessageBox.Show(result.Label.ToString());
            }
            catch (Emgu.CV.Util.CvException ex)
            {
                Console.WriteLine(ex);
            }

但是当调用这段代码时,它给了我以下错误。

A first chance exception of type 'Emgu.CV.Util.CvException' occurred in Emgu.CV.World.dll
The program '[1136] IPTest.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
The program '[1136] IPTest.vshost.exe: Program Trace' has exited with code 0 (0x0).

它是在尝试使用 recognizer.Train() 时发生的。我做错了什么?

更新
经过反复试验,我知道问题出在 recognizer.Predict() 方法上。
当我使用 try/catch 它显示以下异常

Emgu.CV.Util.CvException: OpenCV: The matrix is not continuous, thus its number of rows can not be changed

好的,我找到了解决方案。问题不在于我的代码。当本机代码试图访问某些已损坏或无法访问的内存位置时抛出。
所以我找到了一个简单有效的解决方案。

try
{
   var result = recognizer.Predict(image);
}
catch (System.AccessViolationException)
{
   recognier =  new EigenFaceRecognizer(80, double.PositiveInfinity);
}

但是您无法在运行时捕获 AccessViolationException。所以你必须改变设置,这样你就可以。为此,您可以关注 this question 和答案。