原文:http://blog.csdn.net/lihuajie1003/article/details/53116856
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;
using namespace cv;
int main()
{
//cv::Mat testMat = cv::Mat::zeros ( 3, 3, CV_8UC1 );
Mat dst;
Mat testMat=(Mat_<int>(4,4) << 1,5,4,2,8,9,7,3,6,10,2,3,6,9,8,5);
std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
std::cout<<"testMat = "<<testMat<<std::endl;
//cv::Mat result = testMat.reshape ( 0, 1 );
cv::Mat flat;
testMat.reshape(1,1).copyTo(flat);
cv::sort(flat,flat,CV_SORT_EVERY_ROW + CV_SORT_ASCENDING);
flat.reshape(1,4).copyTo(dst);
cout << " size of flat: " << flat.rows << " x " << flat.cols << std::endl;
cout << " size of reshaped dst: " << dst.rows << " x " << dst.cols << std::endl;
cout << "result = " << dst << std::endl;
waitKey(0);
system("pause");
return 0;
}
reshape有两个参数:
其中,参数:cn为新的通道数,如果cn = 0,表示通道数不会改变。
参数rows为新的行数,如果rows = 0,表示行数不会改变。
注意:新的行*列必须与原来的行*列相等。