如何使用Java代码SVM训练我的Edge图像

时间:2017-02-14 17:56:09

标签: svm

我有一组图像,我使用OpenCV 3.1进行边缘检测。边缘存储在OpenCV的MAT中。有人可以帮我处理这些图像集上的Java SVM训练和测试代码吗?

1 个答案:

答案 0 :(得分:0)

在评论中进行讨论后,我将为您提供一个我为android studio一段时间构建的示例项目。 这用于根据Lab颜色空间对图像进行分类。

//1.a Assign the parameters for SVM training here
    double nu = 0.999D;
    double gamma = 0.4D;
    double epsilon = 0.01D;
    double coef0 = 0;
    //kernel types are Linear(0), Poly(1), RBF(2), Sigmoid(3)
    //For Poly(1) set degree and gamma
    double degree = 2;
    int kernel_type = 4;
//1.b Create an SVM object
    SVM B_channel_svm = SVM.create();
    B_channel_svm.setType(104);
    B_channel_svm.setNu(nu);
    B_channel_svm.setCoef0(coef0);
    B_channel_svm.setKernel(kernel_type);
    B_channel_svm.setDegree(degree);
    B_channel_svm.setGamma(gamma);
    B_channel_svm.setTermCriteria(new TermCriteria(2, 10, epsilon));
    // Repeat Step 1.b for the number of SVMs. 
//2. Train the SVM 
    // Note: training_data - If your image has n rows and m columns, you have to make a matrix of size (n*m, o), where o is the number of labels. 
    // Note: Label_data is same as above, n rows and m columns, make a matrix of size (n*m, o) where o is the number of labels.
    // Note: Very Important - Train the SVM for the entire data as training input and the specific column of the Label_data as the Label. Here, I train the data using B, G and R channels and hence, the name B_channel_SVM. I make 3 different SVM objects separately but you can do this by creating only one object also.       
    B_channel_svm.train(training_data, Ml.ROW_SAMPLE, Label_data.col(0));
    G_channel_svm.train(training_data, Ml.ROW_SAMPLE, Label_data.col(1));
    R_channel_svm.train(training_data, Ml.ROW_SAMPLE, Label_data.col(2));
    // Now after training we "predict" the outcome for a sample from the trained SVM. But first, lets prepare the Test data. 
    // As above for the training data, make a matrix of (n*m, o) and use the columns to predict. So, since I created 3 different SVMs, I will input three separate matrices for the three SVMs of size (n*m, 1).
//3. Predict the testing data outcome using the trained SVM. 
    B_channel_svm.predict(scene_ml_input, predicted_final_B, StatModel.RAW_OUTPUT);
    G_channel_svm.predict(scene_ml_input, predicted_final_G, StatModel.RAW_OUTPUT);
    R_channel_svm.predict(scene_ml_input, predicted_final_R, StatModel.RAW_OUTPUT);
//4. Here, predicted_final_ are matrices which gives you the final value as in Label(0,1,2... etc) for the input data (edge profile in your case)

现在,我希望您对SVM的工作原理有所了解。您基本上需要执行以下步骤:

第1步:识别标签 - 在您的情况下从边缘配置文件中进行手势。

步骤2 :为标签指定值 - 例如,如果您尝试对触觉手势进行分类 - 打开手= 1,闭手/拳头= 2,拇指向上= 3等等。

步骤3 :根据上述过程准备训练数据(边缘轮廓)和标签(1,2,3)等。

步骤4 :使用SVM计算的转换为预测准备数据。

对OpenCV上的SVM非常重要 - 规范化您的数据,确保所有矩阵的类型相同 - CvType

希望它有所帮助。如果您有任何疑问,请随时提出问题并发布您尝试过的内容。如果你发给我一些图片,我可以为你解决问题但是你不会学到任何东西吗? ;)