将轮廓点的向量复制到垫子中
Copy Vector of Contour Points into Mat
我正在使用 OpenCV 3.1 和 VS2012 C++/CLI。
我已将 finContours 调用的结果存储到:
std::vector<std::vector<Point>> Contours;
因此,Contours[0]是第一个轮廓的轮廓点向量。
Contours[1]是第二个向量等高线点的向量
现在,我想将其中一个轮廓加载到基于 Convert Mat to vector <float> and Vector<float> to mat in opencv 的垫子中,我认为这样的方法可行。
Mat testMat=Mat(Images->Contours[0].size(),2,CV_32FC1);
memcpy(testMat.data,Images->Contours[0].data(),Images->Contours[0].size()*CV_32FC1);
我指定了两列,因为我每个基础 pint 必须由 X 点和 Y 点组成,并且每个都应该是浮点数。但是,当我访问 Mat 元素时,我可以看到第一个元素不是基础数据,而是轮廓点的总数。
感谢您以正确的方式完成此任务的任何帮助。
你可以这样做:
Mat testMat = Mat(Images->Contours[0]).reshape(1);
现在 testMat
是 CV_32SC1
类型,又名 int
。如果您需要 float
,您可以:
testMat.convertTo(testMat, CV_32F);
更多细节和变体...
您可以简单地使用接受 std::vector
:
的 Mat
构造函数
vector<Point> v = { {0,1}, {2,3}, {4,5} };
Mat m(v);
有了这个,你会得到一个 2 通道矩阵,其中的基础数据在 v
中。这意味着如果您更改 v
中的值,m
中的值也会更改。
v[0].x = 7; // also 'm' changes
如果你想要一个 深度 值的副本,这样 v
中的更改不会反映在 m
中,你可以使用 clone
:
Mat m2 = Mat(v).clone();
您的矩阵是 CV_32SC2
类型,即 int
的 2 通道矩阵(因为 Point
使用 int
。对 [=21 使用 Point2f
=]).如果你想要一个 2 列的单通道矩阵,你可以使用 reshape
:
Mat m3 = m2.reshape(1);
如果要转换为float
类型,需要使用convertTo
:
Mat m4;
m2.convertTo(m4, CV_32F);
这里有一些工作代码作为概念证明:
#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
vector<Point> v = { {0,1}, {2,3}, {4,5} };
// changes in v affects m
Mat m(v);
// changes in v doesn't affect m2
Mat m2 = Mat(v).clone();
// m is changed
v[0].x = 7;
// m3 is a 2 columns single channel matrix
Mat m3 = m2.reshape(1);
// m4 is a matrix of floats
Mat m4;
m2.convertTo(m4, CV_32F);
return 0;
}
我正在使用 OpenCV 3.1 和 VS2012 C++/CLI。
我已将 finContours 调用的结果存储到:
std::vector<std::vector<Point>> Contours;
因此,Contours[0]是第一个轮廓的轮廓点向量。 Contours[1]是第二个向量等高线点的向量
现在,我想将其中一个轮廓加载到基于 Convert Mat to vector <float> and Vector<float> to mat in opencv 的垫子中,我认为这样的方法可行。
Mat testMat=Mat(Images->Contours[0].size(),2,CV_32FC1);
memcpy(testMat.data,Images->Contours[0].data(),Images->Contours[0].size()*CV_32FC1);
我指定了两列,因为我每个基础 pint 必须由 X 点和 Y 点组成,并且每个都应该是浮点数。但是,当我访问 Mat 元素时,我可以看到第一个元素不是基础数据,而是轮廓点的总数。
感谢您以正确的方式完成此任务的任何帮助。
你可以这样做:
Mat testMat = Mat(Images->Contours[0]).reshape(1);
现在 testMat
是 CV_32SC1
类型,又名 int
。如果您需要 float
,您可以:
testMat.convertTo(testMat, CV_32F);
更多细节和变体...
您可以简单地使用接受 std::vector
:
Mat
构造函数
vector<Point> v = { {0,1}, {2,3}, {4,5} };
Mat m(v);
有了这个,你会得到一个 2 通道矩阵,其中的基础数据在 v
中。这意味着如果您更改 v
中的值,m
中的值也会更改。
v[0].x = 7; // also 'm' changes
如果你想要一个 深度 值的副本,这样 v
中的更改不会反映在 m
中,你可以使用 clone
:
Mat m2 = Mat(v).clone();
您的矩阵是 CV_32SC2
类型,即 int
的 2 通道矩阵(因为 Point
使用 int
。对 [=21 使用 Point2f
=]).如果你想要一个 2 列的单通道矩阵,你可以使用 reshape
:
Mat m3 = m2.reshape(1);
如果要转换为float
类型,需要使用convertTo
:
Mat m4;
m2.convertTo(m4, CV_32F);
这里有一些工作代码作为概念证明:
#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
vector<Point> v = { {0,1}, {2,3}, {4,5} };
// changes in v affects m
Mat m(v);
// changes in v doesn't affect m2
Mat m2 = Mat(v).clone();
// m is changed
v[0].x = 7;
// m3 is a 2 columns single channel matrix
Mat m3 = m2.reshape(1);
// m4 is a matrix of floats
Mat m4;
m2.convertTo(m4, CV_32F);
return 0;
}