OpenCV用数据填充CvMat并验证它

时间:2011-04-10 17:36:21

标签: pointers opencv

我有一个名为data

的TrainingSets(下面的结构)向量
class TrainingSet
{
public:
int time;
float input[2];
float output[3*NUM_TRACKING_POINTS];

TrainingSet(int t, float in[2], float out[3*NUM_TRACKING_POINTS])
{
    time = t;
    for (int i = 0; i < 2; i++)
        input[i] = in[i];
    for (int i = 0; i < 3*NUM_TRACKING_POINTS; i++)
        output[i] = out[i];
}
TrainingSet()
{

}
};

然后我尝试获取此Vector的内容,并将它们放入CvMats中以便训练神经网络。

int datasize = data.size();
float** in = new float*[datasize];
float** out = new float*[datasize];

for (int i = 0; i < datasize; i++) {
    in[i] = new float[2*TIME_STEPS];
    out[i] = new float[3*NUM_TRACKING_POINTS];
}

for ( int i = 0 ; i < datasize; i ++)
{
    // get the first set in the sequence.
    TrainingSet tset = data.front();
    data.pop();

    // get the inputs
    in[i] = new float[2*TIME_STEPS];
    in[i][0] = tset.input[0];
    in[i][1] = tset.input[1];

    // get the outputs
    out[i] = new float[3*NUM_TRACKING_POINTS];
    for (int j = 0; j < 3*NUM_TRACKING_POINTS; j++)
        out[i][j] = tset.output[j];

    for (int j = 2; j < 2*TIME_STEPS; j++)
    {
        if (i == 0)
            in[i][j] = 0.0f;
        else
            in[i][j] = in[i - 1][j - 2];
    }
}

// make matrices from data.
CvMat *trainInput = cvCreateMat(datasize, 2*TIME_STEPS, CV_32FC1);
cvInitMatHeader(trainInput, datasize, 2*TIME_STEPS, CV_32FC1, in);
CvMat *trainOutput = cvCreateMat(datasize, 3*NUM_TRACKING_POINTS, CV_32FC1);
cvInitMatHeader(trainOutput, datasize, 3*NUM_TRACKING_POINTS, CV_32FC1, out);

for (int x = 0; x < datasize; x++)
{
    cout << "IN: ";
    for (int y = 0; y < 2*TIME_STEPS; y++)
        cout << cvmGet(trainInput, x, y) << " ";
    cout << endl << "IN: ";
    for (int y = 0; y < 2*TIME_STEPS; y++)
        cout << in[x][y] << " ";
    cout << endl << "OUT: ";
    for (int y = 0; y < 3 * NUM_TRACKING_POINTS; y++)
        cout << cvmGet(trainOutput, x, y) << " ";
    cout << endl << "OUT: ";
    for (int y = 0; y < 3 * NUM_TRACKING_POINTS; y++)
        cout << out[x][y] << " ";
    cout << endl << endl;
}

最后一个forloop是检查矩阵内容是否是我刚刚提供的数据,但它们不匹配。矩阵似乎有完全不同的数据。

关于出了什么问题的任何想法?

1 个答案:

答案 0 :(得分:0)

我觉得inout不是连续的数组,而是一个指针数组。

我认为cvMat需要一个连续的内存阵列才能对其进行操作。

一旦你创建了数组,你就不需要从它创建一个CvMat了 使用

CvSetData( header, data ).