1、Mat——>vector
:
Mat m;
vector<Point3f> p;
p=Mat_<Point3f>(m)
2、vector
:
vector<Point3f> p;
Mat m=Mat(p);
3、vector
vector<Point3f> p1,p2,p3;
vector<vectoe<Point3f>> pp;
pp.pushback(p1);
pp.pushback(p2);
pp.pushback(p3);
4、vector
vector<vector<Point3f>> p;
Mat pm((int)p.size(), p[0].size(), CV_32FC3);
for( int i = 0; i < (int)p.size(); i++ )
{
Mat r = pm.row(i).reshape(3, pm.cols);
Mat pm1(p[i]);
pm1.copyTo(r);
}
#include
using namespace cv;
using namespace std;
/***************** Mat转vector **********************/
template<typename _Tp>
vector<_Tp> convertMat2Vector(const Mat &mat)
{
return (vector<_Tp>)(mat.reshape(1, 1));//通道数不变,按行转为一行
}
/****************** vector转Mat *********************/
template<typename _Tp>
cv::Mat convertVector2Mat(vector<_Tp> v, int channels, int rows)
{
cv::Mat mat = cv::Mat(v);//将vector变成单列的mat
cv::Mat dest = mat.reshape(channels, rows).clone();//PS:必须clone()一份,否则返回出错
return dest;
}
int main()
{
/* char ->CV_8SC
* unsigned char,uchar ->CV_8UC
* unsigned short int,ushort->CV_16UC
* short int->CV_16SC
* int ->CV_32SC
* float ->CV_32FC
* double->CV_64FC
*/
Mat srcDate=Mat(3,3,CV_32FC3,Scalar(2));
vector<float> v = convertMat2Vector<float>(srcDate);
cv::Mat dest = convertVector2Mat<float>(v, 3, 3);//把数据转为3通道,3行的Mat数据
cout << "dest=\n" << dest << endl;
system("pause");
waitKey();
return 0;
}
使用Mat存储数据,并读取相应元素
Mat mean = (cv::Mat_<float>(2, 1) << 0.4404, 0.3111);
cout << "mean=" << mean << endl;
float a = mean.at<float>(0, 0);
float b = mean.at<float>(1, 0);
将数组内容传递给Mat
unsigned char cbuf[height][width];
cv::Mat img(height, width, CV_8UC1, (unsigned char*)cbuf);
pp
和output
的区别在于:pp
是vector
而output
是vector
#include
using namespace std;
using namespace cv;
int main()
{
//定义3*1的矩阵m1、m2、m3
Mat m1 = Mat(3, 1, CV_32F, Scalar(1));
Mat m2 = Mat(3, 1, CV_32F, Scalar(2));
Mat m3 = Mat(3, 1, CV_32F, Scalar(3));
//把Mat数组转换成Point3f点
vector<Point3f> p1;
p1 = Mat_<Point3f>(m1);
vector<Point3f> p2;
p2 = Mat_<Point3f>(m2);
vector<Point3f> p3;
p3 = Mat_<Point3f>(m3);
//把Point3f点放入pp中
vector<vector<Point3f>> pp;
pp.push_back(p1);
pp.push_back(p2);
pp.push_back(p3);
cout << pp[0] << endl;
//先把m1m2m3转换成Point3f然后存储在vector中
vector<Point3f> output;
output.push_back(Point3f(m1.at<Point3f>(0, 0)));
output.push_back(Point3f(m2.at<Point3f>(0, 0)));
output.push_back(Point3f(m3.at<Point3f>(0, 0)));
cout << output << endl;
}