转自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. }

结果如下:

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

  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. }

因为行和列的乘积不相等

结果如下:

我们在使用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;

opencv reshape函数说明的更多相关文章

  1. opencv3学习:reshape函数

    在opencv中,reshape函数比较有意思,它既可以改变矩阵的通道数,又可以对矩阵元素进行序列化,非常有用的一个函数. 函数原型: C++: Mat Mat::reshape() const 参数 ...

  2. Matlab 的reshape函数

    看Matlab的help文档讲得不是清楚. 先给上一段代码: >> a=[1 2 3;4 5 6;7 8 9;10 11 12]; >> b=reshape(a,2,6); 这 ...

  3. Matlab 的reshape函数(转)

    看Matlab的help文档讲得不是清楚. 先给上一段代码: >> a=[1 2 3;4 5 6;7 8 9;10 11 12]; >> b=reshape(a,2,6); 这 ...

  4. OpenCV reshape The Matrix is not continuous, thus its number of rows can not be changed

    When using OpenCV  reshape and gets this error: OpenCV Error: Image step is wrong (The matrix is not ...

  5. python numpy.shape 和 numpy.reshape函数

    导入numpy模块   from numpy import *   import numpy as np ############################################### ...

  6. opencv-6-图像绘制与opencv Line 函数剖析

    opencv-6-图像绘制与opencv Line 函数剖析 opencvc++qt 开始之前 越到后面, 写的越慢, 之前还抽空去看了下 学堂在线那篇文章提供的方法, 博客第一个人评论的我, 想想还 ...

  7. 【记录一个问题】macos下lldb调试opencv的一个程序,出现“failed to load objfile for”错误,并且无法调试进入opencv的函数

    opencv编译使用了Debug版本,打开了BUILD_WITH_DEBUG_INFO=ON选项. 发现问题后,我又在CMAKE_CXX_FLAGS_DEBUG中设置为 -g -ggdb3,在CMAK ...

  8. 学习OpenCV——Gabor函数的应用

    原文:http://blog.csdn.net/yao_zhuang/article/details/2532279 下载cvgabor.cpp和cvgabor.h到你的C/C++工程目录下 注:在我 ...

  9. 【麦子学院】OpenCV教程函数总结

    个自带样例. parter 1: No1. adaptiveskindetector.cpp 利用HSV空间的色调信息的皮肤检測,背景不能有太多与肤色相似的颜色.效果不是特别好. No2. bagof ...

随机推荐

  1. 常用T-CODE ,快捷键

    RSA1 --主界面. RSA8 --后台处理数据源和层次结构,很少使用 RSRT --QUERY 测试  .输入技术名称查询即可 RSRV--分析修复BW对象 RSMO --信息包监测. 检查处理链 ...

  2. 红帽企业版Linux成为Linux下的.NET Core的参考平台

    微软和红帽声明将在红帽企业版Linux运行的.NET纳入官方支持.经两家公司透露,“红帽企业级Linux将成为Linux下的.NET Core主要参考操作系统”. 来自红帽资深开发者Harry Mow ...

  3. winform中DataGridView使用DataGridViewCheckBoxColumn实现RadioBox单选功能

    private void dgvMaterial_CellContentClick(object sender, DataGridViewCellEventArgs e) { ; i < dgv ...

  4. POJ--1094--Sorting It All Out||NYOJ--349--Sorting It All Out(拓扑排序)

    NYOJ的数据水一点,POJ过了是真的过了 /* 拓扑排序模板题: 每次输入都要判断有环与有序的情况,如果存在环路或者已经有序可以输出则跳过下面的输入 判断有序,通过是否在一个以上的入度为0的点,存在 ...

  5. DECLARE_MESSAGE_MAP用法

    DECLARE_MESSAGE_MAP( ) 说明:  你的程序中的每一个CCmdTarget的派生类都可以提供一个消息映射以处理消息.在你的类声明的末尾使用DECLARE_MESSAGE_MAP宏. ...

  6. pip镜像源

    pip是用国内镜像源 https://pypi.python.org/http://pypi.douban.com/simple/ http://mirrors.aliyun.com/pypi/sim ...

  7. 使用open_read_write等底层函数来赋值一个文件

    /* * 该程序是练习read(),write(),open(),create(),close(),lseek()等函数. *  * 该程序的处理思路: *  1: 在程序所在的目录去打开一个文件,如 ...

  8. How To Uninstall Software Using The Ubuntu Command Line

    How To Uninstall Software Using The Ubuntu Command Line Uninstall Ubuntu Software Using The Terminal ...

  9. Linux sed 批量替换多个文件中的字符串【转载】

    原文网址:http://blog.sina.com.cn/s/blog_730edb930100qzz5.html     比如,要将目录/modules下面所有文件中的zhangsan都修改成lis ...

  10. Java实现Queue类

    Java实现Queue类 import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Sc ...