简体   繁体   English

在OpenCV中将图像转换为CvMat以训练神经网络

[英]Converting image into CvMat in OpenCV for training neural network

I'm writing a program which uses OpenCv neural networks module along with C# and OpenCvSharp library. 我正在编写一个使用OpenCv神经网络模块以及C#和OpenCvSharp库的程序。 It must recognise the face of user, so in order to train the network, i need a set of samples. 它必须识别用户的面孔,因此为了训练网络,我需要一组样本。 The problem is how to convert a sample image into array suitable for training. 问题是如何将样本图像转换为适合训练的阵列。 What i've got is 200x200 BitMap image, and network with 40000 input neurons, 200 hidden neurons and one output: 我得到的是200x200 BitMap图像,以及具有40000个输入神经元,200个隐藏神经元和一个输出的网络:

        CvMat layerSizes = Cv.CreateMat(3, 1, MatrixType.S32C1);
        layerSizes[0, 0] = 40000;
        layerSizes[1, 0] = 200;
        layerSizes[2, 0] = 1;
        Network = new CvANN_MLP(layerSizes,MLPActivationFunc.SigmoidSym,0.6,1);

So then I'm trying to convert BitMap image into CvMat array: 所以然后我试图将BitMap图像转换为CvMat数组:

private void getTrainingMat(int cell_count, CvMat trainMAt, CvMat responses)
    {
        CvMat res = Cv.CreateMat(cell_count, 10, MatrixType.F32C1);//10 is a number of samples
        responses = Cv.CreateMat(10, 1, MatrixType.F32C1);//array of supposed outputs
        int counter = 0;
        foreach (Bitmap b in trainSet)
        {
            IplImage img = BitmapConverter.ToIplImage(b);
            Mat imgMat = new Mat(img);
            for (int i=0;i<imgMat.Height;i++)
            {
                for (int j = 0; j < imgMat.Width; j++)
                {
                    int val =imgMat.Get<int>(i, j);
                    res[counter, 0] = imgMat.Get<int>(i, j);
                }
                responses[i, 0] = 1;
            }
            trainMAt = res;
        }
    }

And then, when trying to train it, I've got this exception: 然后,在尝试对其进行训练时,我遇到了以下异常:

input training data should be a floating-point matrix withthe number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer 输入的训练数据应为浮点矩阵,其行数等于训练样本数,列数等于第0(输入)层的大小

Code for training: 培训代码:

        trainMAt = Cv.CreateMat(inp_layer_size, 10, MatrixType.F32C1);
        responses = Cv.CreateMat(inp_layer_size, 1, MatrixType.F32C1);
        getTrainingMat(inp_layer_size, trainMAt, responses);
        Network.Train(trainMAt, responses, new CvMat(),null, Parameters);

I'm new to OpenCV and I think I did something wrong in converting because of lack of understanding CvMat structure. 我是OpenCV的新手,我认为由于缺乏对CvMat结构的了解,在转换时做错了一些事情。 Where is my error and is there any other way of transforming the bitmap? 我的错误在哪里,还有其他转换位图的方法吗?

With the number of rows equal to the number of training samples 行数等于训练样本数

That's 10 samples. 那是10个样本。

and the number of columns equal to the size of 0-th (input) layer 并且列数等于第0(输入)层的大小

That's inp_layer_size. 那是inp_layer_size。

trainMAt = Cv.CreateMat(10, inp_layer_size, MatrixType.F32C1);
responses = Cv.CreateMat(10, 1, MatrixType.F32C1); // 10 labels for 10 samples

I primarily do C++, so forgive me if I'm misunderstanding, but your pixel loop will need adapting in addition. 我主要使用C ++,所以如果我误会我,请原谅我,但您的像素循环还需要进行调整。

Your inner loop looks broken, as you assign to val , but never use it, and also never increment your counter. 当您分配给val ,您的内部循环看起来val ,但是从不使用它,也从不增加您的计数器。

In addition, in your outer loop assigning trainMAt = res; 另外,在您的外循环中,分配trainMAt = res; for every image doesn't seem like a very good idea. 因为每个图像似乎都不是一个好主意。

I am certain you will get it to operate correctly, just keep in mind the fact that the goal is to flatten each image into a single row, so you end up with 10 rows and inp_layer_size columns. 我确信您可以使它正常运行,只需记住以下事实:目标是将每个图像展平为一行,因此最终inp_layer_size 10行和inp_layer_size列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM