转自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. Oracle Index Clustering Factor(集群因子)

    一.本文说明: 今天在做测试的时候发现字段上有索引,但是执行计划就是不走索引,经过在网上查找才发现原来是索引的集群因子过高导致的.本文属于转载 二.官网说明 The index clustering ...

  2. GEF入门实例_总结_03_显示菜单和工具栏

    一.前言 本文承接上一节: GEF入门实例_总结_02_新建初始RCP空项目 这一节,我们来给我们的插件加上菜单. 二.基础知识 1.action bar.menubar.coolbar   含义 a ...

  3. @angular/cli项目构建--interceptor

    JWTInterceptor import {Injectable} from '@angular/core'; import {HttpEvent, HttpHandler, HttpInterce ...

  4. const指针和指向const的指针

    int *const p=&a; 这是const指针,这种指针必须在定义时就给出它所指向的地址,否则会error:uninitialized const 'p'.const指针的指针本身是co ...

  5. HQ的测试流程

    测试流程如下图:

  6. 两个VLC实现播放串流测试

    实现原理: 一个VLC打开视频文件发布串流(格式HTTP.RTP.RTSP等),另一个VLC打开串流播放 发布串流步骤: 1.菜单“媒体”->“流”,先添加视频文件.选择“串流”,如下图: 2. ...

  7. svn 服务器安装包下载地址

    svn 服务器安装包下载地址 https://www.visualsvn.com/server/download/

  8. 如果两个人,两台电脑同时登录同一个帐号,同时对同一个账单提交,账单同时被服务器处理,那服务器应该先处理谁的,或者怎么规避这个问题。 非单点登录,重定向,stoken拦截器的问题

    方法一:给用户设置个状态 服务器端坐标记,比如数据库中增加一列,标识是否登陆,登录时先判断这个就行了,不过要考虑非正常退出的情况 http 方法二:在用户表里面 多加一个状态字段,登录成功 改变状态  ...

  9. Git 常用命令详解(三)

    转自:http://www.cnblogs.com/1-2-3/archive/2010/07/18/git-commands.html

  10. 使用Nginx配置资源虚拟路径

    不实用Nginx的时候我们会使用虚拟路径来配置: 在tomcat下的conf/server.xml中增加一个代码 在<Host></Host>中间 如下: <!--增加的 ...