如何使用 Java 代码 SVM 训练我的 Edge 图像 [英] How to SVM Train my Edge images using Java code

查看:39
本文介绍了如何使用 Java 代码 SVM 训练我的 Edge 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have set of images on which I performed edge detection using OpenCV 3.1. The edges are stored in MAT of OpenCV. Can someone help me in processing for Java SVM train and test code on those set of images ?

推荐答案

根据评论中的讨论,我为您提供了一个示例项目,该项目是我不久前为 android studio 构建的.这用于根据 Lab 颜色空间对图像进行分类.

Following discussion in comments I am providing you with an example project which I built for android studio a while back. This was used to classify images depending on Lab color spaces.

//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 的工作原理有所了解.您基本上需要执行以下步骤:

Now, I hope you have an idea for how SVM works. You basically need to do these steps:

第 1 步:识别标签 - 在您的情况下是边缘轮廓的手势.

Step 1: Identify labels - In your case Gestures from edge profile.

第 2 步:为标签分配值 - 例如,如果您尝试对触觉手势进行分类 - 张开手 = 1、合上手/拳头 = 2、竖起大拇指 = 3 等等.

Step 2: Assign values to the labels - For example, if you are trying to classify haptic gestures - Open Hand = 1, Closed Hand/Fist = 2, Thumbs up = 3 and so on.

Step 3:按照上述流程准备训练数据(edge profile)和Labels(1,2,3)等.

Step 3: Prepare the training data (edge profiles) and Labels (1,2,3) etc. according to the process above.

第 4 步:使用 SVM 计算的转换准备用于预测的数据.

Step 4: Prepare data for prediction using the transformation calculated using SVM.

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

Very Important for SVM on OpenCV - Normalize your data, make sure you all matrices are of Same Type - CvType

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

Hope it helps. Feel free to ask questions if you have any doubts and post what you have tried. I can solve the problem for you if you send me some images but then you won't learn anything right? ;)

这篇关于如何使用 Java 代码 SVM 训练我的 Edge 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆