写一个程序,想将一副图片保存为一维的mat数据,用reshape函数一直错误,改了很久没找到方法,直到看了一篇博客,感谢大神,保存下来学习用,转载自http://blog.csdn.net/mingzhentanwo/article/details/45695609
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
void PrintMat( Mat &_m)
{
Mat m = Mat_<float>( _m );
for ( int k1=0; k1<m.rows; k1++ )
{
for ( int k2=0; k2<m.cols; k2++ )
{
cout << m.at<float>(k1,k2) << " ";
}
cout << endl;
}
cout << "m.rows: " << _m.rows << endl << "m.cols: " << _m.cols << endl;
}
int main()
{
Mat m = Mat::zeros(2,2,CV_32F);
m.at<float>(0,0) = 1;
m.at<float>(0,1) = 2;
m.at<float>(1,0) = 3;
m.at<float>(1,1) = 4;
m.reshape(0, 1); //没有赋值给m,只是当当reshape的话,m的行列都不会显示出来有改变
cout << "m:" << endl;
PrintMat(m);
cout << endl;
cout << "m.reshape(0, 1):" << endl;
m = m.reshape(0, 1);//赋值给m的后,m的行列发生改变
cout << "m:" << endl;
PrintMat(m) ;
cout << endl;
cout << "m.reshape(0, 4):" << endl;
Mat m1 = m.reshape(0, 4); //也可以赋值给新的mat
cout << "m1:" << endl;
PrintMat(m1);
cout << endl;
return 0;
}
reshape的使用
最新推荐文章于 2024-05-15 20:38:32 发布