简体   繁体   English

将cv :: Mat转换为const CvMat *或CvMat *

[英]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. 我只知道C语言,所以我感到困惑/不理解openCV数据类型的语法,特别是在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 . 我的问题是如何将cv::Mat转换为const CvMat *或CvMat* ,并且任何人都可以在opencv2.4中提供CvMat *matcv::MatMat之间差异的文档链接。

and How can I convert my int data to float data in CvMat ? 如何将我的int数据转换为CvMat中的浮点数据? Thank you 谢谢

cv::Mat has a operator CvMat() so simple assignment works: cv::Mat有一个运算符CvMat(),所以简单的赋值工作:

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 . 这使用相同的底层数据,因此您必须注意cv::MatCvMat之前不会超出范围。

If you need to use the CvMat in an API that takes a CvMat* , then pass the address of the object: 如果您需要使用CvMat ,需要一个一个API中CvMat* ,然后传递对象的地址:

functionTakingCmMatptr(&cvMat);

As for the difference between cv::Mat and Mat , they are the same. 至于cv::MatMat之间的区别,它们是相同的。 In OpenCV examples, it is often assumed (and I don't think this is a good idea) that using namespace cv is used. OpenCV示例中,通常假定using namespace cv (并且我认为这不是一个好主意)。

To answer especially surya's second question: 特别回答surya的第二个问题:

THB, the documentation on OpenCV is not the best. THB,关于OpenCV的文档不是最好的。 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. 这里是最新类型的链接:cv :: Mat http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat较新的类型比c风格更现代的c ++。

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/ 这里还有两个OpenCV论坛答案,其中有类似的主题: http//answers.opencv.org/question/65224/conversion-between-cvmat-and-cvmat/http://www.answers.opencv.org/question/ 13437 /差之间-与CvMat-与CvMat-CvMat中和垫/

Especially for the conversion problem (as juanchopanza mentioned): 特别是对于转换问题(如提到的juanchopanza):

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. 注意:通常你可能会使用CvMat* - 但你应该考虑完全切换到更新的类型。 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: 将int更改为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. 前两个例子(没有指针)很好,因为CvMat然后保存在堆上。 cvCreateMat(...) allocates memory you have to free on your own later. cvCreateMat(...)分配你必须在以后自由释放的内存。 Another reason to use cv::Mat . 使用cv::Mat另一个原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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