简体   繁体   中英

Retrieve elements of a CV_32FC3 CvMat?

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!

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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