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


C# CvMat.MinMaxLoc方法代码示例

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


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

示例1: BuildMlpClassifier


//.........这里部分代码省略.........

            // Create or load MLP classifier
            if (filenameToLoad != null)
            {
                // load classifier from the specified file
                mlp.Load(filenameToLoad);
                ntrainSamples = 0;
                if (mlp.GetLayerCount() == 0)
                {
                    Console.WriteLine("Could not read the classifier {0}", filenameToLoad);
                    return;
                }
                Console.WriteLine("The classifier {0} is loaded.", filenameToLoad);
            }
            else
            {
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //
                // MLP does not support categorical variables by explicitly.
                // So, instead of the output class label, we will use
                // a binary vector of <class_count> components for training and,
                // therefore, MLP will give us a vector of "probabilities" at the
                // prediction stage
                //
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                using (CvMat newResponses = new CvMat(ntrainSamples, ClassCount, MatrixType.F32C1))
                {
                    // 1. unroll the responses
                    Console.WriteLine("Unrolling the responses...");
                    unsafe
                    {
                        for (int i = 0; i < ntrainSamples; i++)
                        {
                            int clsLabel = Cv.Round(responses.DataArraySingle[i]) - 'A';
                            float* bitVec = (float*)(newResponses.DataByte + i * newResponses.Step);
                            for (int j = 0; j < ClassCount; j++)
                            {
                                bitVec[j] = 0.0f;
                            }
                            bitVec[clsLabel] = 1.0f;
                        }
                    }
                    Cv.GetRows(data, out trainData, 0, ntrainSamples);

                    // 2. train classifier
                    int[] layerSizesData = { data.Cols, 100, 100, ClassCount };
                    layerSizes = new CvMat(1, layerSizesData.Length, MatrixType.S32C1, layerSizesData);
                    mlp.Create(layerSizes);
                    Console.Write("Training the classifier (may take a few minutes)...");
                    mlp.Train(
                        trainData, newResponses, null, null,
                        new CvANN_MLP_TrainParams(new CvTermCriteria(300, 0.01), MLPTrainingMethod.RPROP, 0.01)
                    );
                }
                Console.WriteLine();
            }

            mlpResponse = new CvMat(1, ClassCount, MatrixType.F32C1);

            // compute prediction error on train and test data
            for (int i = 0; i < nsamplesAll; i++)
            {
                int bestClass;
                CvMat sample;
                CvPoint minLoc, maxLoc;

                Cv.GetRow(data, out sample, i);                
                mlp.Predict(sample, mlpResponse);
                mlpResponse.MinMaxLoc(out minLoc, out maxLoc, null);
                bestClass = maxLoc.X + 'A';

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

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

            testHr /= (double)(nsamplesAll - ntrainSamples);
            trainHr /= (double)ntrainSamples;
            Console.WriteLine("Recognition rate: train = {0:F1}%, test = {1:F1}%", trainHr * 100.0, testHr * 100.0);

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


            Console.Read();


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


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