转自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. Git_学习_09_指定某些文件不上传

    一.前言 在git提交文件到远程分支时,可能有些文件我们并不想上传. 这时可以使用如下命令来将这些文件从暂存区移除 git rm --cached "文件路径" 注:git add ...

  2. luogu 3375 KMP模板题

    #include<bits/stdc++.h> using namespace std; ,M = ; int next[N]; void getNext(char s[]) //找nex ...

  3. New Concept English three (51)

    22 76 Predicting the future is notoriously difficult. Who could have imagined, in the mid 1970s, for ...

  4. IE9 placeholder 不兼容的解决

    坑爹的IE9-,真的是够够的了,不过公司不要求兼容这个玩意了,自己觉得兼容这个鬼还是挺有挑战性的,自己也碰到不少难题,一个个解决. css: .placeholderColor { color : # ...

  5. XML的语法

    XML的语法 文档声明: 写法 <?xml version="1.0" ?> 文档声明必须出现在xml文件的第一行和第一列的位置 属性: version="1 ...

  6. 洛谷 P2822 组合数问题

    题目描述 组合数C_n^mC​n​m​​表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法.根据组合数的 ...

  7. 【LeetCode】075. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. CentOS 7.2 PowerShell下安装Azure Module

    目前Linux版本的PowerShell还是Alpha版本,所以很多功能不能使用. 比如通过Powershell命令:install-module AzureRM在线安装Azure的Module.但我 ...

  9. 寻找php.ini之旅

    /usr/local/php-fpm/lib/php.ini 可以通过phpinfo()来查看 https://www.cnblogs.com/ChineseMoonGod/p/6474772.htm ...

  10. STM32 -- 故障记录

    1.串口2无法发送数据 1)串口2和串口1使用的时钟总线不同: usart1:RCC_APBPeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); usart2:R ...