emgucv抛出异常的示例代码?

本文关键字:代码 抛出异常 emgucv | 更新日期: 2023-09-27 18:28:31

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

不包括2:进入:跨过非用户代码"Emgu.CV.ML.RTrees.Train"Emgu.CV.dll中首次出现类型为"Emgu.CV.Util.CvException"的异常进入:跳过非用户代码"MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen"

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

我的代码,使用此示例-http://www.emgu.com/wiki/index.php/Mushroom_Poisonous_Prediction_(决策树)_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));
        }
    }

emgucv抛出异常的示例代码?

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

-我开始发现错误发生在哪里,并评论了这行param.priors=priorisHandle.AddrOfPinnedObject();//先验数组

-我坐上了火车和测试车,尽管RTrees无法预测任何情况。即使使用蘑菇示例,我也试图将Dtrees更改为Rtrees,但也没有得到任何结果。也许我需要调整参数。不管怎样,我解决了评论那行的错误。

要使其工作,您应该简单地使用param = MCvRTParams.GetDefaultParameter();我使其与x64配置一起工作