如何在OpenCV中创建空Mat? [英] How to create empty Mat in OpenCV?
本文介绍了如何在OpenCV中创建空Mat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在OpenCV中创建空Mat?创建后我想使用push_back方法在Mat中推行。
How to create empty Mat in OpenCV? After creation I want to use push_back method to push rows in Mat.
类似:
Mat M(0,3,CV_32FC1);
或只有选项是:
Mat M;
M.converTo(M,CV_32FC1);
?
推荐答案
您可以使用下列方法创建一个空矩阵:
You can create an empty matrix simply using:
Mat m;
如果您已经知道其类型,您可以执行:
If you already know its type, you can do:
Mat1f m; // Empty matrix of float
如果知道其大小 p>
If you know its size:
Mat1f m(rows, cols); // rows, cols are int
or
Mat1f m(size); // size is cv::Size
还可以添加默认值:
And you can also add the default value:
Mat1f m(2, 3, 4.1f);
//
// 4.1 4.1 4.1
// 4.1 4.1 4.1
如果您要向 push_back
的空矩阵中添加值,您可以按@berak建议的方式进行:
If you want to add values to an empty matrix with push_back
, you can do as already suggested by @berak:
Mat1f m;
m.push_back(Mat1f(1, 3, 3.5f)); // The first push back defines type and width of the matrix
m.push_back(Mat1f(1, 3, 9.1f));
m.push_back(Mat1f(1, 3, 2.7f));
// m
// 3.5 3.5 3.5
// 9.1 9.1 9.1
// 2.7 2.7 2.7
如果你需要push_back包含在 vector<>
中的数据,并将其转置。
If you need to push_back data contained in vector<>
, you should take care to put values in a matrix and transpose it.
vector<float> v1 = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f};
vector<float> v2 = {1.2f, 2.3f, 3.4f, 4.5f, 5.6f};
Mat1f m1(Mat1f(v1).t());
Mat1f m2(Mat1f(v2).t());
Mat1f m;
m.push_back(m1);
m.push_back(m2);
// m
// 1.1 2.2 3.3 4.4 5.5
// 1.2 2.3 3.4 4.5 5.6
这篇关于如何在OpenCV中创建空Mat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文