开发者

OpenCV populating CvMat with data and verifying it

I have a vector of TrainingSets(struct below) called data

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()
{

}
};

And then I try to take the contents of this Vector, and put them into CvMats for the purpose of training a Neural Network.

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++)
      开发者_StackOverflow社区  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;
}

That last forloop is to check to see if the matrices contents are the data I just fed it, but they don't match. The Matrices seem to have completely different data.

Any thoughts on what is going wrong?


Seems to me that in and out are not a contiguous array, but an array of pointers.

I think the cvMat needs a contiguous memory array to be able to operate on it.

Also once you create the array, you don't need to create a CvMat from it, just use the

CvSetData( header, data ).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜