轉自http://blog.csdn.net/yang6464158/article/details/20129991
reshape有兩個參數:
其中,參數:cn為新的通道數,如果cn = 0,表示通道數不會改變。
參數rows為新的行數,如果rows = 0,表示行數不會改變。
注意:新的行*列必須與原來的行*列相等。就是說,如果原來是5行3列,新的行和列可以是1行15列,3行5列,5行3列,15行1列。僅此幾種,否則會報錯。
具體調用也很簡單,代碼如下所示:
- #include <iostream>
- #include <opencv/cv.h>
- #include <opencv/highgui.h>
- int main()
- {
- cv::Mat testMat = cv::Mat::zeros ( 5, 3, CV_8UC3 );
- std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- std::cout<<"testMat = "<<testMat<<std::endl;
- cv::Mat result = testMat.reshape ( 0, 3 );
- std::cout << " size of original testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- std::cout << " size of reshaped testMat: " << result.rows << " x " << result.cols << std::endl;
- std::cout << "result = " << result << std::endl;
- cv::waitKey(0);
- system("pause");
- return 0;
- }
結果如下:
比如說:下面的情況就會報錯:
- #include <iostream>
- #include <opencv/cv.h>
- #include <opencv/highgui.h>
- int main()
- {
- cv::Mat testMat = cv::Mat::ones ( 5, 3, CV_8UC3 );
- std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- std::cout<<"testMat = "<<testMat<<std::endl;
- cv::Mat result = testMat.reshape ( 0, 6 );
- std::cout << " size of original testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- std::cout << " size of reshaped testMat: " << result.rows << " x " << result.cols << std::endl;
- std::cout << "result = " << result << std::endl;
- cv::waitKey(0);
- system("pause");
- return 0;
- }
因為行和列的乘積不相等
結果如下:
我們在使用reshape的時候一定不能用定義的Mat類型賦給原來的類型,必須重新定義一個新類。
可以這樣:
- unsigned char v11[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
- cv::Mat A = cv::Mat(3, 4, CV_8U, v11);
- cv::Mat B = A.reshape(1, 12);
但不能這樣:
- cv::Mat testMat = cv::Mat::zeros ( 5, 3, CV_8UC3 );
- std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- testMat.reshape ( 0, 1 );
- std::cout << " size of reshaped testMat: " << testMat.rows << " x " << testMat.cols << std::endl;