如何使用Java代码支持向量机训练我的边缘图像



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

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

//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

希望能有所帮助。如果你有任何疑问,可以随时提问,并发布你尝试过的内容。如果你给我发一些图片,我可以帮你解决问题,但你什么都不会学到,对吗?)

最新更新