doc
doc

Reputation: 71

OpenCV CvMat to Mat and pointers

I'm trying to convert some code from using OpenCV's CvMat to Mat but am having some trouble with pointers.

This is the original code:

CvMat *m_projectedTrainFaceMat;
float d_i;
i = 0;
d_i = projectTestFace[i] - m_projectedTrainFaceMat->data.fl[iTrain * m_nEigens + i];

Here is my updated code:

Mat *m_projectedTrainFaceMat;
float d_i;
i = 0;
d_i = projectTestFace[i] - m_projectedTrainFaceMat->data[iTrain * m_nEigens + i];

As you can see, I removed the .fl from the call to data but I can't quite figure out how to return the data as floats.

Any suggestions? I tried casting it to float and (float *) but they ended up causing errors/mistakes.

e: projectTestFace is a float*.

Upvotes: 1

Views: 3760

Answers (1)

lightalchemist
lightalchemist

Reputation: 10219

Andrey's code above is correct assuming that your memory is continuous. There are cases when that is not and we should not assume. Perhaps a more straightforward way (without having to use pointers) would be as follows:

d_i = projectTestFace[i] - (*m_projectedTrainFaceMat).at<float>(row, col);

Similar to Andrey I'm assuming that your m_projectedTrainFaceMat is intialized as having CV_32FC1 elements.

Upvotes: 1

Related Questions