当前位置: 首页>>代码示例>>C#>>正文


C# CvMat.Sum方法代码示例

本文整理汇总了C#中CvMat.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# CvMat.Sum方法的具体用法?C# CvMat.Sum怎么用?C# CvMat.Sum使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CvMat的用法示例。


在下文中一共展示了CvMat.Sum方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BuildBoostClassifier


//.........这里部分代码省略.........
                using (CvMat newResponses = new CvMat(ntrainSamples * ClassCount, 1, MatrixType.S32C1))
                {

                    // 1. unroll the database type mask
                    Console.WriteLine("Unrolling the database...");
                    for (int i = 0; i < ntrainSamples; i++)
                    {
                        unsafe
                        {
                            float* dataRow = (float*)(data.DataByte + data.Step * i);
                            for (int j = 0; j < ClassCount; j++)
                            {
                                float* newDataRow = (float*)(newData.DataByte + newData.Step * (i * ClassCount + j));
                                for (int k = 0; k < varCount; k++)
                                {
                                    newDataRow[k] = dataRow[k];
                                }
                                newDataRow[varCount] = (float)j;
                                newResponses.DataInt32[i * ClassCount + j] = (responses.DataSingle[i] == j + 'A') ? 1 : 0;
                            }
                        }
                    }

                    // 2. create type mask
                    varType = new CvMat(varCount + 2, 1, MatrixType.U8C1);
                    varType.Set(CvScalar.ScalarAll(CvStatModel.CV_VAR_ORDERED));
                    // the last indicator variable, as well
                    // as the new (binary) response are categorical
                    varType.SetReal1D(varCount, CvStatModel.CV_VAR_CATEGORICAL);
                    varType.SetReal1D(varCount + 1, CvStatModel.CV_VAR_CATEGORICAL);

                    // 3. train classifier
                    Console.Write("Training the classifier (may take a few minutes)...");
                    boost.Train(
                        newData, DTreeDataLayout.RowSample, newResponses, null, null, varType, null,
                        new CvBoostParams(CvBoost.REAL, 100, 0.95, 5, false, null)
                    );
                }
                Console.WriteLine();
            }

            tempSample = new CvMat(1, varCount + 1, MatrixType.F32C1);
            weakResponses = new CvMat(1, boost.GetWeakPredictors().Total, MatrixType.F32C1);

            // compute prediction error on train and test data
            for (int i = 0; i < nsamplesAall; i++)
            {
                int bestClass = 0;
                double maxSum = double.MinValue;
                double r;
                CvMat sample;

                Cv.GetRow(data, out sample, i);
                for (int k = 0; k < varCount; k++)
                {
                    tempSample.DataArraySingle[k] = sample.DataArraySingle[k];
                }

                for (int j = 0; j < ClassCount; j++)
                {
                    tempSample.DataArraySingle[varCount] = (float)j;
                    boost.Predict(tempSample, null, weakResponses);
                    double sum = weakResponses.Sum().Val0;
                    if (maxSum < sum)
                    {
                        maxSum = sum;
                        bestClass = j + 'A';
                    }
                }

                r = (Math.Abs(bestClass - responses.DataArraySingle[i]) < float.Epsilon) ? 1 : 0;

                if (i < ntrainSamples)
                    trainHr += r;
                else
                    testHr += r;
            }

            testHr /= (double)(nsamplesAall - ntrainSamples);
            trainHr /= (double)ntrainSamples;
            Console.WriteLine("Recognition rate: train = {0:F1}%, test = {1:F1}%", trainHr * 100.0, testHr * 100.0);
            Console.WriteLine("Number of trees: {0}", boost.GetWeakPredictors().Total);

            // Save classifier to file if needed
            if (filenameToSave != null)
            {
                boost.Save(filenameToSave);
            }


            Console.Read();


            tempSample.Dispose();
            weakResponses.Dispose();
            if (varType != null) varType.Dispose();
            data.Dispose();
            responses.Dispose();
            boost.Dispose();
        }
开发者ID:healtech,项目名称:opencvsharp,代码行数:101,代码来源:LetterRecog.cs


注:本文中的CvMat.Sum方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。