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


C# CvMat.SetReal1D方法代码示例

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


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

示例1: BuildRtreesClassifier

        /// <summary>
        /// RTrees
        /// </summary>
        /// <param name="dataFilename"></param>
        /// <param name="filenameToSave"></param>
        /// <param name="filenameToLoad"></param>
        private void BuildRtreesClassifier(string dataFilename, string filenameToSave, string filenameToLoad)
        {
            CvMat data = null;
            CvMat responses = null;
            CvMat varType = null;
            CvMat sampleIdx = null;


            int nsamplesAll = 0, ntrainSamples = 0;
            double trainHr = 0, testHr = 0;
            CvRTrees forest = new CvRTrees();

            try
            {
                ReadNumClassData(dataFilename, 16, out data, out responses);
            }
            catch
            {
                Console.WriteLine("Could not read the database {0}", dataFilename);
                return;
            }
            Console.WriteLine("The database {0} is loaded.", dataFilename);

            nsamplesAll = data.Rows;
            ntrainSamples = (int)(nsamplesAll * 0.8);

            // Create or load Random Trees classifier
            if (filenameToLoad != null)
            {
                // load classifier from the specified file
                forest.Load(filenameToLoad);
                ntrainSamples = 0;
                if (forest.GetTreeCount() == 0)
                {
                    Console.WriteLine("Could not read the classifier {0}", filenameToLoad);
                    return;
                }
                Console.WriteLine("The classifier {0} is loaded.", filenameToLoad);
            }
            else
            {
                // create classifier by using <data> and <responses>
                Console.Write("Training the classifier ...");

                // 1. create type mask
                varType = new CvMat(data.Cols + 1, 1, MatrixType.U8C1);
                varType.Set(CvScalar.ScalarAll(CvStatModel.CV_VAR_ORDERED));
                varType.SetReal1D(data.Cols, CvStatModel.CV_VAR_CATEGORICAL);

                // 2. create sample_idx
                sampleIdx = new CvMat(1, nsamplesAll, MatrixType.U8C1);
                {
                    CvMat mat;
                    Cv.GetCols(sampleIdx, out mat, 0, ntrainSamples);
                    mat.Set(CvScalar.RealScalar(1));

                    Cv.GetCols(sampleIdx, out mat, ntrainSamples, nsamplesAll);
                    mat.SetZero();
                }

                // 3. train classifier
                forest.Train(
                    data, DTreeDataLayout.RowSample, responses, null, sampleIdx, varType, null,
                    new CvRTParams(10, 10, 0, false, 15, null, true, 4, new CvTermCriteria(100, 0.01f))
                );
                Console.WriteLine();
            }

            // compute prediction error on train and test data
            for (int i = 0; i < nsamplesAll; i++)
            {
                double r;
                CvMat sample;
                Cv.GetRow(data, out sample, i);

                r = forest.Predict(sample);
                r = Math.Abs((double)r - 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);

            Console.WriteLine("Number of trees: {0}", forest.GetTreeCount());

            // Print variable importance
            Mat varImportance0 = forest.GetVarImportance();
            CvMat varImportance = varImportance0.ToCvMat();
            if (varImportance != null)
//.........这里部分代码省略.........
开发者ID:healtech,项目名称:opencvsharp,代码行数:101,代码来源:LetterRecog.cs

示例2: BuildBoostClassifier

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataFilename"></param>
        /// <param name="filenameToSave"></param>
        /// <param name="filenameToLoad"></param>
        private void BuildBoostClassifier(string dataFilename, string filenameToSave, string filenameToLoad)
        {
            const int ClassCount = 26;

            CvMat data = null;
            CvMat responses = null;
            CvMat varType = null;
            CvMat tempSample = null;
            CvMat weakResponses = null;

            int nsamplesAall = 0, ntrainSamples = 0;
            int varCount;
            double trainHr = 0, testHr = 0;
            CvBoost boost = new CvBoost();

            try
            {
                ReadNumClassData(dataFilename, 16, out data, out responses);
            }
            catch
            {
                Console.WriteLine("Could not read the database {0}", dataFilename);
                return;
            }
            Console.WriteLine("The database {0} is loaded.", dataFilename);

            nsamplesAall = data.Rows;
            ntrainSamples = (int)(nsamplesAall * 0.5);
            varCount = data.Cols;

            // Create or load Boosted Tree classifier
            if (filenameToLoad != null)
            {
                // load classifier from the specified file
                boost.Load(filenameToLoad);
                ntrainSamples = 0;
                if (boost.GetWeakPredictors() == null)
                {
                    Console.WriteLine("Could not read the classifier {0}", filenameToLoad);
                    return;
                }
                Console.WriteLine("The classifier {0} is loaded.", filenameToLoad);
            }
            else
            {
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //
                // As currently boosted tree classifier in MLL can only be trained
                // for 2-class problems, we transform the training database by
                // "unrolling" each training sample as many times as the number of
                // classes (26) that we have.
                //
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                using (CvMat newData = new CvMat(ntrainSamples * ClassCount, varCount + 1, MatrixType.F32C1))
                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();
//.........这里部分代码省略.........
开发者ID:healtech,项目名称:opencvsharp,代码行数:101,代码来源:LetterRecog.cs

示例3: FindExtrinsicCameraParams2Cs


//.........这里部分代码省略.........
                        CvMat _LV = new CvMat(12, 12, MatrixType.F64C1, LV);
                        CvMat _RR, _tt;
                        CvPoint3D64f* M = (CvPoint3D64f*)matM.DataDouble;
                        CvPoint2D64f* mn = (CvPoint2D64f*)_mn.DataDouble;

                        CvMat matL = new CvMat(2 * count, 12, MatrixType.F64C1);
                        double* L = matL.DataDouble;

                        for (int i = 0; i < count; i++, L += 24)
                        {
                            double x = -mn[i].X, y = -mn[i].Y;
                            L[0] = L[16] = M[i].X;
                            L[1] = L[17] = M[i].Y;
                            L[2] = L[18] = M[i].Z;
                            L[3] = L[19] = 1.0;
                            L[4] = L[5] = L[6] = L[7] = 0.0;
                            L[12] = L[13] = L[14] = L[15] = 0.0;
                            L[8] = x * M[i].X;
                            L[9] = x * M[i].Y;
                            L[10] = x * M[i].Z;
                            L[11] = x;
                            L[20] = y * M[i].X;
                            L[21] = y * M[i].Y;
                            L[22] = y * M[i].Z;
                            L[23] = y;
                        }

                        MulTransposed(matL, _LL, true);
                        SVD(_LL, _LW, null, _LV, SVDFlag.ModifyA | SVDFlag.V_T);
                        double[] LV12 = new double[12];
                        Array.Copy(LV, 11 * 12, LV12, 0, 12);
                        CvMat _RRt = new CvMat(3, 4, MatrixType.F64C1, LV12);
                        GetCols(_RRt, out _RR, 0, 3);
                        GetCol(_RRt, out _tt, 3);
                        if (Det(_RR) < 0)
                            Scale(_RRt, _RRt, -1);
                        double sc = Norm(_RR);
                        SVD(_RR, matW, matU, matV, SVDFlag.ModifyA | SVDFlag.U_T | SVDFlag.V_T);
                        GEMM(matU, matV, 1, null, 0, matR, GemmOperation.A_T);
                        Scale(_tt, _t, Norm(matR) / sc);
                        Rodrigues2_(matR, _r);
                    }
                }

                Cv.Reshape(matM, matM, 3, 1);
                Cv.Reshape(_mn, _mn, 2, 1);

                // refine extrinsic parameters using iterative algorithm
                CvLevMarq solver = new CvLevMarq(6, count * 2, new CvTermCriteria(maxIter, float.Epsilon), true);
                Copy(_param, solver.Param);

                /*
                Console.WriteLine("matM-----");
                for (int i = 0; i < matM.Rows * matM.Cols; i++)
                {
                    Console.WriteLine("{0}\t", matM[i].Val0);
                }
                Console.WriteLine("_mn-----");
                for (int i = 0; i < _mn.Rows * _mn.Cols; i++)
                {
                    Console.WriteLine(_mn[i].Val0);
                }
                Console.WriteLine("_param-----");
                for (int i = 0; i < _param.Rows * _param.Cols; i++)
                {
                    Console.WriteLine(_param[i].Val0);
                }*/

                for (; ; )
                {
                    CvMat matJ, _err, __param;
                    bool proceed = solver.Update(out __param, out matJ, out _err);
                    Copy(__param, _param);
                    if (!proceed || _err == null)
                        break;
                    Reshape(_err, _err, 2, 1);
                    if (matJ != null)
                    {
                        GetCols(matJ, out _dpdr, 0, 3);
                        GetCols(matJ, out _dpdt, 3, 6);
                        ProjectPoints2(matM, _r, _t, matA, distCoeffs,
                                          _err, _dpdr, _dpdt, null, null, null);
                    }
                    else
                    {
                        ProjectPoints2(matM, _r, _t, matA, distCoeffs, _err, null, null, null, null, null);
                    }
                    Sub(_err, _m, _err);
                    Reshape(_err, _err, 1, 2 * count);
                }
                Copy(solver.Param, _param);

                for (int i = 0; i < 3; i++)
                {
                    rvec.SetReal1D(i, param[i]);
                    tvec.SetReal1D(i, param[i + 3]);
                }
            }
            
        }
开发者ID:jorik041,项目名称:opencvsharp,代码行数:101,代码来源:Cv_F.cs


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