Halcon学习之超盒分类

void testHyperboxes()
{
    //创建一个新的分类器
    HClassBox classBox;
    classBox.CreateClassBox();

    //训练一个分类器
    //参数说明: Features要学习的属性数组, Class指定属性数组被分配到的类
    HTuple Features=HTuple(0).Append(1).Append(2);
    Hlong Class=0;
    classBox.LearnClassBox(Features, Class);

    Features = HTuple(1).Append(0).Append(2);
    Class = 1;
    classBox.LearnClassBox(Features, Class);

    Features = HTuple(1).Append(2).Append(0);
    Class = 2;
    classBox.LearnClassBox(Features, Class);

    //利用分类器分类一个属性数组
    // FeatureList被分类的数组序列, Class数组序列最后被分到哪一类的返回值
    HTuple FeatureList1 = HTuple(1).Append(0).Append(3);
    Hlong Class1;
    Class1 = classBox.EnquireClassBox(FeatureList1);
    cout << " [1,0,3] 被分到了类别: " << Class1 << endl<<endl;

    //分类器的序列化和反序列化
    HSerializedItem item = classBox.SerializeClassBox();
    HClassBox serialclassBox;
    serialclassBox.DeserializeClassBox(item);

    //分类器写入或读出文件
    classBox.WriteClassBox("classBox.gbc");
    HClassBox fileclassBox;
    fileclassBox.ReadClassBox("classBox.gbc");

    //描述一个立方体分类器的类
    //参数说明:Dimensions输出的最高维度值,ClassIdx类的索引值,BoxIdx方格索引值,BoxLowerBound方格较低边, 
    //BoxHigherBound方格较高边,BoxNumSamplesTrain被用来定义方格的训练样本数,BoxNumSamplesWrong被分错到方格的训练样本数
    Hlong Dimensions=3;
    HTuple ClassIdx, BoxIdx, BoxLowerBound, BoxHigherBound, BoxNumSamplesTrain, BoxNumSamplesWrong;
    ClassIdx=classBox.DescriptClassBox(Dimensions, &BoxIdx, &BoxLowerBound, &BoxHigherBound, &BoxNumSamplesTrain, &BoxNumSamplesWrong);

    for (int i = 0; i < ClassIdx.Length(); i++)
    {
        cout << "类别: " << i << " "<<endl;
        cout << "方格较低边: " << endl;
        for (int j = 3 * i; j < 3 * i + 3; j++)
        {
            cout << (double)BoxLowerBound[j] << " ";
        }
        cout << endl;
        cout << "方格较高边: " << endl;
        for (int h = 3 * i; h < 3 * i + 3; h++)
        {
            cout << (double)BoxHigherBound[h] << " ";
        }
        cout << endl;
    }

    //利用一个投影类分类一个数组
    // FeatureList被分类的数组序列, Class数组序列最后被分到哪一类的返回值
    HTuple FeatureList2 = HTuple(1).Append(0);
    Hlong Class2;
    Class2 = classBox.EnquireRejectClassBox(FeatureList2);
    cout << " [1,0,3] 被分到了类别: " << Class2 << endl << endl;


    //获得分类器中目前的参数
    //参数说明: Flag指定系统参数的类型, Value对应参数类型的参数值
    HString Flag = "used_memory";
    HTuple Value;
    Value = classBox.GetClassBoxParam(Flag);
    cout << "使用的内存值: " << (double)Value << endl;

    //设置一个分类器的系统属性
    //参数说明: Flag指定系统参数的类型, Value对应参数类型的参数值
    HString Flag1 = "min_samples_for_split";
    HTuple Value1=81;
    classBox.SetClassBoxParam(Flag1,Value1);

}

你可能感兴趣的:(knn,halcon,超盒)