具有openCV的神经网络

时间:2016-06-22 16:24:34

标签: c++ opencv neural-network

我正在尝试训练神经网络。但是当我尝试设置图层时,我得到了一个例外:

An unhandled exception of type 'System.NullReferenceException' occurred.

我认为这是因为我在我用来训练它的方法之外创建网络;但是当我在里面创建它时,我得到了同样的例外。这是我的代码:

ml::ANN_MLP* nnetwork =ml::ANN_MLP::create ();

string eyes_cascadeclas2 = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier eyes_cascade2;



void TrainEye(string path) {

vector <Rect> eyes;
eyes_cascade2.load(eyes_cascadeclas2);
read_csvNN(path, images, Labels, ';');
Mat LabelsMat = Mat::zeros(72, 3, CV_32F);
Mat Images;
cout << images.size() << endl;
for (int i = 0; i < images.size(); i++) {
    Size size(30, 30);
    Mat image = imread(images[i]);
    Boolean Empty = image.empty();
    cout << Empty << endl;
    imshow("Try",image);
    Mat imageHSV;
    cvtColor(image, imageHSV, CV_BGR2HSV);
    Mat ImgRE;
    resize(imageHSV, ImgRE, size);
    vector <Mat> HSV;
    split(ImgRE, HSV);
    //pongo todos los pixeles en una fila
    HSV[0].reshape(0, 1);//Lo meto con solo la H
    Images.push_back(HSV[0]);
    vector <int> label (3);
    label = { 0,0,0 };
    label[Labels[i]-1] = 1;
    Mat ForLabel = Mat(1, 3, CV_32F, &label);
    LabelsMat.push_back(ForLabel);
}

int layers_d[] = { 200,400,100,20,3 };
Mat layers = Mat(1, 5, CV_32S);
layers.at<int>(0,0) = layers_d[0];
layers.at<int>(0,1) = layers_d[1];
layers.at<int>(0,2) = layers_d[2];
layers.at<int>(0,3) = layers_d[3];
layers.at<int>(0,4) = layers_d[4];


nnetwork->setLayerSizes(layers);
nnetwork->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM);
//Ptr<TrainData> data = ;
//ANN_MLP::TrainParams params = ANN_MLP_TrainParams(cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 1000, 0.000001), ANN_MLP_TrainParams::BACKPROP, 0.1, 0.1);
//no poner params se ajustan por defecto
int iterations = nnetwork->train(Images,ROW_SAMPLE, LabelsMat);
cout << "Number of iterations :" << endl;

}

0 个答案:

没有答案