How to SVM Train my Edge images using Java code

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 18:35:58

问题


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 ?


回答1:


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)

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

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

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: Prepare the training data (edge profiles) and Labels (1,2,3) etc. according to the process above.

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

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? ;)



来源:https://stackoverflow.com/questions/42233031/how-to-svm-train-my-edge-images-using-java-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!