C# "EmguCV" 中 OpenCV 的 "getDecisionFunction" 等价于什么?

What is the equivalent of "getDecisionFunction" from OpenCV in C# "EmguCV"?

我正在将此代码从 C++ 转换为 C#

Mat alpha, svidx;
double rho = svm->getDecisionFunction(0, alpha, svidx);

我在 C# 的 EmguCV 中找不到 getDecisionFunction 的等价物。

我认为getDecisionFunctionEmguCV中没有明确的功能。我什至没有找到直接获取 rhoalphasvidx 所需值的方法。我找到的唯一解决方案 - 感谢@deon cagadoes - 是使用 OpenCV OpenCV SVM 的开源重写函数。但是,我无法直接获取所有数据,所以我不得不将 SVM 的值写入一个文件,然后从该文件中获取我需要的所有数据。

private void TrainandLoad()
{   
    double rho;
    Mat sv, alpha, svidx;
    SVM svm;
    svm.Train(train_data, Emgu.CV.ML.MlEnum.DataLayoutType.RowSample, labels);
    svm.Save(TRAINED_SVM);
    sv = svm.GetSupportVectors();
    alpha = new Mat();
    svidx = new Mat();
    rho = GetDecisionFunction(0, alpha, svidx);
}

private double GetDecisionFunction(int i, IOutputArray alpha, IOutputArray svidx)
{
    int j, nl, idf, class_count, sv_total, df_size, df_index_size, count;
    string svmType, rem;
    Mat tmp;
    DecisionFunc df;
    int[] sv_count;
    string[] lines;
    DecisionFunc[] dfs;
    int[][] svind;
    double[][] alph;
    lines = File.ReadAllLines(TRAINED_SVM);
    nl = 0;
    svmType = "";
    while (nl < lines.Length)
    {
        if (lines[nl].IndexOf("svmType") != -1)
        {
            svmType = lines[nl].Substring(lines[nl].IndexOf("svmType") + 9);
            nl++;
            break;
        }
        nl++;
    }
    if (svmType == "C_SVC" || svmType == "NU_SVC")
    {
        class_count = 2;
        while (nl < lines.Length)
        {
            if (lines[nl].IndexOf("class_count") != -1)
            {
                class_count = Convert.ToInt32(lines[nl].Substring(lines[nl].IndexOf("class_count") + 13));
                nl++;
                break;
            }
            nl++;
        }
        df_size = class_count * (class_count - 1) / 2;
    }
    else
    {
        if (svmType == "ONE_CLASS")
            class_count = 1;
        else
            class_count = 0;
        df_size = 1;
    }
    sv_total = 1;
    while (nl < lines.Length)
    {
        if (lines[nl].IndexOf("sv_total") != -1)
        {
            sv_total = Convert.ToInt32(lines[nl].Substring(lines[nl].IndexOf("sv_total") + 10));
            nl++;
            break;
        }
        nl++;
    }
    Debug.Assert(i <= 0 && i < df_size);
    while (nl < lines.Length)
    {
        if (lines[nl].IndexOf("decision_functions") != -1)
        {
            nl++;
            break;
        }
        nl++;
    }
    sv_count = new int[df_size];
    dfs = new DecisionFunc[df_size];
    svind = new int[df_size][];
    alph = new double[df_size][];
    idf = dfs[0].ofs = df_index_size = 0;
    while (nl < lines.Length && idf < df_size)
    {
        if (lines[nl].IndexOf("sv_count") != -1)
            sv_count[idf] = Convert.ToInt32(lines[nl].Substring(lines[nl].IndexOf("sv_count") + 10));
        else if (lines[nl].IndexOf("rho") != -1)
            dfs[idf].rho = Convert.ToDouble(lines[nl].Substring(lines[nl].IndexOf("rho") + 5));
        else if (lines[nl].IndexOf("alpha") != -1)
        {
            alph[idf] = new double[sv_count[idf]];
            rem = lines[nl].Substring(lines[nl].IndexOf("alpha") + 9);
            for (j = 0; j < sv_count[idf]; j++)
            {
                while (rem[0] == ' ')
                    rem = rem.Substring(1);
                alph[idf][j] = Convert.ToDouble(rem.Substring(0, rem.IndexOf(' ')));
                rem = rem.Substring(rem.IndexOf(' '));
            }
            if (idf != 0)
                dfs[idf].ofs = dfs[idf - 1].ofs + sv_count[idf];
            df_index_size += sv_count[idf];
            if (class_count < 2)
                idf++;
        }
        else if (lines[nl].IndexOf("index") != -1)
        {
            svind[idf] = new int[sv_count[idf]];
            rem = lines[nl].Substring(lines[nl].IndexOf("index") + 9);
            for (j = 0; j < sv_count[idf]; j++)
            {
                while (rem[0] == ' ')
                    rem = rem.Substring(1);
                svind[idf][j] = Convert.ToInt32(rem.Substring(0, rem.IndexOf(' ')));
                rem = rem.Substring(rem.IndexOf(' '));
            }
            idf++;
        }
        nl++;
    }
    df = dfs[i];
    //Get SV Count
    count = (i < df_size - 1 ? dfs[i + 1].ofs : df_index_size) - dfs[i].ofs;
    if (class_count < 2)
    {
        svind[0] = new int[sv_total];
        for (j = 0; j < sv_total; j++)
            svind[0][j] = j;
    }
    tmp = new Mat(1, count, DepthType.Cv64F, 1);
    tmp.SetTo<double>(alph[i]);
    tmp.CopyTo(alpha);
    tmp.Dispose();
    tmp = new Mat(1, count, DepthType.Cv32S, 1);
    tmp.SetTo<int>(svind[i]);
    tmp.CopyTo(svidx);
    tmp.Dispose();
    return df.rho;
}

我希望我做对了,我希望是否有人可以根据我的试验提供解决这个问题的痛苦方法。