转自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. AI探索(三)Tensorflow编程模型

    Tensorflow编程模型 ....后续完善 import os os.environ[' import numpy as np num_points = data_array = [] for i ...

  2. MySQL 各种引擎

    数据库中的存储引擎其实是对使用了InnoDB.HEAP(也称为MEMORY).CSV.BLACKHOLE(黑洞引擎).ARCHIVE.PERFORMANCE_SCHEMA. Berkeley.Merg ...

  3. LeetCode OJ:Binary Tree Zigzag Level Order Traversal(折叠二叉树遍历)

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  4. Memcache mutex设计模式

    Memcache mutex设计模式 转自:https://timyang.net/programming/memcache-mutex/ 场景 Mutex主要用于有大量并发访问并存在cache过期的 ...

  5. Python日志统计

    #!/usr/bin/env python # coding:utf-8   import sys,time   class DisplayFormat(object):       def form ...

  6. I.MX6 make menuconfig进入x86模式

    /************************************************************************ * I.MX6 make menuconfig进入x ...

  7. 【转】Python获取当前系统时间

    转自:https://www.cnblogs.com/-ldzwzj-1991/p/5889629.html 取得时间相关的信息的话,要用到python time模块,python time模块里面有 ...

  8. MongoDB shell基本操作

    shell命令操作语法和JavaScript很类似,其实控制台底层的查询语句都是用JavaScript脚本完成操作的.使用shell 命令,需要启动mongo.exe.mongodb百科 常用shel ...

  9. bzoj 3629 [JLOI2014]聪明的燕姿——约数和定理+dfs

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3629 如果要搜索,肯定得质因数分解吧:就应该朝这个方向想. **约数和定理: 对于任意一个大 ...

  10. Angular5学习笔记 - 配置Http(七)

    一.引入Http模块 编辑\src\app\app.module.ts文件 import { HttpModule } from '@angular/http'; /* 注册模块 */ imports ...