JavaCV

入门 ANN ——JavaCV

最近在看关于图像识别,想尝试使用Javacv,看到资料比较少所以把最近看的写出来,总结一下:
注意:
1.本次使用javacv其中版本为opencv3.1,各版本中的Mat有些差异,尤其注意CvMat与Mat的转化

2.在ANN(人工神经网络)的用法中一定要先定义网络层数,再定义激活函数,否则可能输出为NaN!(用一天的时间换来的经验啊!)

package test.ANN;

import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.opencv_ml.ANN_MLP;
import org.opencv.ml.Ml;

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_highgui.*;

import java.io.File; 

public class ANN_test {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ANN_MLP ann = ANN_MLP.create();
//      if(new File("D://ann.xml").exists())ANN_MLP.loadANN_MLP("D://ann.xml",ann.toString());
         ann.setTrainMethod(ANN_MLP.BACKPROP);
//       ann.setRpropDW0(0.1);
//       ann.setRpropDWPlus(0.1);
//       ann.setRpropDWMinus(0.1);
//       ann.setRpropDWMin(0.1);
//       ann.setRpropDWMax(0.5);
//       ann.setBackpropWeightScale(0.9);
//       ann.setBackpropMomentumScale(0.9);
         TermCriteria t=new TermCriteria(TermCriteria.MAX_ITER,5000,0.001);
         ann.setTermCriteria(t);
        // setup training data
        int[][] labelData = new int[][] { { 1, 0, 0 },
                                        { 0, 1, 0 },
                                        { 0, 0, 1 },
                                        { 0, 1, 0 },
                                        { 1, 0, 0 } };
        CvMat labelsMat = new CvMat(new Mat(5, 3, CV_32FC1));
        for(int i=0;i<5;i++){
            for(int j=0;j<3;j++){
                labelsMat.put(i,j,labelData[i][j]);
            }
        }
        int[][] trainData = new int[][] { { 1, 2, 3, 4, 5 },
                                            { 11, 12, 13, 14, 15 },
                                            { 111, 112, 113, 114, 115 },
                                            { 11, 12, 13, 14, 15 },
                                            { 1, 2, 3, 4, 5 } };
        CvMat trainMat = new CvMat(new Mat(5, 5, CV_32FC1));
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                trainMat.put(i, j, trainData[i][j]);
            }
        }
        int[] layer=new int[]{5,2,3};
        CvMat layerSizes = new CvMat(new Mat(1,layer.length,CV_32FC1));
        for(int i=0;i0, i, layer[i]);

        }
        //设置激活函数一定要在设置层数之后!!!!
        ann.setLayerSizes(new Mat(layerSizes));
        ann.setActivationFunction(ANN_MLP.SIGMOID_SYM);
        ann.train(new Mat(trainMat),opencv_ml.ROW_SAMPLE, new Mat(labelsMat));
        ann.save("D://ann.xml");
        int[]newSample=new int[]{111, 112, 113, 115, 114};
        CvMat newDataMat = new CvMat(new Mat(1, 5, CV_32FC1));
        for (int i = 0; i < 5; i++) {
            newDataMat.put(0,i, newSample[i]);
        }
        Mat predict = new Mat();
        ann.predict(new Mat(newDataMat),predict,ANN_MLP.UPDATE_MODEL);
        CvMat result = new CvMat(predict);
        for (int i = 0; i < 3; i++) {
                 System.out.print(result.get(0,i) + " ");
        }
    }
}

输出
-0.003799209138378501
0.002347266301512718
0.9986994862556458

你可能感兴趣的:(图像识别)