emgucv抛出异常的示例代码??

时间:2013-08-20 17:06:13

标签: c# opencv machine-learning emgucv

所以我使用emguCV来使用OpenCV的机器学习算法。我的代码如下,当它进入dtree.Train方法时,它给我异常(exc1),如果我等待ir给我和错误消息(err1)。如果我尝试调试并进入这个方法,如果给我另一个异常(exc2)并且调试器不会前进。 EXC1: Emgu.CV.dll中出现'Emgu.CV.Util.CvException'类型的第一次机会异常

EXC2: 步入:踩过非用户代码'Emgu.CV.ML.RTrees.Train' 在Emgu.CV.dll中发生了'Emgu.CV.Util.CvException'类型的第一次机会异常 步入:踩过非用户代码'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'

ERR1: CLR无法从COM上下文0x795fa8转换到COM上下文0x796118。拥有目标上下文/公寓的线程很可能是在非抽空等待或处理非常长时间运行的操作而不抽取Windows消息。这种情况通常会对性能产生负面影响,甚至可能导致应用程序变得无响应或内存使用量随时间不断累积。为了避免这个问题,所有单线程单元(STA)线程都应该使用抽取等待原语(例如CoWaitForMultipleHandles)并在长时间运行操作期间定期泵送消息。

我的代码,使用此示例 - http://www.emgu.com/wiki/index.php/Mushroom_Poisonous_Prediction_(Decision_Tree)_in_CSharp

   public void train()
    {

        Matrix<float> data, response;
       // data = new Matrix<float>(15, 200);
       // response = new Matrix<float>(15, 200);
        Console.WriteLine("reading shroom data");
        ReadMushroomData(out data, out response);

        ///data = new Matrix<float>(1, 5);

        //Use the first 80% of data as training sample
        int trainingSampleCount = (int)(data.Rows * 0.8);

        Matrix<Byte> varType = new Matrix<byte>(data.Cols + 1, 1);
        varType.SetValue((byte)Emgu.CV.ML.MlEnum.VAR_TYPE.CATEGORICAL); //the data is categorical

        Matrix<byte> sampleIdx = new Matrix<byte>(data.Rows, 1);
        using (Matrix<byte> sampleRows = sampleIdx.GetRows(0, trainingSampleCount, 1))
            sampleRows.SetValue(255);

        float[] priors = new float[] { 1, 0.5f };
        GCHandle priorsHandle = GCHandle.Alloc(priors, GCHandleType.Pinned);

        MCvRTParams param = new MCvRTParams();
        param.maxDepth = 8;// max depth
        param.minSampleCount = 10;// min sample count
        param.regressionAccuracy = 0;// regression accuracy: N/A here
        param.useSurrogates = true; //compute surrogate split, no missing data
        param.maxCategories = 15;// max number of categories (use sub-optimal algorithm for larger numbers)
        param.cvFolds = 10;
        //param.use1seRule = true;
        param.truncatePrunedTree = true;
        param.priors = priorsHandle.AddrOfPinnedObject(); // the array of priors


        Console.WriteLine("starting train");
        using (RTrees dtree = new RTrees())
        {

            bool success = dtree.Train(data, 
                Emgu.CV.ML.MlEnum.DATA_LAYOUT_TYPE.ROW_SAMPLE, 
                response, 
                null, 
                sampleIdx, 
                varType, 
                null, 
                param);


            Console.WriteLine("starting tests");

            if (!success) return;
            double trainDataCorrectRatio = 0;
            double testDataCorrectRatio = 0;
            for (int i = 0; i < data.Rows; i++)
            {
                using (Matrix<float> sample = data.GetRow(i))
                {
                    double r = dtree.Predict(sample, null);
                    r = Math.Abs(r - response[i, 0]);
                    if (r < 1.0e-5)
                    {
                        if (i < trainingSampleCount)
                            trainDataCorrectRatio++;
                        else
                            testDataCorrectRatio++;
                    }
                }
            }

            trainDataCorrectRatio /= trainingSampleCount;
            testDataCorrectRatio /= (data.Rows - trainingSampleCount);

            Console.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio * 100));
            Console.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio * 100));
        }
    }

2 个答案:

答案 0 :(得分:0)

经过大量的测试和反复试验,我发现了这个: -i用它的源代码调试emgucv并找到错误的描述(这不是很有帮助)

-i开始查看错误发生的位置,我对此行进行了评论param.priors = priorsHandle.AddrOfPinnedObject(); //先行数组

-i让火车和测试工作,虽然它无法用RTree预测。即使有蘑菇的例子,我也试图将Dtrees改为Rtrees,但我也没有得到任何结果。也许我需要调整参数。无论如何,我在评论该行时解决了错误。

答案 1 :(得分:0)

要使其工作,您只需使用param = MCvRTParams.GetDefaultParameter();我就可以使用x64配置