首页 技术 正文
技术 2022年11月10日
0 收藏 585 点赞 2,779 浏览 1876 个字

转自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列。仅此几种,否则会报错。

具体调用也很简单,代码如下所示:

  1. #include <iostream>
  2. #include <opencv/cv.h>
  3. #include <opencv/highgui.h>
  4. int main()
  5. {
  6. cv::Mat testMat = cv::Mat::zeros ( 5, 3, CV_8UC3 );
  7. std::cout << “size of testMat: ” << testMat.rows << ” x ” << testMat.cols << std::endl;
  8. std::cout<<“testMat = “<<testMat<<std::endl;
  9. cv::Mat result = testMat.reshape ( 0, 3 );
  10. std::cout << ” size of original testMat: ” << testMat.rows << ” x ” << testMat.cols << std::endl;
  11. std::cout << ” size of reshaped testMat: ” << result.rows << ” x ” << result.cols << std::endl;
  12. std::cout << “result = ” << result << std::endl;
  13. cv::waitKey(0);
  14. system(“pause”);
  15. return 0;
  16. }

结果如下:

opencv reshape函数说明

比如说:下面的情况就会报错:

  1. #include <iostream>
  2. #include <opencv/cv.h>
  3. #include <opencv/highgui.h>
  4. int main()
  5. {
  6. cv::Mat testMat = cv::Mat::ones ( 5, 3, CV_8UC3 );
  7. std::cout << “size of testMat: ” << testMat.rows << ” x ” << testMat.cols << std::endl;
  8. std::cout<<“testMat = “<<testMat<<std::endl;
  9. cv::Mat result = testMat.reshape ( 0, 6 );
  10. std::cout << ” size of original testMat: ” << testMat.rows << ” x ” << testMat.cols << std::endl;
  11. std::cout << ” size of reshaped testMat: ” << result.rows << ” x ” << result.cols << std::endl;
  12. std::cout << “result = ” << result << std::endl;
  13. cv::waitKey(0);
  14. system(“pause”);
  15. return 0;
  16. }

因为行和列的乘积不相等

结果如下:

opencv reshape函数说明

我们在使用reshape的时候一定不能用定义的Mat类型赋给原来的类型,必须重新定义一个新类。

可以这样:

  1. unsigned char v11[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
  2. cv::Mat A = cv::Mat(3, 4, CV_8U, v11);
  3. cv::Mat B = A.reshape(1, 12);

但不能这样:

    1. cv::Mat testMat = cv::Mat::zeros ( 5, 3, CV_8UC3 );
    2. std::cout << “size of testMat: ” << testMat.rows << ” x ” << testMat.cols << std::endl;
    3. testMat.reshape ( 0, 1 );
    4. std::cout << ” size of reshaped testMat: ” << testMat.rows << ” x ” << testMat.cols << std::endl;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,495
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,741
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298