Reputation: 6203
I'm creating a CvMat structure by calling
cvCreateMat(1,1,CV_32FC3);
This structure is filled by a subsequent OpenCV function call and fills it with three values (as far as I understand it, this is a 1x1 array with an additional depth of 3).
So how can I access these three values? A normal call to
CV_MAT_ELEM(myMat,float,0,0)
would not do the job since it expects only the arrays dimensions indices but not its depth. So how can I get these values?
Thanks!
Upvotes: 4
Views: 13612
Reputation: 7845
CV_32FC3
is a three channel matrix of 32-bit floats. You can access each channel by declaring a struct element with 3 floats and using CV_MAT_ELEM
. For example:
typedef struct element {
float cn1;
float cn2;
float cn3;
} myElement;
myElement data[N] = ...;
CvMat mat = cvMat(1, 1, CV_32FC2, data);
float channel1 = CV_MAT_ELEM(mat, float, 0, 0).cn1;
float channel2 = CV_MAT_ELEM(mat, float, 0, 0).cn2;
float channel3 = CV_MAT_ELEM(mat, float, 0, 0).cn3;
Edit:
Another way to access each channel is by using the underlying ptr
structure:
mat.ptr<float>(x, y) [channel];
Upvotes: 5
Reputation: 5139
The general way to access a cv::Mat is
type value=myMat.at<cv::VecNT>(j,i)[channel]
For your case:
Mat mymat(1,1,CV_32FC3,cvScalar(0.1,0.2,0.3));
float val=mymat.at<Vec3f>(0,0)[0];
All of the types are defined using the class cv::VecNT where T is the type and N is the number of vector elements.
Upvotes: 5