简体   繁体   中英

convert cv::Mat to const CvMat* or CvMat*

I know only C language, so I am getting confusion/not understanding the syntax of the openCV data types particularly in cv::Mat, CvMat*, Mat.

My question is How can I convert cv::Mat to const CvMat * or CvMat* , and can any one provide documentation link for difference between CvMat *mat and cv::Mat and Mat in opencv2.4 .

and How can I convert my int data to float data in CvMat ? Thank you

cv::Mat has a operator CvMat() so simple assignment works:

cv::Mat mat = ....;
CvMat cvMat = mat;

This uses the same underlying data so you have to be careful that the cv::Mat doesn't go out of scope before the CvMat .

If you need to use the CvMat in an API that takes a CvMat* , then pass the address of the object:

functionTakingCmMatptr(&cvMat);

As for the difference between cv::Mat and Mat , they are the same. In OpenCV examples, it is often assumed (and I don't think this is a good idea) that using namespace cv is used.

To answer especially surya's second question:

THB, the documentation on OpenCV is not the best. Here the link to the newest type: cv::Mat http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat The newer types are more modern c++ like than c style.

Here two more OpenCV forum answers with a similar topic: http://answers.opencv.org/question/65224/conversion-between-cvmat-and-cvmat/ and http://www.answers.opencv.org/question/13437/difference-between-cvmat-cvmat-cvmat-and-mat/

Especially for the conversion problem (as juanchopanza mentioned):

cv::Mat mat = cv::Mat(10, 10, CV_32FC1); //CV_32FC1 equals float 
                                         //(reads 32bit floating-point 1 channel)
CvMat cvMat = mat;

or with

using namespace cv; //this should be in the beginning where you include
Mat mat = Mat(10, 10, CV_32FC1); 
CvMat cvMat = mat;

Note: Usually you would probably work with CvMat* - but you should think about switching to the newer types completely. Example (taken from my second link):

CvMat* A = cvCreateMat(10, 10, CV_32F); //guess this works fine with no channels too

Changing int to float:

CvMat* A = cvCreateMat(10, 10, CV_16SC1);
//Feed A with data
CvMat* B = cvCreateMat(10, 10, CV_32FC1);
for( int i=0; i<10; ++i) 
    for( int i=0; i<10; ++i) 
        CV_MAT_ELEM(*A, float, i, j) = (float) cvmGet(B, i, j);
//Don't forget this unless you want to produce a memory leak. 
cvReleaseMat(&A);
cvReleaseMat(&B);

The first two examples (without the pointer) are fine like that as the CvMat is held on the heap then. cvCreateMat(...) allocates memory you have to free on your own later. Another reason to use cv::Mat .

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