opencv reshape函数说明
2017-02-17 16:10 fdd566 阅读(2091) 评论(0) 编辑 收藏 举报转自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;